forked from scott/threaded_network_chat
Compare commits
14 Commits
9625ab2881
...
refac-work
| Author | SHA1 | Date | |
|---|---|---|---|
| eb0f004954 | |||
| 9c75a62140 | |||
| d3c5518db1 | |||
| 7c1aeafab7 | |||
| be5e928369 | |||
| f947992b61 | |||
| dcd25129a5 | |||
| 099ec012a2 | |||
| bfe1ef98d4 | |||
| 99a88bca46 | |||
| 3810db7ac8 | |||
| 192e2f69ee | |||
| a054041351 | |||
| 0aa7a07c8b |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
client.txt
|
||||
server.txt
|
||||
*.o
|
||||
ct
|
||||
27
README.md
27
README.md
@@ -1,9 +1,12 @@
|
||||
# threaded network chat refactor
|
||||
|
||||
The functions have been split into semantically appropriate files but almost nothing whatsoever
|
||||
has been modified.
|
||||
I have split functions semantically into different files and tied all global variables together in
|
||||
`public.h` so they work without modifications.
|
||||
|
||||
It APPEARS that this version actually works. I tested it a little.
|
||||
Additional changes:
|
||||
- Fixed the dreaded bug where you have to launch the program twice to make it work
|
||||
- Adjusted the ncurses stuff a bit so it doesn't look as janky
|
||||
- Changed server/client mode (`mode` variable) from an integer to an enum (still works as an integer)
|
||||
|
||||
## What now?
|
||||
|
||||
@@ -21,15 +24,19 @@ and inconsistent. If you set up your editor correctly, it will take care of most
|
||||
- Distribute appropriate code to people with those responsibilities
|
||||
- Have everyone upload their respective code to their sections of the gitlab
|
||||
|
||||
## Code
|
||||
|
||||
Everything is in [src/](src).
|
||||
A mostly untouched copy of [Scott's original code](https://git.therats.win/scott/threaded_network_chat) is also in [old/](old).
|
||||
A reformatted version of that code is also in [modified-example.cc](modified-example.cc) but it can be ignored.
|
||||
|
||||
## Build
|
||||
|
||||
```
|
||||
cd src/
|
||||
make
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
./ct # run as server
|
||||
```
|
||||
|
||||
```
|
||||
./ct client # run as client
|
||||
```
|
||||
|
||||
|
||||
21
client.cc
21
client.cc
@@ -24,10 +24,12 @@ closeClient()
|
||||
|
||||
gettimeofday(&end1, NULL);
|
||||
close(clientSocketDescriptor);
|
||||
printf("********Session********");
|
||||
printf("Bytes written: %i\nBytes read: %i\n", bytesWritten, bytesRead);
|
||||
printf("Elapsed time: %ld\n secs\n", e);
|
||||
printf("Connection closed...\n");
|
||||
|
||||
puts("********Session********");
|
||||
printf("Bytes written: %i\n", bytesWritten);
|
||||
printf("Bytes read: %i\n", bytesRead);
|
||||
printf("Elapsed time: %ld secs\n", e);
|
||||
puts("Connection closed.");
|
||||
}
|
||||
|
||||
void *
|
||||
@@ -37,6 +39,7 @@ pollForSever(void *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);
|
||||
|
||||
@@ -49,11 +52,19 @@ pollForSever(void *args)
|
||||
}*/
|
||||
// cout << "Server: " << msg << endl;
|
||||
// printf("Server: %s\n");
|
||||
|
||||
if (msg[0] == '\0')
|
||||
goto server_message_loop;
|
||||
|
||||
writeToFile(logFileName, msg);
|
||||
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -77,8 +88,10 @@ setupClient()
|
||||
// try to connect...
|
||||
int status =
|
||||
connect(clientSocketDescriptor, (sockaddr *)&sendSockAddr, sizeof(sendSockAddr));
|
||||
|
||||
if (status < 0)
|
||||
writeToFile(logFileName, "Error connecting to socket!");
|
||||
|
||||
writeToFile(logFileName, "Connected to the server!");
|
||||
int bytesRead, bytesWritten = 0;
|
||||
struct timeval start1, end1;
|
||||
|
||||
2
com.txt
2
com.txt
@@ -1,2 +0,0 @@
|
||||
g++ -pthread -Wall -Wextra -O2 -std=c++17 -lcurses -g -c *.cc
|
||||
g++ -pthread -Wall -Wextra -O2 -std=c++17 -lcurses -g -o ct *.o
|
||||
28
log.cc
28
log.cc
@@ -1,7 +1,10 @@
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
|
||||
#include "public.h"
|
||||
#include "log.h"
|
||||
#include "public.h"
|
||||
|
||||
// gets the number of lines in a file. returns -1 if there was an error.
|
||||
int
|
||||
@@ -27,15 +30,24 @@ int
|
||||
writeToFile(string path, string line, bool incLineNum)
|
||||
{
|
||||
ofstream file;
|
||||
file.open(path, ios_base::app); // open the file in append mode
|
||||
struct stat s;
|
||||
|
||||
/*
|
||||
* It APPEARS this fixes our problem where the program has to be run twice to work correctly.
|
||||
*/
|
||||
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()) {
|
||||
file << line << endl;
|
||||
// this probably would help but theres too much broken stuff
|
||||
// right now to be sure if (incLineNum)
|
||||
//{
|
||||
// linePos++;
|
||||
// }
|
||||
file.close();
|
||||
//linePos++ // We might also want this here at some point?
|
||||
return 0;
|
||||
} else {
|
||||
// do something if it didn't work
|
||||
|
||||
53
main.cc
53
main.cc
@@ -1,15 +1,10 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <curses.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <ostream>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
@@ -17,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"
|
||||
@@ -34,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;
|
||||
@@ -64,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
|
||||
@@ -91,12 +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++;
|
||||
break;
|
||||
default:
|
||||
goto leave;
|
||||
break;
|
||||
}
|
||||
|
||||
while (!exit) {
|
||||
@@ -107,18 +110,40 @@ main(int argc, char *argv[])
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
|
||||
/* clear message box / reset cursor */
|
||||
move(12, 0);
|
||||
printw("> ");
|
||||
printw(">\t\t\t");
|
||||
move(12, 2);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
leave:
|
||||
endwin();
|
||||
// closeServer();
|
||||
|
||||
switch (mode) {
|
||||
case SERVER_MODE:
|
||||
closeServer();
|
||||
break;
|
||||
case CLIENT_MODE:
|
||||
closeServer();
|
||||
break;
|
||||
default:
|
||||
puts("Warn: program appears to have successfully finished without ever setting "
|
||||
"mode.");
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
4
public.h
4
public.h
@@ -5,11 +5,13 @@
|
||||
|
||||
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 string IP_ADDRESS;
|
||||
extern const int DEFAULT_CUR_X;
|
||||
extern const int DEFAULT_CUR_Y;
|
||||
extern int mode;
|
||||
extern int serverSocketDescriptor;
|
||||
extern int clientSocketDescriptor;
|
||||
extern struct timeval start1, end1;
|
||||
|
||||
22
server.cc
22
server.cc
@@ -1,6 +1,7 @@
|
||||
#include <curses.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
@@ -40,13 +41,19 @@ pollForClient()
|
||||
{
|
||||
char msg[1024];
|
||||
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);
|
||||
writeToFile(logFileName,
|
||||
msg); // write the client's message to file
|
||||
|
||||
if (msg[0] == '\0')
|
||||
goto client_message_loop;
|
||||
|
||||
writeToFile(logFileName, msg);
|
||||
|
||||
if (linesInFile(logFileName) > LOG_LENGTH)
|
||||
linePos++;
|
||||
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
}
|
||||
|
||||
@@ -98,7 +105,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 sucessfully");
|
||||
writeToFile(logFileName, "Server started successfully");
|
||||
gettimeofday(&start1, NULL);
|
||||
|
||||
return 0;
|
||||
@@ -116,8 +123,9 @@ closeServer()
|
||||
close(serverSocketDescriptor);
|
||||
|
||||
/* Don't just die silently */
|
||||
printf("********Session********\n");
|
||||
printf("Bytes written: %i\nBytes Read: %i\n", bytesWritten, bytesRead);
|
||||
printf("Elapsed time: %ld\n secs\n", e);
|
||||
printf("Connection closed...\n");
|
||||
puts("********Session********");
|
||||
printf("Bytes written: %i\n", bytesWritten);
|
||||
printf("Bytes read: %i\n", bytesRead);
|
||||
printf("Elapsed time: %ld secs\n", e);
|
||||
puts("Connection closed.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user