forked from scott/threaded_network_chat
express mode as enum, fix some display issues, use switch for mode checks
This commit is contained in:
48
main.cc
48
main.cc
@@ -5,7 +5,6 @@
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
@@ -13,6 +12,7 @@
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector> //try to make the program work without this without doing anything awful but do it later
|
||||
|
||||
#include "client.h"
|
||||
@@ -30,7 +30,8 @@ const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor
|
||||
const int DEFAULT_CUR_Y = 12; // the x position of the preferred default cursor
|
||||
// position for message entry
|
||||
|
||||
int mode = 0; // what mode is this program in. 0 = nothing. 1 = server. 2 = client
|
||||
chatmode_t mode = NO_MODE; // what mode is this program in. 0 = nothing. 1 = server. 2 = client
|
||||
|
||||
int serverSocketDescriptor; // a global variable for storing the server socket
|
||||
// descriptor.
|
||||
int clientSocketDescriptor;
|
||||
@@ -60,14 +61,12 @@ struct waitClientArgs {
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if (argc > 1 && argv[1][0] == 'c') {
|
||||
// client mode
|
||||
if (argc > 1 && (!strncmp(argv[1], "client", 6))) {
|
||||
logFileName = "client.txt";
|
||||
mode = 2;
|
||||
mode = CLIENT_MODE;
|
||||
} else {
|
||||
// server mode
|
||||
logFileName = "server.txt";
|
||||
mode = 1;
|
||||
mode = SERVER_MODE;
|
||||
}
|
||||
|
||||
// putting this lower down to circumvent the terminal brick when the
|
||||
@@ -87,13 +86,20 @@ main(int argc, char *argv[])
|
||||
char *userInput = new char[1024];
|
||||
bool exit = false;
|
||||
|
||||
if (mode == 2) {
|
||||
switch (mode) {
|
||||
case CLIENT_MODE:
|
||||
writeToFile(logFileName, "CLIENT MODE");
|
||||
setupClient();
|
||||
} else if (mode == 1) {
|
||||
linePos++;
|
||||
break;
|
||||
case SERVER_MODE:
|
||||
writeToFile(logFileName, "SERVER MODE");
|
||||
setupServer(PORT_NUM);
|
||||
linePos++; /* No idea why, but this needs to be here. */
|
||||
linePos++;
|
||||
break;
|
||||
default:
|
||||
goto leave;
|
||||
break;
|
||||
}
|
||||
|
||||
while (!exit) {
|
||||
@@ -107,8 +113,8 @@ main(int argc, char *argv[])
|
||||
/* clear message box / reset cursor */
|
||||
move(12, 0);
|
||||
printw(">\t\t\t");
|
||||
move(12,2);
|
||||
|
||||
move(12, 2);
|
||||
|
||||
getstr(userInput);
|
||||
writeToFile(logFileName, userInput);
|
||||
|
||||
@@ -121,13 +127,23 @@ main(int argc, char *argv[])
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
}
|
||||
|
||||
leave:
|
||||
endwin();
|
||||
//closeServer();
|
||||
// closeServer();
|
||||
|
||||
if (mode == 1)
|
||||
switch (mode) {
|
||||
case SERVER_MODE:
|
||||
closeServer();
|
||||
else if (mode == 2)
|
||||
closeClient();
|
||||
break;
|
||||
case CLIENT_MODE:
|
||||
closeServer();
|
||||
break;
|
||||
default:
|
||||
puts("Warn: program appears to have successfully finished without ever setting "
|
||||
"mode.");
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user