Skip to content

Commit

Permalink
More initial additions
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-botticelli committed Apr 26, 2023
1 parent 69f1b00 commit 7e32bce
Show file tree
Hide file tree
Showing 17 changed files with 507 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ Temporary Items



.vscode/
build/
9 changes: 0 additions & 9 deletions .vscode/settings.json

This file was deleted.

1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS := $(INC_FLAGS) -MMD -MP -Wall -Wno-unused-command-line-argument
LDFLAGS := -pthread -lpthread

#
RELEASE_FLAGS := -Ofast -DNDEBUG -fvisibility=hidden -fstack-protector-strong \
-fomit-frame-pointer -fPIE -fstack-clash-protection -fsanitize=bounds \
-fsanitize-undefined-trap-on-error -D_FORTIFY_SOURCE=3 -flto
Expand Down
3 changes: 3 additions & 0 deletions buildDebugAndRun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

make clean && CC=clang-16 make debug && ./build/chat_node
3 changes: 3 additions & 0 deletions chatnode.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ip=127.0.0.1
port=51966
username=Person
1 change: 1 addition & 0 deletions src/chat_node.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "chat_node.h"
13 changes: 13 additions & 0 deletions src/chat_node.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#pragma once

#define PORT 0xCAFE // 51966



typedef struct {
char *ip;
unsigned short port;
Expand All @@ -10,3 +16,10 @@ typedef struct {
} NodeListItem;

typedef NodeListItem *NodeList;



Node *getNode(int index);
Node *addNode(Node *node);
Node *removeNode(Node *node);
Node *removeNodeAtIndex(int index);
32 changes: 32 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// dbg.h
//
// Based on Zed's awesome debug macros
// Modified by Wolf-Dieter Otte
//

#pragma once

#include <stdio.h>
#include <errno.h>
#include <string.h>

#ifndef NDEBUG
// debug() - maskable debug message, expanded only if symbol DEBUG is defined
#define debug(M, ...) fprintf(stderr, "\n[DEBUG] %s:%d: " M, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define debug(M, ...)
#endif

// helper macro, not to be used on its own
#define clean_errno() (errno == 0 ? "None" : strerror(errno))

// logging macros - unlike debug() above, cannot be masked
#define log_err(M, ...) fprintf(stderr, "\n[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_warn(M, ...) fprintf(stderr, "\n[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_info(M, ...) fprintf(stderr, "\n[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)

// probe() checks an assertion A. if it fails, logs an error and goes to label 'error'
#define probe(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
// sentinel() put into a part of a program that should not be accessed by program flow. if still accessed, logs error and goes to 'error'
#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
150 changes: 114 additions & 36 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,95 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>

#include "chat_node.h"
#include "receiver_handler.h"
#include "sender_handler.h"

void *connection_handler(void *threadid)
// TODO: Remove
#define SERVER_ADDR "127.0.0.1"

void *send_handler(void *unused)
{
int thread_num = (int) threadid;
int sock_desc;
int sock;

int32_t inputRaw, input;
int32_t responseRaw, response;
ssize_t response_length;

struct sockaddr_in serv_addr;

sock_desc = socket(AF_INET, SOCK_STREAM, 0);
if (sock_desc < 0) {
perror("Failed to create socket!");
return EXIT_FAILURE;
// TODO: Read properties file (chatnode.properties): read IP + port + username

// Initialize socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("Failed to create socket");
return NULL;
}

// Command loop
puts("Enter a command, or type '/help' for a list of commands.\n");
while (true) {
char cmdBuf[128]; // 100 characters for message + command

printf("> ");

// Read command input and craft outbound message
scanf("%s", cmdBuf);
Message *outboundMsg = handleCommand(cmdBuf);

// Send message
// TODO: Loop through all members of chat room to send to
// sendMessage(sock, outboundMsg);
}

// sock = socket(AF_INET, SOCK_STREAM, 0);
// if (sock < 0) {
// perror("Failed to create socket");
// return NULL;
// }

// bzero((char *) &serv_addr, sizeof(serv_addr));

// serv_addr.sin_family = AF_INET;
// serv_addr.sin_port = htons(PORT);

// if (inet_pton(AF_INET, SERVER_ADDR, &serv_addr.sin_addr) <= 0) {
// perror("Error: Invalid address (address not supported)");
// return NULL;
// }

// if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
// perror("Error: Failed to connect to server");
// return NULL;
// }

close(sock);
return NULL;
}

void *receive_handler(void *unused)
{
int sock;

int32_t inputRaw, input;
int32_t responseRaw, response;
ssize_t response_length;

struct sockaddr_in serv_addr;

// Initialize socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("Failed to create socket");
return NULL;
}

bzero((char *) &serv_addr, sizeof(serv_addr));
Expand All @@ -26,49 +98,55 @@ void *connection_handler(void *threadid)
serv_addr.sin_port = htons(PORT);

if (inet_pton(AF_INET, SERVER_ADDR, &serv_addr.sin_addr) <= 0) {
perror("Error: Invalid address (address not supported)!");
return EXIT_FAILURE;
perror("Error: Invalid address (address not supported)");
return NULL;
}

if (connect(sock_desc, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error: Failed to connect to server!");
return EXIT_FAILURE;
if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error: Failed to connect to server");
return NULL;
}

printf("Client connected successfully: %d\n", thread_num);
printf("Client connected successfully.\n");
while (true) {
printf("For thread: %d\n", thread_num);
Message *message = receiveMessage(sock);
handleClient(message);

printf("Enter input: ");
scanf("%d", &inputRaw);

input = htonl(inputRaw);
// printf("Server response: %d\n", message->);
}

if (send(sock_desc, &input, sizeof(int32_t), 0) == -1) {
perror("Error: Send failed!");
return EXIT_FAILURE;
}
close(sock);
return NULL;
}

response_length = recv(sock_desc, &responseRaw, sizeof(int32_t), 0);
if (response_length != sizeof(int32_t)) {
perror("Error: Receive failed!");
return EXIT_FAILURE;
}
int main(int argc, char *argv[]) {
puts("=== chat_node ===\n");

response = ntohl(responseRaw);
pthread_t senderThread, receiverThread;

printf("Server response: %d\n", response);

sleep(5);
// Create sender thread
if (pthread_create(&senderThread, NULL, send_handler, (void *) NULL) < 0) {
perror("Error: Could not create sender thread");
return EXIT_FAILURE;
}

close(sock_desc);
return EXIT_SUCCESS;
}
// Create receiver thread
// if (pthread_create(&receiverThread, NULL, receive_handler, (void *) NULL) < 0) {
// perror("Error: Could not create receiver thread");
// return EXIT_FAILURE;
// }

// Join sender thread
if (pthread_join(senderThread, NULL)) {
fprintf(stderr, "Error: Could not join sender thread!\n");
return EXIT_FAILURE;
}

int main(int argc, char *argv[]) {
puts("=== chat_node ===\n");
// Join receiver thread
// if (pthread_join(receiverThread, NULL)) {
// fprintf(stderr, "Error: Could not join receiver thread!\n");
// return EXIT_FAILURE;
// }

return EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions src/message.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "message.h"
13 changes: 13 additions & 0 deletions src/message.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma once

#include <stdint.h>

#include <chat_node.h>

typedef uint8_t PacketHeader;
Expand Down Expand Up @@ -36,3 +40,12 @@ typedef struct {
};
};
} Message;



Message *createJoinMessage(char *name);
Message *createAddMemberMessage(Node *nodeInfo);
Message *createMemberListMessage(NodeListItem *memberList);
Message *createNoteMessage(char *message);
Message *createLeaveMessage();
Message *createShutdownAllMessage();
Loading

0 comments on commit 7e32bce

Please sign in to comment.