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