Compare commits

..

2 Commits

3 changed files with 49 additions and 27 deletions

24
log.cc
View File

@@ -1,9 +1,10 @@
#include <fstream> #include <sys/stat.h>
#include <fstream>
#include <ios> #include <ios>
#include "public.h"
#include "log.h" #include "log.h"
#include "public.h"
// gets the number of lines in a file. returns -1 if there was an error. // gets the number of lines in a file. returns -1 if there was an error.
int int
@@ -29,21 +30,24 @@ int
writeToFile(string path, string line, bool incLineNum) writeToFile(string path, string line, bool incLineNum)
{ {
ofstream file; ofstream file;
struct stat s;
/* /*
* Hopefully this fixes the program messing up where the server/client * It APPEARS this fixes our problem where the program has to be run twice to work correctly.
* doesn't start if the file wasn't created by a previous session.
*/ */
file.open(path, ios_base::out|ios_base::app); if (stat(path.c_str(), &s) != 0) {
file.open(path, ios_base::out);
file << "SYSTEM: No log file found. Created a new one." << endl;
file.close();
linePos += 5; /* This seems to be what actually fixes everything. Not sure. */
}
file.open(path, ios_base::app);
if (file.is_open()) { if (file.is_open()) {
file << line << endl; file << line << endl;
file.close(); file.close();
// this probably would help but theres too much broken stuff //linePos++ // We might also want this here at some point?
// right now to be sure if (incLineNum)
//{
// linePos++;
// }
return 0; return 0;
} else { } else {
// do something if it didn't work // do something if it didn't work

42
main.cc
View File

@@ -5,7 +5,6 @@
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
@@ -13,6 +12,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.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 <vector> //try to make the program work without this without doing anything awful but do it later
#include "client.h" #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 const int DEFAULT_CUR_Y = 12; // the x position of the preferred default cursor
// position for message entry // 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 int serverSocketDescriptor; // a global variable for storing the server socket
// descriptor. // descriptor.
int clientSocketDescriptor; int clientSocketDescriptor;
@@ -60,14 +61,12 @@ struct waitClientArgs {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (argc > 1 && argv[1][0] == 'c') { if (argc > 1 && (!strncmp(argv[1], "client", 6))) {
// client mode
logFileName = "client.txt"; logFileName = "client.txt";
mode = 2; mode = CLIENT_MODE;
} else { } else {
// server mode
logFileName = "server.txt"; logFileName = "server.txt";
mode = 1; mode = SERVER_MODE;
} }
// putting this lower down to circumvent the terminal brick when the // 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]; char *userInput = new char[1024];
bool exit = false; bool exit = false;
if (mode == 2) { switch (mode) {
case CLIENT_MODE:
writeToFile(logFileName, "CLIENT MODE"); writeToFile(logFileName, "CLIENT MODE");
setupClient(); setupClient();
} else if (mode == 1) { linePos++;
break;
case SERVER_MODE:
writeToFile(logFileName, "SERVER MODE"); writeToFile(logFileName, "SERVER MODE");
setupServer(PORT_NUM); setupServer(PORT_NUM);
linePos++; /* No idea why, but this needs to be here. */ linePos++;
break;
default:
goto leave;
break;
} }
while (!exit) { while (!exit) {
@@ -121,13 +127,23 @@ main(int argc, char *argv[])
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0); send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
} }
leave:
endwin(); endwin();
// closeServer(); // closeServer();
if (mode == 1) switch (mode) {
case SERVER_MODE:
closeServer(); closeServer();
else if (mode == 2) break;
closeClient(); case CLIENT_MODE:
closeServer();
break;
default:
puts("Warn: program appears to have successfully finished without ever setting "
"mode.");
return 1;
break;
}
return 0; return 0;
} }

View File

@@ -5,11 +5,13 @@
using namespace std; using namespace std;
typedef enum { NO_MODE = 0, SERVER_MODE = 1, CLIENT_MODE = 2 } chatmode_t;
extern chatmode_t mode;
extern const int PORT_NUM; extern const int PORT_NUM;
extern string IP_ADDRESS; extern string IP_ADDRESS;
extern const int DEFAULT_CUR_X; extern const int DEFAULT_CUR_X;
extern const int DEFAULT_CUR_Y; extern const int DEFAULT_CUR_Y;
extern int mode;
extern int serverSocketDescriptor; extern int serverSocketDescriptor;
extern int clientSocketDescriptor; extern int clientSocketDescriptor;
extern struct timeval start1, end1; extern struct timeval start1, end1;