-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
222 lines (199 loc) · 5.89 KB
/
server.c
File metadata and controls
222 lines (199 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <hiredis.h>
char *hostname = "127.0.0.1";
int port = 6379;
struct timeval timeout = {1, 500000};
void *socketThread(void *arg)
{
char client_message[2000];
char timestampBuffer[50];
char redisCommandText[100];
int newSocket = *((int *)arg);
free(arg);
int bytesReceived = recv(newSocket, client_message, sizeof(client_message) - 1, 0);
if (bytesReceived == 0)
{
printf("zero bytes read\n");
}
if (bytesReceived == -1)
{
printf("read error %d\n", errno);
}
client_message[bytesReceived] = 0;
// get high resolution timestamp
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
unsigned long usNow = (unsigned long)(ts.tv_sec * 1000000L + ts.tv_nsec / 1000L);
sprintf(timestampBuffer, "%lu", usNow);
redisContext *c = redisConnectWithTimeout(hostname, port, timeout);
if (c == NULL || c->err)
{
if (c)
{
printf("Redis Connection error: %s\n", c->errstr);
redisFree(c);
}
else
{
printf("Redis Connection error: can't allocate redis context\n");
}
exit(1);
}
char *messageBody = strstr(client_message, "HELLO");
if (messageBody == NULL)
{
printf("no HELLO in message %s\n", client_message ? client_message : "");
exit(1);
}
char *messageValue = strstr(client_message, "MSGNO");
if (messageValue == NULL)
{
printf("no MSGNO in message %s\n", client_message ? client_message : "");
exit(1);
}
messageValue[-1] = '\0'; // MSGNO comes after the HELLO in the message text.
redisReply *reply;
if (strstr(client_message, "LOGON") != NULL)
{
// check for existing record (message 1 has no predecessor), Save body to redis
if (strcmp(messageValue, "MSGNO1") != 0)
{
sprintf(redisCommandText, "GET %s", messageBody);
reply = redisCommand(c, "GET %s", messageBody);
if (reply->type != REDIS_REPLY_NIL)
{
printf("LOGON missing logoff %lu %s %s %s\n", usNow, reply->str, redisCommandText, messageBody);
}
printf("LOGON GET %s: %s\n", messageBody, reply->str);
freeReplyObject(reply);
}
sprintf(redisCommandText, "SET %s %s", messageBody, messageValue);
reply = redisCommand(c, redisCommandText);
printf("LOGON SET %s: %s\n", messageBody, reply->str);
freeReplyObject(reply);
}
if (strstr(client_message, "LOGOFF") != NULL)
{
// delete message body from redis
sprintf(redisCommandText, "GET %s", messageBody);
reply = redisCommand(c, redisCommandText);
if (reply->type == REDIS_REPLY_NIL)
{
printf("LOGOFF record not found %lu %s %s\n", usNow, messageBody, redisCommandText);
}
printf("LOGOFF GET %s: %s\n", messageBody, reply->str);
freeReplyObject(reply);
sprintf(redisCommandText, "DEL %s", messageBody);
reply = redisCommand(c, redisCommandText);
printf("LOGOFF DEL %s: %s\n", messageBody, reply->str);
freeReplyObject(reply);
}
else
{
sprintf(redisCommandText, "GET %s", messageBody);
reply = redisCommand(c, redisCommandText);
if (reply->type == REDIS_REPLY_NIL)
{
printf("not logged on %lu %s\n", usNow, messageBody);
}
printf("%s: %s\n", redisCommandText, reply->str);
freeReplyObject(reply);
sprintf(redisCommandText, "SET %s %s", messageBody, messageValue);
reply = redisCommand(c, redisCommandText);
printf("%s: %s\n", redisCommandText, reply->str);
freeReplyObject(reply);
}
// Send message to the client socket
char *messagePrefix = "Hello Client : ";
char *message = malloc(sizeof(client_message) + strlen(messagePrefix));
strcpy(message, messagePrefix);
strcat(message, client_message);
printf("%s\n", message);
int sendrc = send(newSocket, message, strlen(message), 0);
if (sendrc == -1)
{
printf("send error %d\n", errno);
}
free(message);
redisFree(c);
int closerc = close(newSocket);
if (closerc == -1)
{
printf("read error %d\n", errno);
}
pthread_exit(NULL);
}
int main()
{
int serverSocket;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
// Create the socket.
serverSocket = socket(PF_INET, SOCK_STREAM, 0);
// Configure settings of the server address struct
// Address family = Internet
serverAddr.sin_family = AF_INET;
// Set port number, using htons function to use proper byte order
serverAddr.sin_port = htons(7799);
// Set IP address to localhost
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
// Set all bits of the padding field to 0
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
// Bind the address struct to the socket
bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
// Listen on the socket, with 40 max connection requests queued
if (listen(serverSocket, 400) == 0)
{
printf("Listening\n");
}
else
{
printf("Error %d\n", errno);
}
pthread_t tid[410];
int i = 0;
while (1)
{
// Accept call creates a new socket for the incoming connection
addr_size = sizeof serverStorage;
int newSocket = accept(serverSocket, (struct sockaddr *)&serverStorage, &addr_size);
if (newSocket == -1)
{
printf("socket error %d\n", errno);
abort();
}
int *sockPointer = malloc(sizeof(newSocket));
*sockPointer = newSocket;
int pthcErrno = pthread_create(&tid[i++], NULL, socketThread, sockPointer);
if (pthcErrno != 0)
{
printf("Failed to create thread %d\n", pthcErrno);
}
if (i >= 400)
{
i = 0;
while (i < 400)
{
int errorNumber = pthread_join(tid[i++], NULL);
if (errorNumber != 0)
{
printf("pthread_join error %d\n", errorNumber);
}
}
i = 0;
}
}
return 0;
}