server02 passed
This commit is contained in:
31
server02.c
31
server02.c
@@ -27,20 +27,21 @@ int main() {
|
||||
}
|
||||
void *handle_connection(void *args) {
|
||||
handle_args_t *handleArgs = args;
|
||||
char buffer[1024] = {0};
|
||||
uint8_t buffer[1024] = {0};
|
||||
int bytesReceived;
|
||||
char_array_t *data = char_array_create(1024);
|
||||
byte_array_t *data = byte_array_create(1024);
|
||||
price_array_t *prices = price_array_create(1024);
|
||||
while ((bytesReceived = recv(handleArgs->client, buffer, sizeof(buffer), 0)) > 0) {
|
||||
printf("{%d} Client sent: |%d| \n", handleArgs->connection, bytesReceived);
|
||||
char_array_append(data, buffer, bytesReceived);
|
||||
char *raw_request;
|
||||
while ((raw_request = char_array_get_bytes(data, 9)) != NULL) {
|
||||
byte_array_append(data, buffer, bytesReceived);
|
||||
// byte_array_print(data);
|
||||
uint8_t *raw_request;
|
||||
while ((raw_request = byte_array_get_bytes(data, 9)) != NULL) {
|
||||
//parse_request(handleArgs, request, data);
|
||||
query_or_insert_t *request = fix_message(raw_request);
|
||||
switch (request->insert.type) {
|
||||
case INSERT:
|
||||
printf("{%d} timestamp: %d, price: %d\n", handleArgs->connection, request->insert.timestamp, request->insert.price);
|
||||
// printf("{%d} timestamp: %d, price: %d\n", handleArgs->connection, request->insert.timestamp, request->insert.price);
|
||||
price_array_append(prices, &request->insert);
|
||||
break;
|
||||
case QUERY:
|
||||
@@ -56,8 +57,7 @@ void *handle_connection(void *args) {
|
||||
pthread_exit(NULL);
|
||||
exit(3);
|
||||
}
|
||||
free(request);
|
||||
free(raw_request);
|
||||
|
||||
}
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
}
|
||||
@@ -69,12 +69,12 @@ void *handle_connection(void *args) {
|
||||
|
||||
|
||||
|
||||
query_or_insert_t *fix_message(char *message) {
|
||||
query_or_insert_t *fix_message(const uint8_t *message) {
|
||||
if (message == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
query_or_insert_t *ret = malloc(sizeof(query_or_insert_t));
|
||||
ret->insert.type = message[0];
|
||||
ret->insert.type = (char)message[0];
|
||||
ret->query.mintime = message[4] | (message[3] << 8) | (message[2] << 16) | (message[1] << 24);
|
||||
ret->query.maxtime = message[8] | (message[7] << 8) | (message[6] << 16) | (message[5] << 24);
|
||||
return ret;
|
||||
@@ -98,23 +98,22 @@ void price_array_append(price_array_t *array, insert_t *value) {
|
||||
size_t new_size = array->size + 1;
|
||||
if (new_size > array->capacity) {
|
||||
array->capacity = array->capacity+1024;
|
||||
price_t *new_array = realloc(array->data, array->capacity);
|
||||
price_t *new_array = realloc(array->data, array->capacity * sizeof(price_t));
|
||||
array->data = new_array;
|
||||
if (array->data == NULL) {
|
||||
printf("Failed to allocate memory for array\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("array resized to %llu\n", array->capacity);
|
||||
}
|
||||
price_t *temp = malloc(sizeof(price_t));
|
||||
temp->timestamp = value->timestamp;
|
||||
temp->price = value->price;
|
||||
array->data[array->size] = *temp;
|
||||
array->data[array->size].timestamp = value->timestamp;
|
||||
array->data[array->size].price = value->price;
|
||||
array->size = new_size;
|
||||
};
|
||||
int price_array_query(price_array_t *array, query_t *query) {
|
||||
|
||||
int count = 0;
|
||||
int total = 0;
|
||||
long long total = 0;
|
||||
for (int i = 0; i < array->size; i++) {
|
||||
if (array->data[i].timestamp >= query->mintime && array->data[i].timestamp <= query->maxtime) {
|
||||
count++;
|
||||
|
||||
Reference in New Issue
Block a user