send tasks
This commit is contained in:
@@ -3,4 +3,4 @@
|
||||
int main(void) {
|
||||
printf("Hello, World!\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,4 +161,4 @@ void DynArr_destroy(DynArr* p) {
|
||||
if (!p) return;
|
||||
free(p->data);
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
|
||||
58
src/common/dynset.c
Normal file
58
src/common/dynset.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <common/dynset.h>
|
||||
|
||||
DynSet* DynSet_Create(size_t elemSize) {
|
||||
DynSet* set = (DynSet*)malloc(sizeof(DynSet));
|
||||
if (!set) {
|
||||
return NULL;
|
||||
}
|
||||
set->arr = DynArr_create(elemSize, 1);
|
||||
if (!set->arr) {
|
||||
free(set);
|
||||
return NULL;
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
void DynSet_Destroy(DynSet* set) {
|
||||
if (set) {
|
||||
DynArr_destroy(set->arr);
|
||||
free(set);
|
||||
}
|
||||
}
|
||||
|
||||
int DynSet_Insert(DynSet* set, const void* element) {
|
||||
if (DynSet_Contains(set, element)) {
|
||||
return 0; // Element already exists
|
||||
}
|
||||
return DynArr_push_back(set->arr, element) != NULL;
|
||||
}
|
||||
|
||||
int DynSet_Contains(DynSet* set, const void* element) {
|
||||
size_t size = DynArr_size(set->arr);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
void* current = DynArr_at(set->arr, i);
|
||||
if (memcmp(current, element, set->arr->elemSize) == 0) {
|
||||
return 1; // Found
|
||||
}
|
||||
}
|
||||
return 0; // Not found
|
||||
}
|
||||
|
||||
size_t DynSet_Size(DynSet* set) {
|
||||
return DynArr_size(set->arr);
|
||||
}
|
||||
|
||||
void* DynSet_Get(DynSet* set, size_t index) {
|
||||
return DynArr_at(set->arr, index);
|
||||
}
|
||||
|
||||
void DynSet_Remove(DynSet* set, const void* element) {
|
||||
size_t size = DynArr_size(set->arr);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
void* current = DynArr_at(set->arr, i);
|
||||
if (memcmp(current, element, set->arr->elemSize) == 0) {
|
||||
DynArr_remove(set->arr, i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,38 @@ unsigned char random_byte(void) {
|
||||
return (unsigned char)(rand() % 256);
|
||||
}
|
||||
|
||||
uint32_t random_four_byte(void) {
|
||||
uint32_t x;
|
||||
unsigned char bytes[4];
|
||||
for (char i = 0; i < 4; i++) {
|
||||
uint16_t random_two_byte(void) {
|
||||
uint16_t x;
|
||||
unsigned char bytes[2];
|
||||
for (unsigned char i = 0; i < 2; i++) {
|
||||
bytes[i] = random_byte();
|
||||
}
|
||||
|
||||
memcpy(&x, bytes, sizeof(x));
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t random_four_byte(void) {
|
||||
uint32_t x;
|
||||
unsigned char bytes[4];
|
||||
for (unsigned char i = 0; i < 4; i++) {
|
||||
bytes[i] = random_byte();
|
||||
}
|
||||
|
||||
memcpy(&x, bytes, sizeof(x));
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
uint64_t random_eight_byte(void) {
|
||||
uint64_t x;
|
||||
unsigned char bytes[8];
|
||||
for (unsigned char i = 0; i < 8; i++) {
|
||||
bytes[i] = random_byte();
|
||||
}
|
||||
|
||||
memcpy(&x, bytes, sizeof(x));
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ void Task_Create(task_t* tsk) {
|
||||
tsk->state = TASK_NONE;
|
||||
memset(tsk->binary, 0, 64);
|
||||
tsk->args = DYNARR_CREATE(task_arg_t, 1);
|
||||
tsk->results = DYNARR_CREATE(task_arg_t, 1);
|
||||
}
|
||||
|
||||
void Task_DestroyArgs(task_t* task) {
|
||||
@@ -29,5 +30,11 @@ void Task_DestroyArgs(task_t* task) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!task->results) {
|
||||
TASK_ERR_CODE = TASK_ERR_INVARGS;
|
||||
return;
|
||||
}
|
||||
|
||||
DynArr_destroy(task->args);
|
||||
}
|
||||
DynArr_destroy(task->results);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ void TcpServer_Destroy(TcpServer* ptr) {
|
||||
if (ptr->clientsArrPtr) {
|
||||
for (size_t i = 0; i < ptr->clients; i++) {
|
||||
if (ptr->clientsArrPtr[i]) {
|
||||
if (ptr->clientsArrPtr[i]->capabilities) {
|
||||
DynArr_destroy(ptr->clientsArrPtr[i]->capabilities);
|
||||
}
|
||||
free(ptr->clientsArrPtr[i]);
|
||||
}
|
||||
}
|
||||
@@ -84,6 +87,7 @@ void* TcpServer_clientthreadprocess(void* ptr) {
|
||||
while (1) {
|
||||
memset(cli->dataBuf, 0, MTU); // Reset buffer
|
||||
ssize_t n = recv(cli->clientFd, cli->dataBuf, MTU, 0);
|
||||
cli->dataBufLen = n;
|
||||
|
||||
if (n == 0) {
|
||||
break; // Client disconnected
|
||||
@@ -106,6 +110,9 @@ void* TcpServer_clientthreadprocess(void* ptr) {
|
||||
size_t idx = Generic_FindClientInArrayByPtr(arr, cli, svr->clients);
|
||||
if (idx != SIZE_MAX) {
|
||||
if (arr[idx]) {
|
||||
if (arr[idx]->capabilities) {
|
||||
DynArr_destroy(arr[idx]->capabilities);
|
||||
}
|
||||
free(arr[idx]);
|
||||
arr[idx] = NULL;
|
||||
}
|
||||
@@ -147,6 +154,8 @@ void* TcpServer_threadprocess(void* ptr) {
|
||||
heapCli->on_data = tempclient.on_data;
|
||||
heapCli->on_disconnect = tempclient.on_disconnect;
|
||||
heapCli->clientId = random_four_byte();
|
||||
heapCli->dataBufLen = 0;
|
||||
heapCli->capabilities = DYNARR_CREATE(capability_t, 4);
|
||||
|
||||
size_t i;
|
||||
for (i = 0; i < svr->clients; i++) {
|
||||
@@ -289,4 +298,4 @@ size_t Generic_FindClientInArrayByPtr(TcpClient** arr, TcpClient* ptr, size_t le
|
||||
}
|
||||
|
||||
return SIZE_MAX; // Returns max unsigned, likely improbable to be correct
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,29 +5,134 @@
|
||||
#include <common/tcpd/tcpserver.h>
|
||||
#include <common/tcpd/tcpclient.h>
|
||||
#include <signal.h>
|
||||
#include <server/info.h>
|
||||
#include <common/packettype.h>
|
||||
#include <common/capability.h>
|
||||
|
||||
volatile int running = 1;
|
||||
task_queue_t taskQueue;
|
||||
|
||||
void signalHandler(int sig) {
|
||||
printf("Caught signal %d, shutting down...\n", sig);
|
||||
running = 0;
|
||||
}
|
||||
|
||||
void on_connect(TcpClient* client) {
|
||||
return;
|
||||
char helloPacket[SERVER_HELLO_PACKET_SIZE + 1]; // +1 for packet type
|
||||
helloPacket[0] = PACKET_TYPE_HELLO;
|
||||
helloPacket[1] = SERVER_INFO_VERSION;
|
||||
memcpy(&helloPacket[2], SERVER_INFO_IDENTIFIER, 16);
|
||||
send(client->clientFd, helloPacket, SERVER_HELLO_PACKET_SIZE + 1, 0);
|
||||
|
||||
printf("Client connected: %u\n", client->clientId);
|
||||
}
|
||||
|
||||
void on_data(TcpClient* client) {
|
||||
return;
|
||||
uint8_t packetType;
|
||||
ssize_t bytesReceived = client->dataBufLen;
|
||||
|
||||
if (bytesReceived == 0) {
|
||||
return;
|
||||
} else if (bytesReceived < 0) {
|
||||
perror("Error receiving data from client\n");
|
||||
return;
|
||||
}
|
||||
|
||||
packetType = client->dataBuf[0];
|
||||
switch (packetType) {
|
||||
case PACKET_TYPE_HELLO:
|
||||
// Unexpected, ignore
|
||||
break;
|
||||
case PACKET_TYPE_CLIENT_CAPABILITIES:
|
||||
printf("Received CLIENT_CAPABILITIES packet from client %u\n", client->clientId);
|
||||
// Decode capabilities here
|
||||
char* ptr = (char*)&client->dataBuf[1]; // Skip packet type byte
|
||||
size_t capabilitiesCount = (bytesReceived - 1) / sizeof(capability_t);
|
||||
for (size_t i = 0; i < capabilitiesCount; i++) {
|
||||
capability_t cap;
|
||||
memcpy(&cap, ptr + i * sizeof(capability_t), sizeof(capability_t));
|
||||
printf("Capability: %s, Supported: %s\n", cap.name, cap.supported ? "Yes" : "No");
|
||||
if (client->capabilities) {
|
||||
DynArr_push_back(client->capabilities, &cap);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case PACKET_TYPE_TASK_REQUEST:
|
||||
printf("Received TASK_REQUEST packet from client %u\n", client->clientId);
|
||||
|
||||
if (client->capabilities) {
|
||||
size_t capCount = DynArr_size(client->capabilities);
|
||||
|
||||
if (capCount <= 0) {
|
||||
// Request capabilities first
|
||||
char missingInfoPacket[2];
|
||||
missingInfoPacket[0] = PACKET_TYPE_MISSING_INFO;
|
||||
missingInfoPacket[1] = PACKET_TYPE_CLIENT_CAPABILITIES;
|
||||
send(client->clientFd, missingInfoPacket, 2, 0);
|
||||
printf("Requested capabilities from client %u\n", client->clientId);
|
||||
break;
|
||||
}
|
||||
|
||||
// Find a task that matches capabilities and assign it
|
||||
for (size_t i = 0; i < DynArr_size(&taskQueue.tasks); i++) {
|
||||
task_t* task = (task_t*)DynArr_at(&taskQueue.tasks, i);
|
||||
int assigned = 0;
|
||||
|
||||
if (task->state == TASK_PENDING) {
|
||||
// For simplicity, assign the first pending task with capabilities
|
||||
// The capabilites (for now) are the required binary name; we're just gonna strcmp it for now
|
||||
for (size_t j = 0; j < capCount; j++) {
|
||||
capability_t* cap = (capability_t*)DynArr_at(client->capabilities, j);
|
||||
if (strcmp(cap->name, task->binary) == 0 && cap->supported) {
|
||||
// Assign task to client
|
||||
assigned = 1;
|
||||
task->state = TASK_ASSIGNED;
|
||||
task->assigned_to = client->clientId;
|
||||
task->assigned_at = time(NULL);
|
||||
|
||||
// Make packet
|
||||
DynArr* packet = DYNARR_CREATE(uint8_t, 1);
|
||||
uint8_t packetType = PACKET_TYPE_TASK_ASSIGN;
|
||||
DynArr_push_back(packet, &packetType);
|
||||
|
||||
// Push back the task struct
|
||||
DynArr_push_back(packet, task);
|
||||
|
||||
send(client->clientFd, packet->data, DynArr_size(packet) * sizeof(uint8_t), 0);
|
||||
printf("Assigned task %u to client %u\n", task->taskId, client->clientId);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (assigned) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
case PACKET_TYPE_TASK_RESULT:
|
||||
printf("Received TASK_RESULT packet from client %u\n", client->clientId);
|
||||
break;
|
||||
default:
|
||||
printf("Received unknown packet type %u from client %u\n", packetType, client->clientId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void on_disconnect(TcpClient* client) {
|
||||
return;
|
||||
printf("Client disconnected: %u\n", client->clientId);
|
||||
|
||||
// TODO; Check if tasks were assigned to this client and handle them
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
signal(SIGINT, signalHandler);
|
||||
|
||||
task_queue_t taskQueue;
|
||||
TaskQueue_Init(&taskQueue);
|
||||
|
||||
printf("Task Queue Created!\n");
|
||||
@@ -59,4 +164,4 @@ int main(void) {
|
||||
printf("Destroyed!\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,4 +90,4 @@ void TaskQueue_RemoveTaskByPtr(task_queue_t* queue, task_t* task) {
|
||||
}
|
||||
|
||||
DynArr_remove(queue->tasks, at);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user