improve visual clarity
This commit is contained in:
24
client.cc
24
client.cc
@@ -35,28 +35,24 @@ closeClient()
|
||||
void *
|
||||
pollForSever(void *args)
|
||||
{
|
||||
int b;
|
||||
char msg[1024], msgf[1033];
|
||||
waitClientArgs *aaa = static_cast<waitClientArgs *>(args);
|
||||
int socketDescriptor = (int)aaa->auxInt;
|
||||
char msg[1024];
|
||||
|
||||
while (1) {
|
||||
server_message_loop:
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead += recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
|
||||
// not needed for proofs of concept testing
|
||||
/*if(!strcmp(msg, "exit"))
|
||||
{
|
||||
writeToFile(logFileName, msg);
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
break;
|
||||
}*/
|
||||
// cout << "Server: " << msg << endl;
|
||||
// printf("Server: %s\n");
|
||||
memset(&msg, 0, sizeof(msg)); /* clear buffer */
|
||||
//bytesRead += recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
b = recv(socketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
msg[b] = '\0';
|
||||
bytesRead += b;
|
||||
|
||||
if (msg[0] == '\0')
|
||||
goto server_message_loop;
|
||||
|
||||
writeToFile(logFileName, msg);
|
||||
snprintf(msgf, sizeof(msgf), "Server: %s", msg);
|
||||
writeToFile(logFileName, msgf);
|
||||
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
|
||||
6
disp.cc
6
disp.cc
@@ -24,7 +24,7 @@ clearRows(int startingRow, int endingRow)
|
||||
|
||||
// display a file using ncurses
|
||||
int
|
||||
displayFile(string path, int startLineNum = 0, int numLines = 10)
|
||||
displayFile(string path, int startLineNum = 0, int numLines = height - 10)
|
||||
{
|
||||
ifstream file(path);
|
||||
if (!file) {
|
||||
@@ -41,14 +41,14 @@ displayFile(string path, int startLineNum = 0, int numLines = 10)
|
||||
// line number isn't too high
|
||||
{
|
||||
if (num >= startLineNum) {
|
||||
move(lineNum, 0);
|
||||
move(lineNum, 2);
|
||||
printw("%s", line.c_str());
|
||||
lineNum++; // increment the row number after
|
||||
// printing each line
|
||||
}
|
||||
num++;
|
||||
}
|
||||
move(DEFAULT_CUR_Y, DEFAULT_CUR_X);
|
||||
movetobox();
|
||||
refresh();
|
||||
return 0;
|
||||
}
|
||||
|
||||
51
main.cc
51
main.cc
@@ -13,7 +13,8 @@
|
||||
#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 "disp.h"
|
||||
@@ -30,6 +31,7 @@ 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 width, height;
|
||||
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
|
||||
@@ -71,9 +73,10 @@ main(int argc, char *argv[])
|
||||
* userInput: character buffer for the user's next message
|
||||
* modeless: check whether a modeless exit was accidental
|
||||
*/
|
||||
int ch;
|
||||
int ch, inlen;
|
||||
bool exit = false;
|
||||
char *userInput = new char[1024];
|
||||
char *userInputF = new char[1032];
|
||||
bool modeless = false;
|
||||
|
||||
if (argc == 1) {
|
||||
@@ -101,7 +104,8 @@ main(int argc, char *argv[])
|
||||
case 'p':
|
||||
PORT_NUM = atoi(optarg);
|
||||
break;
|
||||
case 'h': default:
|
||||
case 'h':
|
||||
default:
|
||||
usage(argv[0], &modeless);
|
||||
goto leave;
|
||||
break;
|
||||
@@ -123,12 +127,12 @@ main(int argc, char *argv[])
|
||||
|
||||
switch (mode) {
|
||||
case CLIENT_MODE:
|
||||
writeToFile(logFileName, "CLIENT MODE");
|
||||
writeToFile(logFileName, "SYSTEM: CLIENT MODE");
|
||||
setupClient();
|
||||
linePos++;
|
||||
break;
|
||||
case SERVER_MODE:
|
||||
writeToFile(logFileName, "SERVER MODE");
|
||||
writeToFile(logFileName, "SYSTEM: SERVER MODE");
|
||||
setupServer(PORT_NUM);
|
||||
linePos++;
|
||||
break;
|
||||
@@ -144,7 +148,7 @@ main(int argc, char *argv[])
|
||||
box(stdscr, 0, 0);
|
||||
|
||||
// Get screen size
|
||||
int height, width;
|
||||
// int height, width;
|
||||
getmaxyx(stdscr, height, width);
|
||||
|
||||
// Draw separator line above input
|
||||
@@ -152,7 +156,18 @@ main(int argc, char *argv[])
|
||||
|
||||
// Labels
|
||||
mvprintw(0, 2, " Chat ");
|
||||
// mvprintw(height - 3, 2, " Input ");
|
||||
switch (mode) {
|
||||
case CLIENT_MODE:
|
||||
mvprintw(height - 3, 2, " Client ");
|
||||
break;
|
||||
case SERVER_MODE:
|
||||
mvprintw(height - 3, 2, " Server ");
|
||||
break;
|
||||
default:
|
||||
mvprintw(height - 3, 2, " Input ");
|
||||
break;
|
||||
}
|
||||
|
||||
// Display Chat Log
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
@@ -166,20 +181,32 @@ main(int argc, char *argv[])
|
||||
move(height - 2, 2);
|
||||
clrtoeol();
|
||||
printw("You: ");
|
||||
move(height - 2, 7);
|
||||
movetobox();
|
||||
|
||||
refresh();
|
||||
|
||||
getstr(userInput);
|
||||
writeToFile(logFileName, userInput);
|
||||
|
||||
if (!strncmp(userInput, "/quit", 5))
|
||||
exit = true;
|
||||
|
||||
if (mode == 1)
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
else
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
inlen = strlen(userInput);
|
||||
|
||||
switch (mode) {
|
||||
case SERVER_MODE:
|
||||
snprintf(userInputF, 1032 * sizeof(char), "Server: %s", userInput);
|
||||
send(clientSocketDescriptor, (char *)userInput, inlen, 0);
|
||||
break;
|
||||
case CLIENT_MODE:
|
||||
snprintf(userInputF, 1032 * sizeof(char), "Client: %s", userInput);
|
||||
send(clientSocketDescriptor, (char *)userInput, inlen, 0);
|
||||
break;
|
||||
default:
|
||||
goto leave;
|
||||
break;
|
||||
}
|
||||
|
||||
writeToFile(logFileName, userInputF);
|
||||
}
|
||||
|
||||
leave:
|
||||
|
||||
4
public.h
4
public.h
@@ -7,6 +7,7 @@ using namespace std;
|
||||
|
||||
typedef enum { NO_MODE = 0, SERVER_MODE = 1, CLIENT_MODE = 2 } chatmode_t;
|
||||
|
||||
extern int width, height;
|
||||
extern chatmode_t mode;
|
||||
extern int PORT_NUM;
|
||||
extern string IP_ADDRESS;
|
||||
@@ -27,4 +28,7 @@ void *waitForClient(void *argss);
|
||||
void *pollForClient();
|
||||
void *pollForSever(void *args);
|
||||
|
||||
#define movetobox() \
|
||||
move(height - 2, 7)
|
||||
|
||||
#endif
|
||||
|
||||
21
server.cc
21
server.cc
@@ -26,7 +26,7 @@ waitForClient(void *argss)
|
||||
clientSocketDescriptor =
|
||||
accept(serverSocketDescriptor, (sockaddr *)&args->newSockAddr, &args->newSockAddrSize);
|
||||
if (clientSocketDescriptor >= 0) {
|
||||
writeToFile(logFileName, "client connected");
|
||||
writeToFile(logFileName, "SYSTEM: Client connected.");
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
@@ -39,17 +39,22 @@ waitForClient(void *argss)
|
||||
void *
|
||||
pollForClient()
|
||||
{
|
||||
char msg[1024];
|
||||
int b;
|
||||
char msg[1024], msgf[1033];
|
||||
|
||||
while (1) {
|
||||
client_message_loop:
|
||||
// receive a message from the client (listen)
|
||||
memset(&msg, 0, sizeof(msg)); // clear the buffer
|
||||
bytesRead += recv(clientSocketDescriptor, (char *)&msg, sizeof(msg), 0);
|
||||
/* receive a message from the client (listen) */
|
||||
memset(&msg, 0, sizeof(msg)); /* clear buffer */
|
||||
b = recv(clientSocketDescriptor, (char *)&msg, sizeof(msg) - 1, 0);
|
||||
msg[b] = '\0';
|
||||
bytesRead += b;
|
||||
|
||||
if (msg[0] == '\0')
|
||||
goto client_message_loop;
|
||||
|
||||
writeToFile(logFileName, msg);
|
||||
snprintf(msgf, sizeof(msgf), "Client: %s", msg);
|
||||
writeToFile(logFileName, msgf);
|
||||
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
@@ -91,7 +96,7 @@ setupServer(int port)
|
||||
fprintf(stderr, "Error binding socket to local address!\n");
|
||||
exit(0);
|
||||
}
|
||||
writeToFile(logFileName, "Waiting for a client to connect...");
|
||||
writeToFile(logFileName, "SYSTEM: Waiting for a client to connect...");
|
||||
// listen for up to 5 requests at a time
|
||||
listen(serverSocketDescriptor, 5);
|
||||
// receive a request from client using accept
|
||||
@@ -105,7 +110,7 @@ setupServer(int port)
|
||||
aaa->newSockAddrSize = newSockAddrSize;
|
||||
int rc = pthread_create(&client_wait_thread, nullptr, waitForClient, aaa);
|
||||
pthread_detach(client_wait_thread);
|
||||
writeToFile(logFileName, "Server started successfully");
|
||||
writeToFile(logFileName, "SYSTEM: Server started successfully.");
|
||||
gettimeofday(&start1, NULL);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user