Skip to content

Commit f52c945

Browse files
committed
chore: formatting
1 parent a35aae9 commit f52c945

File tree

10 files changed

+223
-170
lines changed

10 files changed

+223
-170
lines changed

source/means-to-an-end/main.c

+23-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#define MAX_EVENTS 10
3232
#define PORT "18888"
3333

34-
int handle_request(struct queue *sdq, char* raw_req, size_t size)
34+
int handle_request(struct queue* sdq, char* raw_req, size_t size)
3535
{
3636
assert(sdq != NULL);
3737
assert(raw_req != NULL);
@@ -44,8 +44,8 @@ int handle_request(struct queue *sdq, char* raw_req, size_t size)
4444
bool mal = false;
4545
/*int r = is_prime_request_builder(sdq, raw_req, size, &mal);*/
4646
/*if (r <= 0) {*/
47-
/*log_warn("recv_and_handle: is_prime_request_builder returned '%d'", r);*/
48-
/*return -1;*/
47+
/*log_warn("recv_and_handle: is_prime_request_builder returned '%d'", r);*/
48+
/*return -1;*/
4949
/*}*/
5050

5151
return (mal ? 0 : 1);
@@ -90,7 +90,7 @@ int main()
9090
char *data, *sddata, *complete_req;
9191
int n, fd, res, size, sdsize, rs, result;
9292
struct epoll_ctl_info epci = {epollfd, 0, 0};
93-
struct queue* rcqu = NULL, *sdqu = NULL;
93+
struct queue *rcqu = NULL, *sdqu = NULL;
9494
queue_init(&rcqu, QUEUE_CAPACITY);
9595
queue_init(&sdqu, QUEUE_CAPACITY);
9696

@@ -116,7 +116,10 @@ int main()
116116

117117
// Receive all the data into the queue
118118
res = recv_request(fd, rcqu);
119-
log_trace("main epoll loop: handling POLLIN event on fd '%d' with res: '%d'", fd, res);
119+
log_trace(
120+
"main epoll loop: handling POLLIN event on fd '%d' with res: '%d'",
121+
fd,
122+
res);
120123

121124
// Handle error case while recv data
122125
if (res < -1) {
@@ -133,23 +136,33 @@ int main()
133136
complete_req = NULL;
134137
size = queue_peek(rcqu, &data);
135138
if (size > 0) {
136-
/*complete_req = (char *) memrchr(data, PRIME_REQUEST_DELIMITERS[0], size);*/
137-
log_trace("main epoll loop: complete_req = '%d'", (complete_req == NULL ? 0 : 1));
139+
/*complete_req = (char *) memrchr(data, PRIME_REQUEST_DELIMITERS[0],
140+
* size);*/
141+
log_trace("main epoll loop: complete_req = '%d'",
142+
(complete_req == NULL ? 0 : 1));
138143
}
139144

140145
// If we do, process it
141146
if (complete_req != NULL) {
142147
size = queue_pop_no_copy(rcqu, &data);
143-
log_trace("main epoll loop: raw request: fd: '%d', size: '%d', data: '%s'", fd, size, data);
148+
log_trace(
149+
"main epoll loop: raw request: fd: '%d', size: '%d', data: '%s'",
150+
fd,
151+
size,
152+
data);
144153
result = handle_request(sdqu, data, (size_t)size);
145154
sdsize = queue_pop_no_copy(sdqu, &sddata);
146155
rs = sendall(fd, sddata, &sdsize);
147156

148157
if ((result <= 0) || (rs != 0)) {
149158
if (result == 0)
150-
log_info("main epoll loop: there was a malformed respoonse. need to close socket");
159+
log_info(
160+
"main epoll loop: there was a malformed respoonse. need to "
161+
"close socket");
151162
else if (result < 0)
152-
log_info("main epoll loop: there was an error handling the request. need to close socket");
163+
log_info(
164+
"main epoll loop: there was an error handling the request. "
165+
"need to close socket");
153166
else if (rs != 0)
154167
log_error("main epoll loop:: failed during sendall function");
155168
if (fd_poll_del_and_close(&epci) == -1) {

source/means-to-an-end/messages.c

+23-24
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,36 @@
77
#include "means-to-an-end/prices.h"
88
#include "means-to-an-end/messages.h"
99

10-
void message_parse(struct queue* sdqu, char *data, size_t dsize) {
10+
void message_parse(struct queue* sdqu, char* data, size_t dsize)
11+
{
1112
// TODO: create and allocate struct prices
12-
char *pd = data;
13+
char* pd = data;
1314
int k;
1415
int num_msgs = dsize / MESSAGE_SIZE;
1516
for (k = 0; k < num_msgs; pd += MESSAGE_SIZE) {
1617
char type = *pd;
17-
uint32_t *first_word = (uint32_t *)(pd + MESSAGE_FIRST_WORD_OFFSET);
18-
uint32_t *second_word = (uint32_t *)(pd + MESSAGE_SECOND_WORD_OFFSET);
18+
uint32_t* first_word = (uint32_t*)(pd + MESSAGE_FIRST_WORD_OFFSET);
19+
uint32_t* second_word = (uint32_t*)(pd + MESSAGE_SECOND_WORD_OFFSET);
1920

2021
switch (type) {
21-
case MESSAGE_QUERY:
22-
{
23-
struct price_query qry;
24-
qry.mintime = ntohl(*first_word);
25-
qry.maxtime = ntohl(*second_word);
26-
// Process query...
27-
break;
28-
}
29-
case MESSAGE_INSERT:
30-
{
31-
struct price prc;
32-
prc.timestamp = ntohl(*first_word);
33-
prc.price = ntohl(*second_word);
34-
// Insert price directly into vector without additional memcpy
35-
/*if (p->size == p->capacity) {*/
36-
// Resize logic here
37-
/*}*/
38-
/*p->data[p->size++] = prc;*/
39-
break;
40-
}
22+
case MESSAGE_QUERY: {
23+
struct price_query qry;
24+
qry.mintime = ntohl(*first_word);
25+
qry.maxtime = ntohl(*second_word);
26+
// Process query...
27+
break;
28+
}
29+
case MESSAGE_INSERT: {
30+
struct price prc;
31+
prc.timestamp = ntohl(*first_word);
32+
prc.price = ntohl(*second_word);
33+
// Insert price directly into vector without additional memcpy
34+
/*if (p->size == p->capacity) {*/
35+
// Resize logic here
36+
/*}*/
37+
/*p->data[p->size++] = prc;*/
38+
break;
39+
}
4140
default:
4241
// Handle unknown message type
4342
break;

source/means-to-an-end/messages.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
#define MESSAGE_SIZE 9
66
#define MESSAGE_INSERT 'I'
77
#define MESSAGE_QUERY 'Q'
8-
#define MESSAGE_FIRST_WORD_OFFSET 1 // These are in network byte order
9-
#define MESSAGE_SECOND_WORD_OFFSET 5 // These are in network byte order
8+
#define MESSAGE_FIRST_WORD_OFFSET 1 // These are in network byte order
9+
#define MESSAGE_SECOND_WORD_OFFSET 5 // These are in network byte order
1010

11-
void message_parse(struct queue* sdqu, char *data, size_t dsize);
11+
void message_parse(struct queue* sdqu, char* data, size_t dsize);
1212

1313
#endif // INCLUDE_MESSAGES_H_
14-

source/means-to-an-end/prices.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77

88
#include "means-to-an-end/prices.h"
99

10-
void prices_init_data(struct prices *ps, size_t capacity) {
10+
void prices_init_data(struct prices* ps, size_t capacity)
11+
{
1112
assert(ps != NULL);
1213
assert(capacity > 0);
1314
assert(capacity > ps->capacity);
1415

15-
struct price *new_data = NULL;
16+
struct price* new_data = NULL;
1617
new_data = reallocarray(ps->data, sizeof(struct price), capacity);
1718
assert(new_data != NULL);
1819

1920
ps->data = new_data;
2021
ps->capacity = capacity;
2122
}
2223

23-
void prices_init(struct prices **pps, size_t capacity) {
24+
void prices_init(struct prices** pps, size_t capacity)
25+
{
2426
assert(capacity > 0);
2527

2628
*pps = malloc(sizeof(struct prices));
@@ -32,7 +34,8 @@ void prices_init(struct prices **pps, size_t capacity) {
3234
(*pps)->size = 0;
3335
}
3436

35-
void prices_free(struct prices **pps) {
37+
void prices_free(struct prices** pps)
38+
{
3639
assert(*pps != NULL);
3740
assert((*pps)->data != NULL);
3841

@@ -45,7 +48,8 @@ void prices_free(struct prices **pps) {
4548
// - Check prices with the same timestamp
4649
// - Don't add new prices on timestamp conflict
4750
// - After push, sort, to keep the array sorted
48-
void prices_push(struct prices *ps, struct price* data) {
51+
void prices_push(struct prices* ps, struct price* data)
52+
{
4953
assert(ps != NULL);
5054
assert(ps->data != NULL);
5155
assert(data != NULL);
@@ -57,7 +61,7 @@ void prices_push(struct prices *ps, struct price* data) {
5761
ps->data[ps->size++] = *data;
5862
}
5963

60-
bool prices_duplicate_timestamp_check(struct prices *ps, int32_t timestamp)
64+
bool prices_duplicate_timestamp_check(struct prices* ps, int32_t timestamp)
6165
{
6266
assert(ps != NULL);
6367
assert(ps->data != NULL);
@@ -96,7 +100,6 @@ void prices_binary_sort(struct prices* ps)
96100
}
97101
}
98102

99-
/*If there are no samples within the requested period, or if mintime comes after maxtime, the value returned must be 0.*/
100-
int32_t prices_query(struct prices *ps, struct price_query *pq) {
101-
102-
}
103+
/*If there are no samples within the requested period, or if mintime comes after
104+
* maxtime, the value returned must be 0.*/
105+
int32_t prices_query(struct prices* ps, struct price_query* pq) {}

source/means-to-an-end/prices.h

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@ extern "C" {
66
#endif
77

88
struct price {
9-
int32_t timestamp; // In epoch
9+
int32_t timestamp; // In epoch
1010
int32_t price;
1111
};
1212

1313
struct price_query {
14-
int32_t mintime; // In epoch
14+
int32_t mintime; // In epoch
1515
int32_t maxtime;
1616
};
1717

1818
// Array structure
1919
struct prices {
20-
struct price *data;
21-
size_t size; // Number of struct price we are holding
20+
struct price* data;
21+
size_t size; // Number of struct price we are holding
2222
size_t capacity; // Not bytes, but number struct price we can hold
2323
};
2424

25-
void prices_init(struct prices **pps, size_t capacity);
26-
void prices_init_data(struct prices *ps, size_t capacity);
27-
void prices_free(struct prices **pps);
25+
void prices_init(struct prices** pps, size_t capacity);
26+
void prices_init_data(struct prices* ps, size_t capacity);
27+
void prices_free(struct prices** pps);
2828
// TODO: On push
2929
// - Check prices with the same timestamp
3030
// - Don't add new prices on timestamp conflict
3131
// - After push, sort, to keep the array sorted
32-
void prices_push(struct prices *ps, struct price* data);
32+
void prices_push(struct prices* ps, struct price* data);
3333
// Get pointer to the data
34-
void prices_binary_sort(struct prices *ps);
35-
bool prices_duplicate_timestamp_check(struct prices *ps, int32_t timestamp);
36-
/*If there are no samples within the requested period, or if mintime comes after maxtime, the value returned must be 0.*/
37-
int32_t prices_query(struct prices *ps, struct price_query *pq);
38-
34+
void prices_binary_sort(struct prices* ps);
35+
bool prices_duplicate_timestamp_check(struct prices* ps, int32_t timestamp);
36+
/*If there are no samples within the requested period, or if mintime comes after
37+
* maxtime, the value returned must be 0.*/
38+
int32_t prices_query(struct prices* ps, struct price_query* pq);
3939

4040
#ifdef __cplusplus
4141
}

source/prime-time/is-prime-request.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
#include "prime-time/is-prime-request.h"
2727

2828
/// Return the number of requests processed
29-
int is_prime_request_builder(struct queue *sdq,
29+
int is_prime_request_builder(struct queue* sdq,
3030
char* raw_request,
31-
size_t req_size, bool *malformed)
31+
size_t req_size,
32+
bool* malformed)
3233
{
3334
assert(sdq != NULL);
3435
assert(malformed != NULL);
@@ -45,13 +46,13 @@ int is_prime_request_builder(struct queue *sdq,
4546
if (token == NULL)
4647
break;
4748
// We have exceeded the request size
48-
if ((token - raw_request) >= (long) req_size)
49+
if ((token - raw_request) >= (long)req_size)
4950
break;
5051

5152
*malformed = is_prime_request_malformed(&curr, token);
5253
curr.is_prime = is_prime_f(curr.number);
5354
is_prime_beget_response(&curr, sdq->head, &size);
54-
queue_push_ex(sdq, (size_t) size);
55+
queue_push_ex(sdq, (size_t)size);
5556

5657
// Stop handling requests for this socket as soon as we
5758
// find a malformed request
@@ -72,7 +73,7 @@ void is_prime_init(struct is_prime_request** request)
7273
(*request)->number = 0;
7374
}
7475

75-
bool is_prime_request_malformed(struct is_prime_request *request, char* req)
76+
bool is_prime_request_malformed(struct is_prime_request* request, char* req)
7677
{
7778
assert(request != NULL);
7879
assert(req != NULL);
@@ -99,8 +100,7 @@ bool is_prime_request_malformed(struct is_prime_request *request, char* req)
99100

100101
if (json_object_get_type(method) != json_type_string) {
101102
json_object_put(root);
102-
log_warn(
103-
"is_prime_request_malformed: method is not of type string");
103+
log_warn("is_prime_request_malformed: method is not of type string");
104104
request->is_malformed = true;
105105
return true;
106106
}
@@ -149,8 +149,7 @@ bool is_prime_request_malformed(struct is_prime_request *request, char* req)
149149

150150
if (num_type != json_type_int) {
151151
json_object_put(root);
152-
log_warn(
153-
"is_prime_request_malformed: number is not of type int");
152+
log_warn("is_prime_request_malformed: number is not of type int");
154153
request->is_malformed = true;
155154
return true;
156155
}
@@ -159,9 +158,8 @@ bool is_prime_request_malformed(struct is_prime_request *request, char* req)
159158
int number_value = json_object_get_int(number);
160159
if (errno != 0) {
161160
json_object_put(root);
162-
log_warn(
163-
"is_prime_request_malformed: json_object_get_int failed for '%s'",
164-
number);
161+
log_warn("is_prime_request_malformed: json_object_get_int failed for '%s'",
162+
number);
165163
request->is_malformed = true;
166164
return true;
167165
}
@@ -185,21 +183,22 @@ bool is_prime_f(int number)
185183
return true;
186184
}
187185

188-
void is_prime_beget_response(struct is_prime_request* request, char *response, int *size)
186+
void is_prime_beget_response(struct is_prime_request* request,
187+
char* response,
188+
int* size)
189189
{
190190
assert(request != NULL);
191191
assert(response != NULL);
192192
assert(size != NULL);
193193

194194
if (request->is_malformed) {
195195
*size = PRIME_RESPONSE_ILL_RESPONSE_SIZE;
196-
memcpy(response, PRIME_RESPONSE_ILL_RESPONSE, (size_t) *size);
196+
memcpy(response, PRIME_RESPONSE_ILL_RESPONSE, (size_t)*size);
197197
log_trace("is_prime_beget_response: '%s'", response);
198198
return;
199199
}
200-
*size = sprintf(response,
201-
PRIME_RESPONSE_FORMAT,
202-
(request->is_prime ? "true" : "false"));
200+
*size = sprintf(
201+
response, PRIME_RESPONSE_FORMAT, (request->is_prime ? "true" : "false"));
203202
log_trace("is_prime_beget_response: '%s'", response);
204203
}
205204

source/prime-time/is-prime-request.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ struct is_prime_request {
2323
int number;
2424
};
2525

26-
int is_prime_request_builder(struct queue *sdq,
26+
int is_prime_request_builder(struct queue* sdq,
2727
char* raw_request,
28-
size_t req_size, bool *malformed);
29-
bool is_prime_request_malformed(struct is_prime_request *request, char* req);
28+
size_t req_size,
29+
bool* malformed);
30+
bool is_prime_request_malformed(struct is_prime_request* request, char* req);
3031
bool is_prime_f(int number);
31-
void is_prime_beget_response(struct is_prime_request* request, char *response, int *size);
32+
void is_prime_beget_response(struct is_prime_request* request,
33+
char* response,
34+
int* size);
3235
void is_prime_init(struct is_prime_request** request);
3336
void is_prime_free(struct is_prime_request** request);
3437

0 commit comments

Comments
 (0)