Compare commits
9 Commits
a14f053132
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 19361a839f | |||
| 14c29d7cd5 | |||
| 63d237226f | |||
| a72cceca00 | |||
| d7ca392e49 | |||
| 55c038fad8 | |||
| 7e7124ee11 | |||
| e87c1f74f4 | |||
| 88fa2fa2a3 |
1
.idea/dictionaries/project.xml
generated
1
.idea/dictionaries/project.xml
generated
@@ -2,6 +2,7 @@
|
|||||||
<dictionary name="project">
|
<dictionary name="project">
|
||||||
<words>
|
<words>
|
||||||
<w>mintime</w>
|
<w>mintime</w>
|
||||||
|
<w>wsock</w>
|
||||||
</words>
|
</words>
|
||||||
</dictionary>
|
</dictionary>
|
||||||
</component>
|
</component>
|
||||||
@@ -15,6 +15,7 @@ add_executable(server00 server00.c
|
|||||||
target_link_libraries(server00 wsock32 ws2_32)
|
target_link_libraries(server00 wsock32 ws2_32)
|
||||||
|
|
||||||
find_package(json-c CONFIG REQUIRED)
|
find_package(json-c CONFIG REQUIRED)
|
||||||
|
find_package(PCRE2 CONFIG REQUIRED 8BIT)
|
||||||
|
|
||||||
|
|
||||||
add_executable(server01 server01.c
|
add_executable(server01 server01.c
|
||||||
@@ -26,5 +27,13 @@ target_link_libraries(server01 PRIVATE wsock32 ws2_32 json-c::json-c)
|
|||||||
|
|
||||||
add_executable(server02 server02.c server02.h ${COMMON_SOURCES})
|
add_executable(server02 server02.c server02.h ${COMMON_SOURCES})
|
||||||
target_link_libraries(server02 wsock32 ws2_32)
|
target_link_libraries(server02 wsock32 ws2_32)
|
||||||
|
|
||||||
add_executable(server03 server03.c server03.h ${COMMON_SOURCES})
|
add_executable(server03 server03.c server03.h ${COMMON_SOURCES})
|
||||||
target_link_libraries(server03 wsock32 ws2_32)
|
target_link_libraries(server03 wsock32 ws2_32)
|
||||||
|
|
||||||
|
add_executable(server04 server04.c server04.h ${COMMON_SOURCES})
|
||||||
|
target_link_libraries(server04 wsock32 ws2_32)
|
||||||
|
add_executable(server05 server05.c server05.h ${COMMON_SOURCES})
|
||||||
|
target_link_libraries(server05 wsock32 ws2_32 PCRE2::8BIT)
|
||||||
|
add_executable(server06 server06.c server06.h ${COMMON_SOURCES})
|
||||||
|
target_link_libraries(server06 wsock32 ws2_32 PCRE2::8BIT)
|
||||||
83
client_connection_manager.py
Normal file
83
client_connection_manager.py
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
|
def print_bytes(prefix, data):
|
||||||
|
"""Print raw bytes with both hex and ASCII representation"""
|
||||||
|
hex_data = ' '.join(f'{b:02x}' for b in data)
|
||||||
|
ascii_data = ''.join(chr(b) if 32 <= b <= 126 else '.' for b in data)
|
||||||
|
print(f"{prefix} (hex): {hex_data}")
|
||||||
|
print(f"{prefix} (ascii): {ascii_data}")
|
||||||
|
|
||||||
|
def create_client(name):
|
||||||
|
"""Create and connect a client, handle initial connection sequence"""
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.connect(('localhost', 40000))
|
||||||
|
|
||||||
|
# Receive welcome message
|
||||||
|
welcome = sock.recv(1024)
|
||||||
|
print(f"\n=== {name} receiving welcome message ===")
|
||||||
|
print_bytes("Received", welcome)
|
||||||
|
|
||||||
|
# Send username
|
||||||
|
username = f"{name}\n".encode('ascii')
|
||||||
|
print(f"\n=== {name} sending username ===")
|
||||||
|
print_bytes("Sending", username)
|
||||||
|
sock.send(username)
|
||||||
|
|
||||||
|
# Receive room info
|
||||||
|
room_info = sock.recv(1024)
|
||||||
|
print(f"\n=== {name} receiving room info ===")
|
||||||
|
print_bytes("Received", room_info)
|
||||||
|
|
||||||
|
return sock
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Create first client
|
||||||
|
print("\nConnecting client 1...")
|
||||||
|
client1 = create_client("Alice")
|
||||||
|
|
||||||
|
time.sleep(1) # Small delay between connections
|
||||||
|
|
||||||
|
# Create second client
|
||||||
|
print("\nConnecting client 2...")
|
||||||
|
client2 = create_client("Bob")
|
||||||
|
|
||||||
|
# Client 1 should receive notification about client 2
|
||||||
|
join_notification = client1.recv(1024)
|
||||||
|
print("\n=== Alice receiving Bob's join notification ===")
|
||||||
|
print_bytes("Received", join_notification)
|
||||||
|
|
||||||
|
# Client 1 sends a message
|
||||||
|
message1 = "Hello Bob!\n".encode('ascii')
|
||||||
|
print("\n=== Alice sending message ===")
|
||||||
|
print_bytes("Sending", message1)
|
||||||
|
client1.send(message1)
|
||||||
|
|
||||||
|
# Client 2 receives the message
|
||||||
|
received1 = client2.recv(1024)
|
||||||
|
print("\n=== Bob receiving message ===")
|
||||||
|
print_bytes("Received", received1)
|
||||||
|
|
||||||
|
# Client 2 sends a reply
|
||||||
|
message2 = "Hi Alice!\n".encode('ascii')
|
||||||
|
print("\n=== Bob sending message ===")
|
||||||
|
print_bytes("Sending", message2)
|
||||||
|
client2.send(message2)
|
||||||
|
|
||||||
|
# Client 1 receives the reply
|
||||||
|
received2 = client1.recv(1024)
|
||||||
|
print("\n=== Alice receiving message ===")
|
||||||
|
print_bytes("Received", received2)
|
||||||
|
|
||||||
|
# Close connections
|
||||||
|
print("\nClosing connections...")
|
||||||
|
client1.close()
|
||||||
|
|
||||||
|
received1 = client2.recv(1024)
|
||||||
|
print("\n=== Bob receiving message ===")
|
||||||
|
print_bytes("Received", received1)
|
||||||
|
|
||||||
|
client2.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
25
data.c
25
data.c
@@ -13,7 +13,25 @@ SOCKET get_listen_socket() {
|
|||||||
SOCKADDR_IN serverAddr;
|
SOCKADDR_IN serverAddr;
|
||||||
|
|
||||||
WSAStartup(MAKEWORD(2,0), &WSAData);
|
WSAStartup(MAKEWORD(2,0), &WSAData);
|
||||||
SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
|
SOCKET server = socket(AF_INET, SOCK_STREAM, 0 );
|
||||||
|
|
||||||
|
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
serverAddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
bind(server, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
|
||||||
|
listen(server, 0);
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SOCKET get_listen_socket_udp() {
|
||||||
|
WSADATA WSAData;
|
||||||
|
|
||||||
|
SOCKADDR_IN serverAddr;
|
||||||
|
|
||||||
|
WSAStartup(MAKEWORD(2,2), &WSAData);
|
||||||
|
SOCKET server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
serverAddr.sin_family = AF_INET;
|
serverAddr.sin_family = AF_INET;
|
||||||
@@ -99,6 +117,11 @@ char *char_array_get_until_char(char_array_t *array, char c) {
|
|||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void char_array_put_until_char(char *arr, char_array_t *array, char c) {
|
||||||
|
char *data = char_array_get_until_char(array, c);
|
||||||
|
memcpy(arr, data, strlen(data));
|
||||||
|
}
|
||||||
|
|
||||||
char *char_array_get_bytes(char_array_t *array, size_t length) {
|
char *char_array_get_bytes(char_array_t *array, size_t length) {
|
||||||
if (length > array->size) {
|
if (length > array->size) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
2
data.h
2
data.h
@@ -22,6 +22,7 @@ void char_array_print_hex(const char_array_t *array);
|
|||||||
void char_array_wipe(char_array_t *array);
|
void char_array_wipe(char_array_t *array);
|
||||||
bool char_array_has_char(char_array_t *array, char c);
|
bool char_array_has_char(char_array_t *array, char c);
|
||||||
char *char_array_get_until_char(char_array_t *array, char c);
|
char *char_array_get_until_char(char_array_t *array, char c);
|
||||||
|
void char_array_put_until_char(char *arr, char_array_t *array, char c);
|
||||||
char *char_array_get_bytes(char_array_t *array, size_t length);
|
char *char_array_get_bytes(char_array_t *array, size_t length);
|
||||||
void char_array_shift_bytes(char_array_t *array, size_t length);
|
void char_array_shift_bytes(char_array_t *array, size_t length);
|
||||||
|
|
||||||
@@ -35,3 +36,4 @@ void byte_array_shift_bytes(byte_array_t *array, size_t length);
|
|||||||
|
|
||||||
|
|
||||||
SOCKET get_listen_socket();
|
SOCKET get_listen_socket();
|
||||||
|
SOCKET get_listen_socket_udp();
|
||||||
115
server03.c
115
server03.c
@@ -30,7 +30,7 @@ int main() {
|
|||||||
handle_args_t *args = malloc(sizeof(handle_args_t));
|
handle_args_t *args = malloc(sizeof(handle_args_t));
|
||||||
args->client = client;
|
args->client = client;
|
||||||
args->connection = connection_number++;
|
args->connection = connection_number++;
|
||||||
args->connections = *connections;
|
args->connections = connections;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
pthread_create(&thread, nullptr, handle_connection, args);
|
pthread_create(&thread, nullptr, handle_connection, args);
|
||||||
}
|
}
|
||||||
@@ -42,51 +42,74 @@ void *handle_connection(void *args) {
|
|||||||
handle_args_t *handleArgs = args;
|
handle_args_t *handleArgs = args;
|
||||||
char buffer[1024] = {0};
|
char buffer[1024] = {0};
|
||||||
int bytesReceived;
|
int bytesReceived;
|
||||||
char *message[1024];
|
status_t status = CONNECTED;
|
||||||
|
char *message = malloc(sizeof(char)*1024);
|
||||||
char_array_t *data = char_array_create(1024);
|
char_array_t *data = char_array_create(1024);
|
||||||
send(handleArgs->client, WELCOME, sizeof(WELCOME), 0);
|
send(handleArgs->client, WELCOME, sizeof(WELCOME)-1, 0);
|
||||||
bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0);
|
char *username = malloc(sizeof(char)*1024);
|
||||||
char_array_append(data, buffer, bytesReceived);
|
|
||||||
char *username = char_array_get_until_char(data, '\n');
|
|
||||||
if (username == NULL) {
|
|
||||||
printf("{%d} Failed to parse username\n", handleArgs->connection);
|
|
||||||
connections_remove(&handleArgs->connections, handleArgs->client);
|
|
||||||
free(handleArgs);
|
|
||||||
exit(3);
|
|
||||||
}
|
|
||||||
username = trim(username);
|
|
||||||
if (!username_is_valid(username)) {
|
|
||||||
printf("{%d} Invalid username\n", handleArgs->connection);
|
|
||||||
connections_remove(&handleArgs->connections, handleArgs->client);
|
|
||||||
free(handleArgs);
|
|
||||||
exit(3);
|
|
||||||
}
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
strcat(message, "* The room contains: ");
|
|
||||||
for (int i = 0; i < handleArgs->connections.size; i++) {
|
|
||||||
strcat(message, handleArgs->connections.clients[i].username);
|
|
||||||
strcat(message, " ");
|
|
||||||
}
|
|
||||||
message[strlen(message)-1] = '\n';
|
|
||||||
send(handleArgs->client, message, strlen(message), 0);
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
|
|
||||||
connections_append(&handleArgs->connections, handleArgs->client, username);
|
|
||||||
sprintf(message, "* %s has entered the room\n", username);
|
|
||||||
broadcast(&handleArgs->connections, &handleArgs->client, message);
|
|
||||||
|
|
||||||
printf("{%d} Username: %s\n", handleArgs->connection, username);
|
|
||||||
|
|
||||||
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
|
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
|
||||||
printf("{%d} Client sent: |%d| \n", handleArgs->connection, bytesReceived);
|
printf("{%d} Client sent: |%d| \n", handleArgs->connection, bytesReceived);
|
||||||
char_array_append(data, buffer, bytesReceived);
|
char_array_append(data, buffer, bytesReceived);
|
||||||
|
switch (status) {
|
||||||
|
case CONNECTED: {
|
||||||
|
if ((username = char_array_get_until_char(data, '\n')) != NULL) {
|
||||||
|
printf("{%d} Username raw: %s\n", handleArgs->connection, username);
|
||||||
|
if (username == NULL) {
|
||||||
|
printf("{%d} Failed to parse username\n", handleArgs->connection);
|
||||||
|
closesocket(handleArgs->client);
|
||||||
|
free(handleArgs);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
username = trim(username);
|
||||||
|
printf("{%d} Username after trim: %s\n", handleArgs->connection, username);
|
||||||
|
if (!username_is_valid(username)) {
|
||||||
|
printf("{%d} Invalid username\n", handleArgs->connection);
|
||||||
|
closesocket(handleArgs->client);
|
||||||
|
free(handleArgs);
|
||||||
|
pthread_exit(NULL);
|
||||||
|
exit(3);
|
||||||
|
}
|
||||||
|
pthread_mutex_lock(&mutex);
|
||||||
|
sprintf(message, "* The room contains: ");
|
||||||
|
for (int i = 0; i < handleArgs->connections->len; i++) {
|
||||||
|
strcat(message, handleArgs->connections->clients[i].username);
|
||||||
|
strcat(message, " ");
|
||||||
|
}
|
||||||
|
strcat(message, "\n");
|
||||||
|
send(handleArgs->client, message, strlen(message), 0);
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
|
connections_append(handleArgs->connections, handleArgs->client, username);
|
||||||
|
sprintf(message, "* %s has entered the room\n", username);
|
||||||
|
broadcast(handleArgs->connections, &handleArgs->client, message);
|
||||||
|
|
||||||
|
printf("{%d} Username: %s\n", handleArgs->connection, username);
|
||||||
|
status = IDENTIFIED;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case IDENTIFIED: {
|
||||||
char *request;
|
char *request;
|
||||||
while ((request = char_array_get_until_char(data, '\n')) != NULL) {
|
while ((request = char_array_get_until_char(data, '\n')) != NULL) {
|
||||||
sprintf(message, "[%s] %s\n", username, request);
|
sprintf(message, "[%s] %s\n", username, request);
|
||||||
|
broadcast(handleArgs->connections, &handleArgs->client, message);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
memset(buffer, 0, sizeof(buffer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
}
|
||||||
|
if (status == IDENTIFIED) {
|
||||||
|
sprintf(message, "* %s has left the room\n", username);
|
||||||
|
broadcast(handleArgs->connections, &handleArgs->client, message);
|
||||||
|
}
|
||||||
|
connections_remove(handleArgs->connections, handleArgs->client);
|
||||||
|
printf("{%d} Client disconnected\n", handleArgs->connection);
|
||||||
|
|
||||||
|
free(message);
|
||||||
free(args);
|
free(args);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -94,6 +117,7 @@ void *handle_connection(void *args) {
|
|||||||
connections_t *connections_create(const int size) {
|
connections_t *connections_create(const int size) {
|
||||||
connections_t *connections = malloc(sizeof(connections_t));
|
connections_t *connections = malloc(sizeof(connections_t));
|
||||||
connections->size = size;
|
connections->size = size;
|
||||||
|
connections->len = 0;
|
||||||
connections->clients = calloc(size, sizeof(client_connection_t));
|
connections->clients = calloc(size, sizeof(client_connection_t));
|
||||||
return connections;
|
return connections;
|
||||||
}
|
}
|
||||||
@@ -106,8 +130,8 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
size_t new_size = connections->size + 1;
|
size_t new_len = connections->len + 1;
|
||||||
if (new_size > connections->size) {
|
if (new_len > connections->size) {
|
||||||
connections->size = connections->size+64;
|
connections->size = connections->size+64;
|
||||||
client_connection_t *new_array = realloc(connections->clients, connections->size * sizeof(client_connection_t));
|
client_connection_t *new_array = realloc(connections->clients, connections->size * sizeof(client_connection_t));
|
||||||
connections->clients = new_array;
|
connections->clients = new_array;
|
||||||
@@ -115,16 +139,16 @@ void connections_append(connections_t *connections, SOCKET client, char *usernam
|
|||||||
printf("Failed to allocate memory for array\n");
|
printf("Failed to allocate memory for array\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
connections->clients[connections->size].client = client;
|
connections->clients[connections->len].client = client;
|
||||||
connections->clients[connections->size].username = username;
|
connections->clients[connections->len].username = username;
|
||||||
connections->size = new_size;
|
connections->len = new_len;
|
||||||
printf("{%llu} Connection added\n", connections->size);
|
printf("{%llu} Connection added\n", connections->size);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
void connections_remove(connections_t *connections, SOCKET client) {
|
void connections_remove(connections_t *connections, SOCKET client) {
|
||||||
if (connections == NULL) {
|
if (connections == NULL || connections->clients == NULL) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
@@ -136,15 +160,20 @@ void connections_remove(connections_t *connections, SOCKET client) {
|
|||||||
for (int j = i; j < connections->size; j++) {
|
for (int j = i; j < connections->size; j++) {
|
||||||
connections->clients[j] = connections->clients[j+1];
|
connections->clients[j] = connections->clients[j+1];
|
||||||
}
|
}
|
||||||
connections->size--;
|
connections->len--;
|
||||||
printf("{%llu} Connection removed\n", connections->size);
|
printf("{%llu} Connection removed\n", connections->size);
|
||||||
pthread_mutex_unlock(&mutex);
|
pthread_mutex_unlock(&mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&mutex);
|
||||||
|
printf("{%llu} Connection not found\n", connections->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void broadcast(const connections_t *connections, const SOCKET *source, const char *message) {
|
void broadcast(const connections_t *connections, const SOCKET *source, const char *message) {
|
||||||
|
if (connections == NULL || connections->clients == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
for (size_t i = 0; i < connections->size; i++) {
|
for (size_t i = 0; i < connections->size; i++) {
|
||||||
if (connections->clients[i].client != *source) {
|
if (connections->clients[i].client != *source) {
|
||||||
|
|||||||
@@ -24,10 +24,13 @@ typedef struct Connections {
|
|||||||
typedef struct handleArgs {
|
typedef struct handleArgs {
|
||||||
int connection;
|
int connection;
|
||||||
SOCKET client;
|
SOCKET client;
|
||||||
connections_t connections;
|
connections_t *connections;
|
||||||
} handle_args_t;
|
} handle_args_t;
|
||||||
|
|
||||||
|
typedef enum Status {
|
||||||
|
CONNECTED,
|
||||||
|
IDENTIFIED
|
||||||
|
} status_t;
|
||||||
|
|
||||||
void *handle_connection(void *args);
|
void *handle_connection(void *args);
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import socket
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
import sys
|
||||||
|
|
||||||
class TestBudgetChat(unittest.TestCase):
|
class TestBudgetChat(unittest.TestCase):
|
||||||
SERVER_HOST = 'localhost'
|
SERVER_HOST = 'localhost'
|
||||||
@@ -39,9 +40,11 @@ class TestBudgetChat(unittest.TestCase):
|
|||||||
_ = self.receive_message(client) # Welcome message
|
_ = self.receive_message(client) # Welcome message
|
||||||
client.send(f"{name}\n".encode('ascii'))
|
client.send(f"{name}\n".encode('ascii'))
|
||||||
# Server should disconnect us
|
# Server should disconnect us
|
||||||
response = self.receive_message(client)
|
|
||||||
with self.assertRaises((ConnectionResetError, ConnectionAbortedError, socket.error)):
|
with self.assertRaises((ConnectionResetError, ConnectionAbortedError, socket.error)):
|
||||||
client.send("test\n".encode('ascii'))
|
client.sendall("test\n".encode('ascii'))
|
||||||
|
response = self.receive_message(client)
|
||||||
|
response = self.receive_message(client)
|
||||||
|
response = self.receive_message(client)
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
def test_valid_username(self):
|
def test_valid_username(self):
|
||||||
@@ -95,7 +98,7 @@ class TestBudgetChat(unittest.TestCase):
|
|||||||
client.send(f"{username}\n".encode('ascii'))
|
client.send(f"{username}\n".encode('ascii'))
|
||||||
_ = self.receive_message(client) # Room info
|
_ = self.receive_message(client) # Room info
|
||||||
clients.append(client)
|
clients.append(client)
|
||||||
time.sleep(0.1) # Small delay to prevent race conditions
|
# time.sleep(0.1) # Small delay to prevent race conditions
|
||||||
|
|
||||||
# Send a message from the last client
|
# Send a message from the last client
|
||||||
test_message = "Hello from last user!"
|
test_message = "Hello from last user!"
|
||||||
@@ -105,6 +108,7 @@ class TestBudgetChat(unittest.TestCase):
|
|||||||
expected = f"[{usernames[-1]}] {test_message}"
|
expected = f"[{usernames[-1]}] {test_message}"
|
||||||
for i in range(len(clients)-1):
|
for i in range(len(clients)-1):
|
||||||
received = self.receive_message(clients[i])
|
received = self.receive_message(clients[i])
|
||||||
|
print(f"Received: {received}")
|
||||||
self.assertEqual(expected, received)
|
self.assertEqual(expected, received)
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
@@ -126,10 +130,49 @@ class TestBudgetChat(unittest.TestCase):
|
|||||||
# Close client2 and check if client1 receives departure message
|
# Close client2 and check if client1 receives departure message
|
||||||
client2.close()
|
client2.close()
|
||||||
departure_msg = self.receive_message(client1)
|
departure_msg = self.receive_message(client1)
|
||||||
|
print(departure_msg)
|
||||||
self.assertTrue("user2" in departure_msg)
|
self.assertTrue("user2" in departure_msg)
|
||||||
self.assertTrue(departure_msg.startswith("*"))
|
self.assertTrue(departure_msg.startswith("*"))
|
||||||
|
|
||||||
client1.close()
|
client1.close()
|
||||||
|
|
||||||
|
def list_tests():
|
||||||
|
"""List all available tests"""
|
||||||
|
print("\nAvailable tests:")
|
||||||
|
test_methods = [method for method in dir(TestBudgetChat) if method.startswith('test_')]
|
||||||
|
for i, test in enumerate(test_methods, 1):
|
||||||
|
print(f"{i}. {test}")
|
||||||
|
print("\n0. Run all tests")
|
||||||
|
|
||||||
|
def run_selected_test(test_number):
|
||||||
|
"""Run a specific test based on user selection"""
|
||||||
|
suite = unittest.TestSuite()
|
||||||
|
test_methods = [method for method in dir(TestBudgetChat) if method.startswith('test_')]
|
||||||
|
|
||||||
|
if test_number == 0:
|
||||||
|
# Run all tests
|
||||||
|
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBudgetChat))
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
selected_test = test_methods[test_number - 1]
|
||||||
|
suite.addTest(TestBudgetChat(selected_test))
|
||||||
|
except IndexError:
|
||||||
|
print("Invalid test number!")
|
||||||
|
return
|
||||||
|
|
||||||
|
runner = unittest.TextTestRunner(verbosity=2)
|
||||||
|
runner.run(suite)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
while True:
|
||||||
|
list_tests()
|
||||||
|
try:
|
||||||
|
choice = input("\nEnter test number (or 'q' to quit): ")
|
||||||
|
if choice.lower() == 'q':
|
||||||
|
break
|
||||||
|
test_number = int(choice)
|
||||||
|
run_selected_test(test_number)
|
||||||
|
except ValueError:
|
||||||
|
print("Please enter a valid number!")
|
||||||
|
|
||||||
|
input("\nPress Enter to continue...")
|
||||||
137
server04.c
Normal file
137
server04.c
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
//
|
||||||
|
// Created by PeterDwyer on 30/07/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "server04.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "data.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <inaddr.h>
|
||||||
|
|
||||||
|
#define BUF_SIZE 1024
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
const SOCKET server = get_listen_socket_udp();
|
||||||
|
SOCKADDR_IN clientAddr;
|
||||||
|
SOCKET client;
|
||||||
|
int clientAddrSize = sizeof(clientAddr);
|
||||||
|
int connection_number = 1;
|
||||||
|
char *request = malloc(BUF_SIZE);
|
||||||
|
char buffer[BUF_SIZE] = {0};
|
||||||
|
data_map_t *data = data_map_create(1024);
|
||||||
|
printf("Listening for incoming connections...\n");
|
||||||
|
while (true) {
|
||||||
|
int bytesReceived = recvfrom(server, buffer, sizeof(buffer), 0, (SOCKADDR *) &clientAddr, &clientAddrSize);
|
||||||
|
const char *equals = "=";
|
||||||
|
size_t pos;
|
||||||
|
// IN_ADDR addr = clientAddr.sin_addr;
|
||||||
|
// printf("{%d} Client ip: |%d.%d.%d.%d| \n", connection_number, addr.S_un.S_un_b.s_b1, addr.S_un.S_un_b.s_b2, addr.S_un.S_un_b.s_b3, addr.S_un.S_un_b.s_b4);
|
||||||
|
// printf("{%d} Client port: |%d| \n", connection_number, clientAddr.sin_port);
|
||||||
|
printf("{%d} Client sent: |%s| \n", connection_number, buffer);
|
||||||
|
if ((pos = strcspn(buffer, equals)) != strlen(buffer)) {
|
||||||
|
key_value_t *kv = malloc(sizeof(key_value_t));
|
||||||
|
kv->key = malloc(sizeof(char)*1024);
|
||||||
|
kv->value = malloc(sizeof(char)*1024);
|
||||||
|
strncpy_s(kv->key, BUF_SIZE, buffer, pos);
|
||||||
|
strncpy_s(kv->value, BUF_SIZE, buffer + sizeof(char)*(pos+1), sizeof(buffer) - pos - 1);
|
||||||
|
data_map_insert_kv(data, kv);
|
||||||
|
printf("{%d} Key: |%s| Value: |%s| \n", connection_number, kv->key, kv->value);
|
||||||
|
} else {
|
||||||
|
printf("{%d} Client query: |%s| \n", connection_number, buffer);
|
||||||
|
char *value = data_map_get(data, buffer);
|
||||||
|
if (strcmp(buffer, "version") == 0) {
|
||||||
|
const char *version = "version=Ken's Key-Value Store 1.0";
|
||||||
|
sendto(server, version, strlen(version), 0, (SOCKADDR *) &clientAddr, clientAddrSize);
|
||||||
|
} else {
|
||||||
|
sprintf(buffer, "%s=%s", buffer, value);
|
||||||
|
sendto(server, buffer, strlen(buffer), 0, (SOCKADDR *) &clientAddr, clientAddrSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// sendto(server, buffer, bytesReceived, 0, (SOCKADDR *) &clientAddr, clientAddrSize);
|
||||||
|
// strncpy_s(request, BUF_SIZE, buffer, bytesReceived);
|
||||||
|
// printf("{%d} Client sent: |%s| \n", connection_number, request);
|
||||||
|
connection_number++;
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
}
|
||||||
|
data_map_free(data);
|
||||||
|
free(request);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
data_map_t *data_map_create(const int capacity) {
|
||||||
|
data_map_t *map = malloc(sizeof(data_map_t));
|
||||||
|
map->capacity = capacity;
|
||||||
|
map->count = 0;
|
||||||
|
map->data = calloc(capacity, sizeof(key_value_t));
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
void data_map_free(data_map_t *map) {
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
free(map->data[i].key);
|
||||||
|
free(map->data[i].value);
|
||||||
|
}
|
||||||
|
free(map->data);
|
||||||
|
free(map);
|
||||||
|
}
|
||||||
|
void data_map_insert_kv(data_map_t *map, key_value_t *kv) {
|
||||||
|
if (map==NULL || map->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
if (strcmp(map->data[i].key, kv->key) == 0) {
|
||||||
|
map->data[i].value = kv->value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const size_t new_size = map->count + 1;
|
||||||
|
if (new_size > map->capacity) {
|
||||||
|
map->capacity = map->capacity+1024;
|
||||||
|
key_value_t *new_array = realloc(map->data, map->capacity * sizeof(key_value_t));
|
||||||
|
map->data = new_array;
|
||||||
|
}
|
||||||
|
map->data[map->count] = *kv;
|
||||||
|
map->count = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void data_map_append(data_map_t *map, char *key, char *value) {
|
||||||
|
if (map->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
const size_t new_size = map->count + 1;
|
||||||
|
if (new_size > map->capacity) {
|
||||||
|
map->capacity = map->capacity+1024;
|
||||||
|
key_value_t *new_array = realloc(map->data, map->capacity * sizeof(key_value_t));
|
||||||
|
map->data = new_array;
|
||||||
|
if (map->data == NULL) {
|
||||||
|
printf("Failed to allocate memory for array\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map->data[map->count].key = key;
|
||||||
|
map->data[map->count].value = value;
|
||||||
|
map->count = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void data_map_insert(data_map_t *map, char *key, char *value) {
|
||||||
|
if (map->data == NULL) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
if (strcmp(map->data[i].key, key) == 0) {
|
||||||
|
map->data[i].value = value;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data_map_append(map, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *data_map_get(const data_map_t *map, const char *key) {
|
||||||
|
for (int i = 0; i < map->count; i++) {
|
||||||
|
if (strcmp(map->data[i].key, key) == 0) {
|
||||||
|
return map->data[i].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
28
server04.h
Normal file
28
server04.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Created by PeterDwyer on 30/07/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SERVER04_H
|
||||||
|
#define SERVER04_H
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#endif //SERVER04_H
|
||||||
|
|
||||||
|
typedef struct KeyValue {
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
|
}key_value_t;
|
||||||
|
|
||||||
|
typedef struct DataMap {
|
||||||
|
size_t capacity;
|
||||||
|
size_t count;
|
||||||
|
key_value_t *data;
|
||||||
|
} data_map_t;
|
||||||
|
|
||||||
|
|
||||||
|
data_map_t *data_map_create(int capacity);
|
||||||
|
void data_map_free(data_map_t *map);
|
||||||
|
void data_map_append(data_map_t *map, char *key, char *value);
|
||||||
|
void data_map_insert(data_map_t *map, char *key, char *value);
|
||||||
|
void data_map_insert_kv(data_map_t *map, key_value_t *kv);
|
||||||
|
char *data_map_get(const data_map_t *map, const char *key);
|
||||||
205
server04_database_client_test_suite.py
Normal file
205
server04_database_client_test_suite.py
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
from typing import Optional, Dict, List, Tuple
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseTestClient:
|
||||||
|
def __init__(self, host: str = 'localhost', server_port: int = 40000):
|
||||||
|
self.host = host
|
||||||
|
self.server_port = server_port
|
||||||
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM )
|
||||||
|
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
# Bind to any available interface (empty string) on an ephemeral port (0)
|
||||||
|
self.sock.bind(('0.0.0.0', 50000))
|
||||||
|
# Get the actual port assigned
|
||||||
|
self.client_port = self.sock.getsockname()[1]
|
||||||
|
self.sock.settimeout(5.0) # 1 second timeout for receives
|
||||||
|
print(f"Client bound to port {self.client_port}")
|
||||||
|
|
||||||
|
def send_insert(self, key: str, value: str) -> None:
|
||||||
|
"""Send an insert request."""
|
||||||
|
message = f"{key}={value}"
|
||||||
|
if len(message) >= 1000:
|
||||||
|
raise ValueError("Message too long")
|
||||||
|
self.sock.sendto(message.encode(), (self.host, self.server_port))
|
||||||
|
|
||||||
|
def send_retrieve(self, key: str) -> Optional[str]:
|
||||||
|
"""Send a retrieve request and return the response."""
|
||||||
|
if len(key) >= 1000:
|
||||||
|
raise ValueError("Key too long")
|
||||||
|
self.sock.sendto(key.encode(), (self.host, self.server_port))
|
||||||
|
|
||||||
|
try:
|
||||||
|
data, addr = self.sock.recvfrom(1024) # Changed from recvmsg to recvfrom
|
||||||
|
return data.decode()
|
||||||
|
except socket.timeout:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.sock.close()
|
||||||
|
|
||||||
|
def run_basic_tests(client: DatabaseTestClient) -> List[Tuple[str, bool, str]]:
|
||||||
|
"""Run basic functionality tests."""
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# Test 1: Basic insert and retrieve
|
||||||
|
client.send_insert("test1", "value1")
|
||||||
|
time.sleep(0.1) # Give server time to process
|
||||||
|
response = client.send_retrieve("test1")
|
||||||
|
results.append(("Basic insert/retrieve",
|
||||||
|
response == "test1=value1",
|
||||||
|
f"Expected 'test1=value1', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 2: Update existing key
|
||||||
|
client.send_insert("test1", "value2")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("test1")
|
||||||
|
results.append(("Update existing key",
|
||||||
|
response == "test1=value2",
|
||||||
|
f"Expected 'test1=value2', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 3: Empty key
|
||||||
|
client.send_insert("", "empty_key_value")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("")
|
||||||
|
results.append(("Empty key",
|
||||||
|
response == "=empty_key_value",
|
||||||
|
f"Expected '=empty_key_value', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 4: Empty value
|
||||||
|
client.send_insert("empty_value", "")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("empty_value")
|
||||||
|
results.append(("Empty value",
|
||||||
|
response == "empty_value=",
|
||||||
|
f"Expected 'empty_value=', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 5: Version check
|
||||||
|
response = client.send_retrieve("version")
|
||||||
|
results.append(("Version check",
|
||||||
|
response is not None and response.startswith("version=") and len(response) > 8,
|
||||||
|
f"Version response invalid: '{response}'"))
|
||||||
|
|
||||||
|
# Test 6: Version modification attempt
|
||||||
|
client.send_insert("version", "HACKED")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("version")
|
||||||
|
results.append(("Version modification prevention",
|
||||||
|
response is not None and not response.endswith("HACKED"),
|
||||||
|
f"Version should not be modifiable, got: '{response}'"))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def run_edge_case_tests(client: DatabaseTestClient) -> List[Tuple[str, bool, str]]:
|
||||||
|
"""Run edge case tests."""
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# Test 1: Key with multiple equals signs
|
||||||
|
client.send_insert("multi=equals", "value=with=equals")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("multi=equals")
|
||||||
|
results.append(("Multiple equals in key",
|
||||||
|
response is None or not response.startswith("multi=equals"),
|
||||||
|
"Key with equals sign should not be stored"))
|
||||||
|
|
||||||
|
# Test 2: Value with equals signs
|
||||||
|
client.send_insert("test2", "value=with=equals")
|
||||||
|
time.sleep(0.1)
|
||||||
|
response = client.send_retrieve("test2")
|
||||||
|
results.append(("Equals in value",
|
||||||
|
response == "test2=value=with=equals",
|
||||||
|
f"Expected 'test2=value=with=equals', got '{response}'"))
|
||||||
|
|
||||||
|
# Test 3: Non-existent key
|
||||||
|
response = client.send_retrieve("nonexistent")
|
||||||
|
results.append(("Non-existent key",
|
||||||
|
response is None or response == "nonexistent=",
|
||||||
|
f"Expected None or 'nonexistent=', got '{response}'"))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def run_concurrent_tests(num_clients: int = 5) -> List[Tuple[str, bool, str]]:
|
||||||
|
"""Run concurrent access tests."""
|
||||||
|
results = []
|
||||||
|
clients = [DatabaseTestClient() for _ in range(num_clients)]
|
||||||
|
|
||||||
|
def concurrent_ops(client_id: int):
|
||||||
|
client = clients[client_id]
|
||||||
|
key = f"concurrent_{client_id}"
|
||||||
|
for i in range(10):
|
||||||
|
value = f"value_{i}"
|
||||||
|
client.send_insert(key, value)
|
||||||
|
time.sleep(0.05)
|
||||||
|
response = client.send_retrieve(key)
|
||||||
|
if response != f"{key}={value}":
|
||||||
|
results.append((f"Concurrent client {client_id}",
|
||||||
|
False,
|
||||||
|
f"Expected '{key}={value}', got '{response}'"))
|
||||||
|
return
|
||||||
|
results.append((f"Concurrent client {client_id}",
|
||||||
|
True,
|
||||||
|
"All operations successful"))
|
||||||
|
|
||||||
|
threads = [threading.Thread(target=concurrent_ops, args=(i,))
|
||||||
|
for i in range(num_clients)]
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
|
||||||
|
for client in clients:
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# MESSAGE = "Hello, UDP!"
|
||||||
|
# sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
|
||||||
|
# sock.sendto(bytes(MESSAGE, "utf-8"), ("localhost", 40000))
|
||||||
|
# quit()
|
||||||
|
# Allow command line arguments for host and port
|
||||||
|
import argparse
|
||||||
|
parser = argparse.ArgumentParser(description='Test UDP Database Server')
|
||||||
|
parser.add_argument('--host', default='localhost', help='Server host')
|
||||||
|
parser.add_argument('--port', type=int, default=40000, help='Server port')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
print(f"Starting Unusual Database Program Tests on {args.host}:{args.port}\n")
|
||||||
|
|
||||||
|
client = DatabaseTestClient(args.host, args.port)
|
||||||
|
# ... rest of the main function remains the same ...
|
||||||
|
|
||||||
|
print("Running basic functionality tests...")
|
||||||
|
basic_results = run_basic_tests(client)
|
||||||
|
|
||||||
|
print("\nRunning edge case tests...")
|
||||||
|
edge_results = run_edge_case_tests(client)
|
||||||
|
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
print("\nRunning concurrent access tests...")
|
||||||
|
concurrent_results = run_concurrent_tests()
|
||||||
|
|
||||||
|
# Print results
|
||||||
|
all_results = basic_results + edge_results + concurrent_results
|
||||||
|
passed = sum(1 for _, success, _ in all_results if success)
|
||||||
|
total = len(all_results)
|
||||||
|
|
||||||
|
print("\nTest Results:")
|
||||||
|
print("=" * 60)
|
||||||
|
for test_name, success, message in all_results:
|
||||||
|
status = "✓" if success else "✗"
|
||||||
|
print(f"{status} {test_name}")
|
||||||
|
if not success:
|
||||||
|
print(f" {message}")
|
||||||
|
|
||||||
|
print("\nSummary:")
|
||||||
|
print(f"Passed: {passed}/{total} tests")
|
||||||
|
print(f"Success rate: {(passed/total)*100:.1f}%")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
182
server05.c
Normal file
182
server05.c
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
//
|
||||||
|
// Created by Ajurna on 30/07/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "server05.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "data.h"
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||||
|
#include <pcre2.h>
|
||||||
|
|
||||||
|
|
||||||
|
const char *newline = "\n";
|
||||||
|
const char *bogus_address = "7YWHMfk9JZe0LM0g1ZauHuiSxhI";
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
SOCKET server = get_listen_socket();
|
||||||
|
SOCKADDR_IN clientAddr;
|
||||||
|
SOCKET client;
|
||||||
|
int clientAddrSize = sizeof(clientAddr);
|
||||||
|
int connection_number = 1;
|
||||||
|
|
||||||
|
char *message = "Please pay the ticket price of 15 Boguscoins to one of these addresses: 7ABnYzozPt2cyDHQqDq2ExIMz53xXVcErn 7z6kM0VlNJpZiqTEHmRjxW9FbVCkv2flb 7qReHdk11Ai67nRvvfTQXFjPZH3X";
|
||||||
|
char *new_message = replace_bogus_coin(message);
|
||||||
|
// replace_bogus_coin(message);
|
||||||
|
printf("%s\n", new_message);
|
||||||
|
// exit(0);
|
||||||
|
printf("Listening for incoming connections...\n");
|
||||||
|
while((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
handle_args_t *args = malloc(sizeof(handle_args_t));
|
||||||
|
args->client = client;
|
||||||
|
args->server = get_chat_socket();
|
||||||
|
args->connection = connection_number++;
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, nullptr, handle_server, args);
|
||||||
|
pthread_create(&thread, nullptr, handle_client, args);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *handle_server(void *args) {
|
||||||
|
handle_args_t *handleArgs = args;
|
||||||
|
char buffer[1024] = {};
|
||||||
|
int bytesReceived;
|
||||||
|
printf("{%d} Server connected\n", handleArgs->connection);
|
||||||
|
char_array_t *data = char_array_create(1024);
|
||||||
|
while ((bytesReceived = recv(handleArgs->server, buffer, sizeof(buffer), 0)) > 0) {
|
||||||
|
printf("server sent {%d}: |%d| \n", handleArgs->connection, bytesReceived);
|
||||||
|
char_array_append(data, buffer, bytesReceived);
|
||||||
|
char *request;
|
||||||
|
while ((request = char_array_get_until_char(data, '\n')) != NULL) {
|
||||||
|
printf("{%d} server Said: %s\n", handleArgs->connection, request);
|
||||||
|
request = replace_bogus_coin(request);
|
||||||
|
send(handleArgs->client, request, strlen(request), 0);
|
||||||
|
send(handleArgs->client, newline, strlen(newline), 0);
|
||||||
|
free(request);
|
||||||
|
}
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
}
|
||||||
|
// closesocket(handleArgs->server);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *replace_bogus_coin(char *message) {
|
||||||
|
pcre2_code *re;
|
||||||
|
PCRE2_SPTR pattern = (PCRE2_SPTR)"\\b7\\w{25,34}\\b";
|
||||||
|
PCRE2_SPTR subject = (PCRE2_SPTR)message;
|
||||||
|
size_t subject_length = strlen((char *)subject);
|
||||||
|
int errornumber;
|
||||||
|
PCRE2_SIZE erroroffset;
|
||||||
|
PCRE2_SIZE *ovector;
|
||||||
|
int rc;
|
||||||
|
size_t offset = 0; // Keep track of where we are in the string
|
||||||
|
|
||||||
|
// Compile the pattern
|
||||||
|
re = pcre2_compile(
|
||||||
|
pattern,
|
||||||
|
PCRE2_ZERO_TERMINATED,
|
||||||
|
0,
|
||||||
|
&errornumber,
|
||||||
|
&erroroffset,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
if (re == NULL) {
|
||||||
|
PCRE2_UCHAR buff[256];
|
||||||
|
pcre2_get_error_message(errornumber, buff, sizeof(buff));
|
||||||
|
printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset, buff);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create match data block
|
||||||
|
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
|
||||||
|
char *result = strdup(message); // Create a copy of the original message
|
||||||
|
|
||||||
|
// Loop until no more matches are found
|
||||||
|
while (offset < subject_length) {
|
||||||
|
rc = pcre2_match(
|
||||||
|
re,
|
||||||
|
subject,
|
||||||
|
subject_length,
|
||||||
|
offset, // Start from last match position
|
||||||
|
0,
|
||||||
|
match_data,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
if (rc < 0) {
|
||||||
|
break; // No more matches
|
||||||
|
}
|
||||||
|
|
||||||
|
ovector = pcre2_get_ovector_pointer(match_data);
|
||||||
|
PCRE2_SIZE start = ovector[0];
|
||||||
|
PCRE2_SIZE end = ovector[1];
|
||||||
|
|
||||||
|
// Replace the matched text with bogus_address
|
||||||
|
char *temp = calloc(strlen(result) + strlen(bogus_address) + 1, sizeof(char));
|
||||||
|
strncpy(temp, result, start);
|
||||||
|
strcat(temp, bogus_address);
|
||||||
|
strcat(temp, result + end);
|
||||||
|
|
||||||
|
free(result);
|
||||||
|
result = temp;
|
||||||
|
|
||||||
|
// Update the subject and length for next iteration
|
||||||
|
subject = (PCRE2_SPTR)result;
|
||||||
|
subject_length = strlen(result);
|
||||||
|
offset = start + strlen(bogus_address);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
pcre2_match_data_free(match_data);
|
||||||
|
pcre2_code_free(re);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *handle_client(void *args) {
|
||||||
|
handle_args_t *handleArgs = args;
|
||||||
|
char buffer[1024] = {};
|
||||||
|
int bytesReceived;
|
||||||
|
printf("{%d} Client connected\n", handleArgs->connection);
|
||||||
|
char_array_t *data = char_array_create(1024);
|
||||||
|
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
|
||||||
|
printf("server sent {%d}: |%d| \n", handleArgs->connection, bytesReceived);
|
||||||
|
char_array_append(data, buffer, bytesReceived);
|
||||||
|
char *request;
|
||||||
|
|
||||||
|
while ((request = char_array_get_until_char(data, '\n')) != NULL) {
|
||||||
|
printf("{%d} client Said: %s\n", handleArgs->connection, request);
|
||||||
|
request = replace_bogus_coin(request);
|
||||||
|
send(handleArgs->server, request, strlen(request), 0);
|
||||||
|
send(handleArgs->server, newline, strlen(newline), 0);
|
||||||
|
free(request);
|
||||||
|
}
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
}
|
||||||
|
// closesocket(handleArgs->client);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
SOCKET get_chat_socket() {
|
||||||
|
WSADATA WSAData;
|
||||||
|
|
||||||
|
SOCKADDR_IN serverAddr;
|
||||||
|
struct hostent * host = gethostbyname("chat.protohackers.com");
|
||||||
|
WSAStartup(MAKEWORD(2,0), &WSAData);
|
||||||
|
SOCKET server = socket(AF_INET, SOCK_STREAM, 0 );
|
||||||
|
serverAddr.sin_addr.s_addr = AF_INET;
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
memcpy(&serverAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
|
||||||
|
|
||||||
|
serverAddr.sin_port = htons(16963);
|
||||||
|
|
||||||
|
connect(server, (SOCKADDR *) & serverAddr, sizeof (serverAddr));
|
||||||
|
return server;
|
||||||
|
}
|
||||||
31
server05.h
Normal file
31
server05.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// Created by Ajurna on 30/07/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SERVER05_H
|
||||||
|
#define SERVER05_H
|
||||||
|
#include <winsock2.h>
|
||||||
|
|
||||||
|
#endif //SERVER05_H
|
||||||
|
|
||||||
|
typedef struct HandleArgs {
|
||||||
|
int connection;
|
||||||
|
SOCKET client;
|
||||||
|
SOCKET server;
|
||||||
|
}handle_args_t;
|
||||||
|
|
||||||
|
typedef struct BogusLocation {
|
||||||
|
size_t start;
|
||||||
|
size_t end;
|
||||||
|
} bogus_location_t;
|
||||||
|
|
||||||
|
typedef enum SearchState {
|
||||||
|
START,
|
||||||
|
END
|
||||||
|
} search_state_t;
|
||||||
|
|
||||||
|
void *handle_server(void *args);
|
||||||
|
void *handle_client(void *args);
|
||||||
|
void send_message(SOCKET dest, const char *message);
|
||||||
|
char *replace_bogus_coin(char *message);
|
||||||
|
SOCKET get_chat_socket();
|
||||||
43
server06.c
Normal file
43
server06.c
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
//
|
||||||
|
// Created by Ajurna on 01/08/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "server06.h"
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include "data.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
SOCKET server = get_listen_socket();
|
||||||
|
SOCKADDR_IN clientAddr;
|
||||||
|
SOCKET client;
|
||||||
|
int clientAddrSize = sizeof(clientAddr);
|
||||||
|
int connection_number = 1;
|
||||||
|
printf("Listening for incoming connections...\n");
|
||||||
|
while((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
handle_args_t *args = malloc(sizeof(handle_args_t));
|
||||||
|
args->client = client;
|
||||||
|
args->connection = connection_number++;
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, nullptr, handle_connection, args);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *handle_connection(void *args) {
|
||||||
|
handle_args_t *handleArgs = args;
|
||||||
|
char buffer[1024] = {0};
|
||||||
|
int bytesReceived;
|
||||||
|
connection_type_t connection_type = UNKNOWN;
|
||||||
|
byte_array_t *data = byte_array_create(1024);
|
||||||
|
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
|
||||||
|
printf("Client sent {%d}: |%d| \n", handleArgs->connection, bytesReceived);
|
||||||
|
byte_array_append(data, buffer, bytesReceived);
|
||||||
|
char *request;
|
||||||
|
while ((request = byte_array_get_bytes(data, 1)) != NULL) {
|
||||||
|
parse_request(handleArgs, request, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
server06.h
Normal file
70
server06.h
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
//
|
||||||
|
// Created by Ajurna on 01/08/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SERVER06_H
|
||||||
|
#define SERVER06_H
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <winsock2.h>
|
||||||
|
|
||||||
|
#endif //SERVER06_H
|
||||||
|
|
||||||
|
typedef struct handleArgs {
|
||||||
|
int connection;
|
||||||
|
SOCKET client;
|
||||||
|
}handle_args_t;
|
||||||
|
|
||||||
|
typedef enum ConnectionType {
|
||||||
|
UNKNOWN,
|
||||||
|
CAMERA,
|
||||||
|
DISPATCHER,
|
||||||
|
} connection_type_t;
|
||||||
|
|
||||||
|
typedef enum MessageType {
|
||||||
|
ERROR_MSG = 0x10,
|
||||||
|
PLATE_MSG = 0x20,
|
||||||
|
TICKET_MSG = 0x21,
|
||||||
|
WANT_HEARTBEAT_MSG = 0x40,
|
||||||
|
HEARTBEAT_MSG = 0x41,
|
||||||
|
I_AM_CAMERA_MSG = 0x80,
|
||||||
|
I_AM_DISPATCHER_MSG = 0x81,
|
||||||
|
}message_type_t;
|
||||||
|
|
||||||
|
typedef struct ErrorMsg {
|
||||||
|
char *message;
|
||||||
|
} error_msg_t;
|
||||||
|
|
||||||
|
typedef struct PlateMsg {
|
||||||
|
char *plate;
|
||||||
|
uint32_t timestamp;
|
||||||
|
} plate_msg_t;
|
||||||
|
|
||||||
|
typedef struct TicketMsg {
|
||||||
|
char *plate;
|
||||||
|
uint16_t road;
|
||||||
|
uint16_t mile1;
|
||||||
|
uint32_t timestamp1;
|
||||||
|
uint16_t mile2;
|
||||||
|
uint32_t timestamp2;
|
||||||
|
uint16_t speed;
|
||||||
|
} ticket_msg_t;
|
||||||
|
|
||||||
|
typedef struct WantHeartbeatMsg {
|
||||||
|
uint32_t timestamp;
|
||||||
|
} want_heartbeat_msg_t;
|
||||||
|
|
||||||
|
typedef struct HeartbeatMsg {
|
||||||
|
|
||||||
|
} heartbeat_msg_t;
|
||||||
|
|
||||||
|
typedef struct IAmCameraMsg {
|
||||||
|
uint16_t road;
|
||||||
|
uint16_t mile;
|
||||||
|
uint16_t limit;
|
||||||
|
} i_am_camera_msg_t;
|
||||||
|
|
||||||
|
typedef struct IAmDispatcherMsg {
|
||||||
|
uint16_t *road;
|
||||||
|
} i_am_dispatcher_msg_t;
|
||||||
|
|
||||||
|
void *handle_connection(void *args);
|
||||||
141
speed_ticket_monitoring_system.py
Normal file
141
speed_ticket_monitoring_system.py
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
import time
|
||||||
|
from threading import Thread
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
class MessageType:
|
||||||
|
ERROR = 0x10
|
||||||
|
PLATE = 0x20
|
||||||
|
TICKET = 0x21
|
||||||
|
WANT_HEARTBEAT = 0x40
|
||||||
|
HEARTBEAT = 0x41
|
||||||
|
I_AM_CAMERA = 0x80
|
||||||
|
I_AM_DISPATCHER = 0x81
|
||||||
|
|
||||||
|
class SpeedDaemonClient:
|
||||||
|
def __init__(self, host: str = 'localhost', port: int = 12345):
|
||||||
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
self.socket.connect((host, port))
|
||||||
|
self.running = True
|
||||||
|
self.received_tickets = []
|
||||||
|
|
||||||
|
def send_bytes(self, data: bytes):
|
||||||
|
self.socket.send(data)
|
||||||
|
|
||||||
|
def receive_message(self) -> Optional[tuple]:
|
||||||
|
try:
|
||||||
|
msg_type = self.socket.recv(1)
|
||||||
|
if not msg_type:
|
||||||
|
return None
|
||||||
|
|
||||||
|
msg_type = msg_type[0]
|
||||||
|
|
||||||
|
if msg_type == MessageType.ERROR:
|
||||||
|
length = struct.unpack('!B', self.socket.recv(1))[0]
|
||||||
|
message = self.socket.recv(length).decode('ascii')
|
||||||
|
return (MessageType.ERROR, message)
|
||||||
|
|
||||||
|
elif msg_type == MessageType.TICKET:
|
||||||
|
# Read plate length
|
||||||
|
plate_len = struct.unpack('!B', self.socket.recv(1))[0]
|
||||||
|
plate = self.socket.recv(plate_len).decode('ascii')
|
||||||
|
|
||||||
|
# Read other fields
|
||||||
|
data = self.socket.recv(16)
|
||||||
|
road, mile1, timestamp1, mile2, timestamp2, speed = struct.unpack('!HHIHHI', data)
|
||||||
|
return (MessageType.TICKET, {
|
||||||
|
'plate': plate,
|
||||||
|
'road': road,
|
||||||
|
'mile1': mile1,
|
||||||
|
'timestamp1': timestamp1,
|
||||||
|
'mile2': mile2,
|
||||||
|
'timestamp2': timestamp2,
|
||||||
|
'speed': speed
|
||||||
|
})
|
||||||
|
|
||||||
|
elif msg_type == MessageType.HEARTBEAT:
|
||||||
|
return (MessageType.HEARTBEAT, None)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error receiving message: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.running = False
|
||||||
|
self.socket.close()
|
||||||
|
|
||||||
|
class Camera(SpeedDaemonClient):
|
||||||
|
def __init__(self, road: int, mile: int, limit: int, host: str = 'localhost', port: int = 12345):
|
||||||
|
super().__init__(host, port)
|
||||||
|
self.road = road
|
||||||
|
self.mile = mile
|
||||||
|
self.limit = limit
|
||||||
|
self.register()
|
||||||
|
|
||||||
|
def register(self):
|
||||||
|
# Send IAmCamera message
|
||||||
|
msg = struct.pack('!BHHH', MessageType.I_AM_CAMERA, self.road, self.mile, self.limit)
|
||||||
|
self.send_bytes(msg)
|
||||||
|
|
||||||
|
def report_plate(self, plate: str, timestamp: int):
|
||||||
|
# Send Plate message
|
||||||
|
msg = struct.pack(f'!BB{len(plate)}sI', MessageType.PLATE, len(plate),
|
||||||
|
plate.encode('ascii'), timestamp)
|
||||||
|
self.send_bytes(msg)
|
||||||
|
|
||||||
|
class Dispatcher(SpeedDaemonClient):
|
||||||
|
def __init__(self, roads: List[int], host: str = 'localhost', port: int = 12345):
|
||||||
|
super().__init__(host, port)
|
||||||
|
self.roads = roads
|
||||||
|
self.register()
|
||||||
|
# Start listening for tickets
|
||||||
|
self.listener_thread = Thread(target=self._listen_for_tickets)
|
||||||
|
self.listener_thread.start()
|
||||||
|
|
||||||
|
def register(self):
|
||||||
|
# Send IAmDispatcher message
|
||||||
|
roads_data = struct.pack(f'!{len(self.roads)}H', *self.roads)
|
||||||
|
msg = struct.pack('!BB', MessageType.I_AM_DISPATCHER, len(self.roads)) + roads_data
|
||||||
|
self.send_bytes(msg)
|
||||||
|
|
||||||
|
def _listen_for_tickets(self):
|
||||||
|
while self.running:
|
||||||
|
message = self.receive_message()
|
||||||
|
if message and message[0] == MessageType.TICKET:
|
||||||
|
self.received_tickets.append(message[1])
|
||||||
|
|
||||||
|
def run_speed_test():
|
||||||
|
# Create a dispatcher for road 123
|
||||||
|
dispatcher = Dispatcher([123])
|
||||||
|
time.sleep(0.1) # Give dispatcher time to register
|
||||||
|
|
||||||
|
# Create two cameras on road 123
|
||||||
|
camera1 = Camera(123, 8, 60) # Camera at mile 8, 60mph limit
|
||||||
|
camera2 = Camera(123, 9, 60) # Camera at mile 9, same limit
|
||||||
|
time.sleep(0.1) # Give cameras time to register
|
||||||
|
|
||||||
|
# Report a speeding car (same as in example session)
|
||||||
|
camera1.report_plate("UN1X", 0)
|
||||||
|
camera2.report_plate("UN1X", 45) # 1 mile in 45 seconds = 80mph
|
||||||
|
|
||||||
|
# Wait for ticket processing
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
# Check received tickets
|
||||||
|
if dispatcher.received_tickets:
|
||||||
|
print("Test passed! Received tickets:")
|
||||||
|
for ticket in dispatcher.received_tickets:
|
||||||
|
print(f"Plate: {ticket['plate']}")
|
||||||
|
print(f"Road: {ticket['road']}")
|
||||||
|
print(f"Speed: {ticket['speed']/100.0} mph")
|
||||||
|
else:
|
||||||
|
print("Test failed: No tickets received")
|
||||||
|
|
||||||
|
# Cleanup
|
||||||
|
dispatcher.close()
|
||||||
|
camera1.close()
|
||||||
|
camera2.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_speed_test()
|
||||||
Reference in New Issue
Block a user