Base Construct works
[hochregal.git] / src / main.c
blob17fcafb06a5fa01c3f02fe10f5c4e02af3db6428
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <netinet/in.h>
6 #include <arpa/inet.h>
7 #include <unistd.h>
8 #include <pthread.h>
9 #include <unistd.h>
10 #include <stdbool.h>
16 /***************SERVER****************/
18 void *Parser(void*);
19 void *Receive(void *);
20 void *Send(void *);
21 void *Timer_X(void *);
22 void *Timer_Y(void *);
23 int CreateSocket(int);
25 pthread_mutex_t simu_mutex;
26 pthread_cond_t sensor_trigger;
28 #pragma pack (1)
29 struct commands
31 unsigned int xon :1;
32 unsigned int xdir :1;
33 unsigned int yon :1;
34 unsigned int ydir :1;
35 unsigned int zon :1;
36 unsigned int zdir :1;
37 unsigned int grep :1;
39 #pragma pack()
43 #pragma pack (2)
44 struct sensors
46 unsigned int x :4;
47 unsigned int y :4;
48 unsigned int z :2;
50 #pragma pack()
52 struct commands my_commands;
53 struct sensors my_sensors;
56 //pthread_attr_t attr;
58 int main(int argc, char *arcv[])
60 pthread_t parser, send, receive;
61 int server_socket, new_socket, rc;
63 my_sensors.x=1;
64 my_sensors.y=1;
65 my_sensors.z=1;
67 pthread_mutex_init(&simu_mutex, NULL);
68 pthread_cond_init (&sensor_trigger, NULL);
70 new_socket = CreateSocket(server_socket);
72 // pthread_attr_init(&attr);
73 // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
76 rc = pthread_create(&receive, NULL, Receive, (void *) new_socket);
77 if (rc){
78 printf("ERROR; return code from pthread_create() receive is %d\n", rc);
81 rc = pthread_create(&send, NULL, Send, (void *) new_socket);
82 if (rc){
83 printf("ERROR; return code from pthread_create() send is %d\n", rc);
86 rc = pthread_create(&parser, &attr, Parser, NULL);
87 if (rc){
88 printf("ERROR; return code from pthread_create() parser is %d\n", rc);
93 rc = pthread_create(&timer_y, &attr, Timer_Y, NULL);
94 if (rc){
95 printf("ERROR; return code from pthread_create() timer_y is %d\n", rc);
98 //pthread_join(send, NULL);
99 //pthread_join(receive, NULL);
101 //pthread_join(parser, NULL);
103 //pthread_join(timer_y, NULL);
105 while(1)
110 printf("\n Server: close()...");
112 close(server_socket);
113 close(new_socket);
115 printf("\n Serverprogramm beendet\n\n");
118 // pthread_attr_destroy(&attr);
119 pthread_mutex_destroy(&simu_mutex);
120 pthread_cond_destroy(&sensor_trigger);
121 pthread_exit(NULL);
122 return(0);
125 int CreateSocket(int server_socket)
127 int new_socket;
128 int length;
129 struct sockaddr_in serverinfo, clientinfo;
130 unsigned short int portnmbr=5000;
131 char ip_addr[]="127.0.0.1";
133 printf("\n Server: socket()...");
135 server_socket = socket(AF_INET, SOCK_STREAM, 0);
136 serverinfo.sin_family = AF_INET;
137 serverinfo.sin_addr.s_addr = inet_addr(ip_addr);
138 serverinfo.sin_port = htons(portnmbr);
139 length = sizeof(serverinfo);
141 printf("\n Server: bind()...");
142 bind(server_socket, (struct sockaddr *)&serverinfo, length);
143 printf("\n Server: listen()...");
144 printf("\n Server mit IP %s", ip_addr);
145 printf(" an Port %d wartet...", portnmbr);
147 listen(server_socket, 3);
148 printf("\n Server: accept()...");
149 new_socket = accept(server_socket,
150 (struct sockaddr *)&clientinfo, &length);
152 printf("Verbindung mit %s", inet_ntoa(clientinfo.sin_addr));
154 return new_socket;
157 void *Parser(void* arg)
159 char input[10];
160 printf("\n<Parser> started");
161 while(1)
163 printf("\n~>");
164 scanf("%10s", input);
165 fflush(stdin);
166 if(strstr(input, "test"))
167 printf("\nTesteingabe");
170 pthread_exit(NULL);
173 void *Receive(void *new_socket)
175 pthread_t timer_x, timer_y;
176 int socket, rc;
177 socket=(int) new_socket;
179 printf("\n<Receive> started");
181 while(1)
183 int test;
184 //pthread_mutex_lock(&simu_mutex);
185 rc=recv(socket, (struct commands *)&my_commands, sizeof(my_commands),0);
187 printf("\n%d Bytes empfangen <new commands>", rc);
189 if(my_commands.xon)
191 printf("\nxon");
192 fflush(stdout);
193 rc = pthread_create(&timer_x, NULL, Timer_X, NULL);
194 if (rc)
195 printf("\nERROR; return code from pthread_create() timer_x is %d\n", rc);
196 //pthread_join(timer_x, NULL);
198 // my_commands.xon=1;
199 printf("\nxon %d", my_commands.xon);
200 printf(" xdir %d", my_commands.xdir);
201 printf(" yon %d", my_commands.yon);
202 printf(" ydir %d", my_commands.ydir);
203 printf(" zon %d", my_commands.zon);
204 printf(" zdir %d", my_commands.zdir);
205 printf(" grep %d", my_commands.grep);
207 fflush(stdout);
208 //pthread_mutex_unlock(&simu_mutex);
211 pthread_exit(NULL);
214 void *Send(void *new_socket)
216 int socket;
217 socket=(int) new_socket;
218 int len;
220 printf("\n<Send> started");
222 while(1)
224 pthread_mutex_lock(&simu_mutex);
225 pthread_cond_wait(&sensor_trigger, &simu_mutex);
226 printf("\n<Send> received Signal");
227 len=send(socket, (struct sensors*)&my_sensors, sizeof(my_sensors),0);
228 printf("\nsent %d bytes",len);
229 fflush(stdout);
230 pthread_mutex_unlock(&simu_mutex);
232 pthread_exit(NULL);
236 void *Timer_X(void * arg)
238 int i, sensor;
240 printf("\n<TimerX> started");
241 fflush(stdout);
242 while(1)
244 usleep(1000000);
245 if( my_sensors.x>0 && my_sensors.x<11)
247 if(my_commands.xdir)
248 my_sensors.x++;
249 else
250 my_sensors.x--;
252 else
253 printf("[ERROR] *****MACHINE OUT OF X-RANGE!!!*****");
255 pthread_mutex_lock(&simu_mutex);
256 pthread_cond_signal(&sensor_trigger);
257 pthread_mutex_unlock(&simu_mutex);
259 pthread_exit(NULL);
262 void *Timer_Y(void *arg)
264 int i;
266 printf("\n<TimerY> started");
268 while(1)
270 usleep(1000000);
271 if( my_sensors.y>0 && my_sensors.y<11)
273 if(my_commands.ydir)
274 my_sensors.y++;
275 else
276 my_sensors.y--;
278 else
279 printf("[ERROR] *****MACHINE OUT OF X-RANGE!!!*****");
281 pthread_mutex_lock(&simu_mutex);
282 pthread_cond_signal(&sensor_trigger);
283 pthread_mutex_unlock(&simu_mutex);
286 pthread_exit(NULL);