1
|
1 |
#include <assert.h>
|
9
|
2 |
#include <stdio.h>
|
1
|
3 |
#include "msg.h"
|
3
|
4 |
#include "main.h"
|
1
|
5 |
|
8
|
6 |
static char *client_command = 0;
|
9
|
7 |
static void c_end_of_job();
|
8
|
8 |
|
3
|
9 |
void c_new_job(const char *command)
|
|
10 |
{
|
|
11 |
struct msg m;
|
|
12 |
int res;
|
|
13 |
|
|
14 |
m.type = NEWJOB;
|
|
15 |
|
8
|
16 |
/* global */
|
|
17 |
client_command = command;
|
|
18 |
|
3
|
19 |
strcpy(m.u.command, command);
|
|
20 |
|
9
|
21 |
res = send(server_socket, &m, sizeof(m), 0);
|
3
|
22 |
if(res == -1)
|
9
|
23 |
{
|
|
24 |
perror("c: send");
|
|
25 |
exit(-1);
|
|
26 |
}
|
3
|
27 |
}
|
|
28 |
|
|
29 |
void c_wait_server_commands()
|
|
30 |
{
|
|
31 |
struct msg m;
|
|
32 |
int res;
|
|
33 |
|
|
34 |
while (1)
|
|
35 |
{
|
9
|
36 |
res = recv(server_socket, &m, sizeof(m), 0);
|
3
|
37 |
if(res == -1)
|
9
|
38 |
{
|
3
|
39 |
perror("read");
|
9
|
40 |
exit(-1);
|
|
41 |
}
|
3
|
42 |
|
|
43 |
if (res == 0)
|
|
44 |
break;
|
9
|
45 |
if (res != sizeof(m))
|
|
46 |
{
|
|
47 |
fprintf(stderr, "c: recv() message size wrong: %i instead of %i\n",
|
|
48 |
res, (int) sizeof(m));
|
|
49 |
}
|
3
|
50 |
assert(res == sizeof(m));
|
4
|
51 |
msgdump(&m);
|
3
|
52 |
if (m.type == NEWJOB_OK)
|
|
53 |
;
|
8
|
54 |
if (m.type == RUNJOB)
|
5
|
55 |
{
|
8
|
56 |
run_job(client_command);
|
9
|
57 |
c_end_of_job();
|
8
|
58 |
break;
|
5
|
59 |
}
|
|
60 |
}
|
8
|
61 |
close(server_socket);
|
5
|
62 |
}
|
|
63 |
|
|
64 |
void c_wait_server_lines()
|
|
65 |
{
|
|
66 |
struct msg m;
|
|
67 |
int res;
|
|
68 |
|
|
69 |
while (1)
|
|
70 |
{
|
9
|
71 |
res = recv(server_socket, &m, sizeof(m),0);
|
5
|
72 |
if(res == -1)
|
|
73 |
perror("read");
|
|
74 |
|
|
75 |
if (res == 0)
|
|
76 |
break;
|
|
77 |
assert(res == sizeof(m));
|
|
78 |
msgdump(&m);
|
|
79 |
if (m.type == LIST_LINE)
|
|
80 |
{
|
|
81 |
printf("%s", m.u.line);
|
|
82 |
}
|
3
|
83 |
}
|
|
84 |
}
|
|
85 |
|
8
|
86 |
void c_list_jobs()
|
3
|
87 |
{
|
|
88 |
struct msg m;
|
|
89 |
int res;
|
|
90 |
|
|
91 |
m.type = LIST;
|
|
92 |
|
9
|
93 |
res = send(server_socket, &m, sizeof(m), 0);
|
3
|
94 |
if(res == -1)
|
9
|
95 |
perror("send");
|
3
|
96 |
}
|
|
97 |
|
9
|
98 |
static void c_end_of_job()
|
8
|
99 |
{
|
|
100 |
struct msg m;
|
|
101 |
int res;
|
|
102 |
|
|
103 |
m.type = ENDJOB;
|
|
104 |
|
9
|
105 |
res = send(server_socket, &m, sizeof(m),0);
|
8
|
106 |
if(res == -1)
|
9
|
107 |
perror("send");
|
8
|
108 |
}
|
|
109 |
|
3
|
110 |
int c_shutdown_server()
|
1
|
111 |
{
|
2
|
112 |
struct msg m;
|
|
113 |
int res;
|
1
|
114 |
|
2
|
115 |
m.type = KILL;
|
9
|
116 |
res = send(server_socket, &m, sizeof(m), 0);
|
2
|
117 |
assert(res != -1);
|
1
|
118 |
}
|