-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliente.c
288 lines (246 loc) · 5.72 KB
/
cliente.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <netinet/in.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#define PROCESS_MAX 20
#define COLS 2
#define PORT 8080
// Message structure
struct Message
{
int socket;
char *message;
};
// Stop structure
struct Stop
{
int stopC;
};
struct Stop stopServer;
int sock = 0;
// Reads keyboard
int getChar()
{
int pressedK;
// struct with terminal information
struct termios staryTermios, novyTermios;
int actualF, futureF;
novyTermios = staryTermios;
// Search for IO signals
novyTermios.c_lflag &= ~(ICANON);
// System call with flags
actualF = fcntl(STDIN_FILENO, F_GETFL);
futureF = actualF;
// Non-blocking function
futureF |= O_NONBLOCK;
// System call with flags
fcntl(STDIN_FILENO, F_SETFL, futureF);
// If a keyboard is pressed
pressedK = getchar();
// Client exits
if (pressedK == 'q')
{
stopServer.stopC = 1;
close(sock);
return 1;
}
return 0;
}
// Concat char to string
char *ConcatCharToCharArray(char *Str, char Chr)
{
int len = strlen(Str);
char *StrResult = malloc(len + 2);
while (stopServer.stopC != 1 & getChar() != 1)
{
strcpy(StrResult, Str);
StrResult[len] = Chr;
StrResult[len + 1] = '\0';
break;
}
return StrResult;
}
// Convert int into String
char *numberToString(int number)
{
char *string = (char *)malloc(sizeof(char));
while (stopServer.stopC != 1 & getChar() != 1)
{
sprintf(string, "%d", number);
break;
}
return string;
}
// Send to server and wait reply
void *sendProcessSocket(void *msg)
{
sleep(2);
while (stopServer.stopC != 1 & getChar() != 1)
{
struct Message *my_msg = (struct Message *)msg;
int valread;
char buffer[2000] = {};
char *sLength = numberToString(strlen(my_msg->message));
my_msg->message = ConcatCharToCharArray(my_msg->message, ',');
strcat(my_msg->message, sLength);
send(my_msg->socket, my_msg->message, strlen(my_msg->message), 0);
valread = read(my_msg->socket, buffer, 2000);
puts(buffer);
break;
}
}
// Make thread by send data to server
void sendProcess(int burst, int priority, int socket)
{
while (stopServer.stopC != 1 & getChar() != 1)
{
pthread_t thrd;
char *msgOut = numberToString(burst);
char *numString = numberToString(priority);
msgOut = ConcatCharToCharArray(msgOut, ',');
msgOut = strcat(msgOut, numString);
struct Message *msg = malloc(sizeof(struct Message));
msg->message = msgOut;
msg->socket = socket;
// Created thread and function call
pthread_create(&thrd, NULL, sendProcessSocket, (void *)msg);
pthread_join(thrd, NULL);
break;
}
}
// Make random data by socket to server
void randProcess(int socket, int minTime, int maxTime, int minBurst, int maxBurst)
{
while (stopServer.stopC != 1 & getChar() != 1)
{
int burst = rand() % (maxBurst + 1 - minBurst) + minBurst;
int priority = rand() % (5 + 1 - 1) + 1;
sendProcess(burst, priority, socket);
int time = rand() % (maxTime + 1 - minTime) + minTime;
sleep(time);
break;
}
}
// Placeholder function for file reading and random gen tests
int fileRead(int socket, char *txtName)
{
while (stopServer.stopC != 1 & getChar() != 1)
{
srand(time(NULL));
int burst, priority, time;
FILE *reader = fopen(txtName, "r");
// If file does not exist
if (reader == NULL)
{
return 0;
}
// checks file for each line
while (fscanf(reader, "%d %d", &burst, &priority) == 2)
{
time = rand() % (8 + 1 - 3) + 3;
// Send data by socket to server
sendProcess(burst, priority, socket);
sleep(time);
// Each time should have a certain amount of sleep
// Here is where you send the data through the sockets on individual threads
}
fclose(reader);
break;
}
return 1;
}
// If CPU creates random process
void *autoCPU(int socket)
{
int minTime, maxTime, minBurst, maxBurst;
printf("Input MIN burst range for random when creating process: \n");
scanf("%d", &minBurst);
printf("Input MAX burst range for random when creating process: \n");
scanf("%d", &maxBurst);
printf("Input MIN sleep time for random when creating process: \n");
scanf("%d", &minTime);
printf("Input MAX sleep time for random when creating process: \n");
scanf("%d", &maxTime);
while (getChar() != 1 & stopServer.stopC != 1)
{
randProcess(socket, minTime, maxTime, minBurst, maxBurst);
}
}
// If CPU reads process from file
void *manualCPU(int socked)
{
char archive[100];
int algorit;
int q;
printf("Write file name: \n");
scanf("%100s", archive);
fileRead(socked, archive);
}
// main menu
void *mainMenu(int socket)
{
int choice;
printf("Main Menu");
printf("\n\t----------------------");
printf("\n 1. Automatic CPU");
printf("\n 2. Run manual CPU");
printf("\n 3. Exit");
printf("\n Enter your choice \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
autoCPU(socket);
break;
case 2:
manualCPU(socket);
break;
case 3:
stopServer.stopC = 1;
printf("Client has closed the program\n");
close(sock);
exit(0);
break;
default:
printf("Invalid choice!\n");
break;
}
}
int main()
{
char buffer[2000] = {};
int valread;
struct sockaddr_in serv_addr;
char *msg = "Client sends";
// SOCKET CREATION
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket can not be create\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert adress IP
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address\n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed\n");
return -1;
}
mainMenu(sock);
printf("\nClient simulation finished\n");
close(sock);
return 0;
}