-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverif.cpp
159 lines (154 loc) · 3.73 KB
/
serverif.cpp
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <math.h>
#include "includes/serverif.h"
#include "includes/robotconnector.h"
#include "includes/lasersensor.h"
void serverconnect(componentservertype *s)
{
char buf[256];
int len;
s->serv_adr.sin_family = AF_INET;
s->serv_adr.sin_port = htons(s->port);
s->serv_adr.sin_addr.s_addr = inet_addr(s->host);
printf("port %d host %s \n", s->port, s->host);
if ((s->connected = (connect(s->sockfd, (struct sockaddr *) &s->serv_adr, sizeof(s->serv_adr))) > -1))
{
printf(" connected to %s \n", s->name);
len = sprintf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
send(s->sockfd, buf, len, 0);
len = sprintf(buf, "mrc version=\"1.00\" >\n");
send(s->sockfd, buf, len, 0);
if (fcntl(s->sockfd, F_SETFL, O_NONBLOCK) == -1)
{
fprintf(stderr, "startserver: Unable to set flag O_NONBLOCK on %s fd \n", s->name);
}
}
else
{
printf("Not connected to %s %d \n", s->name, s->connected);
}
}
void xml_proc(struct xml_in *x)
{
while (1)
{
switch (xml_in_nibble(x)) {
case XML_IN_NONE:
return;
case XML_IN_TAG_START:
#if (0)
{
int i;
printf("start tag: %s, %d attributes\n", x->a, x->n);
for (i = 0; i < x->n; i++)
{
printf(" %s %s \n", x->attr[i].name, x->attr[i].value);
}
}
#endif
if (strcmp("gmk", x->a) == 0)
{
printf(" %s %s \n", x->attr[0].name, x->attr[0].value);
double a;
if (getdouble(&a, "id", x))
{
gmk.id = a;
printf("id= %f\n", gmk.id);
}
if (getdouble(&a, "crcOK", x))
{
gmk.crc = a;
printf("crc= %f\n", gmk.crc);
}
}
else if (strcmp("pos3d", x->a) == 0)
{
getdouble(&gmk.x, "x", x);
getdouble(&gmk.y, "y", x);
getdouble(&gmk.z, "z", x);
}
else if (strcmp("rot3d", x->a) == 0)
{
getdouble(&gmk.omega, "Omega", x);
getdouble(&gmk.phi, "Phi", x);
getdouble(&gmk.kappa, "Kappa", x);
}
else if (strcmp("vision", x->a) == 0)
{
for (int i = 0; i < x->n; i++)
{
const int ix = atoi(x->attr[i].name + 3);
if (ix > -1 && ix < 10)
{
visionpar[ix] = atof(x->attr[i].value);
}
}
}
break;
case XML_IN_TAG_END:
//printf("end tag: %s\n", x->a);
break;
case XML_IN_TEXT:
//printf("text: %d bytes\n \"", x->n);
//fwrite(x->a, 1, x->n, stdout);
//printf("\"\n");
break;
}
}
}
void xml_proca(struct xml_in *x)
{
while (1)
{
switch (xml_in_nibble(x)) {
case XML_IN_NONE:
return;
case XML_IN_TAG_START:
#if (0)
{
printf("start tag: %s, %d attributes\n", x->a, x->n);
for (int i = 0; i < x->n; i++)
{
printf(" %s %s \n", x->attr[i].name, x->attr[i].value);
}
}
#endif
//check if xml name is the correct one
if (strcmp("lval", x->a) == 0)
{
const int ANGLE_INDEX = 1;
const int DISTANCE_INDEX = 2;
//double check if the xml is correct by checking if one element has
//the correct name
if (strcmp(x->attr[DISTANCE_INDEX].name, "dist") == 0)
{
const double angle = atof(x->attr[ANGLE_INDEX].value);
//calculate where the index the laser value should be saved at by converting the angle to an index
const int laserIndex = (int) round(ANGLE_TO_INDEX(angle));
//printf("%d\n", laserIndex);
laserpar[laserIndex] = atof(x->attr[DISTANCE_INDEX].value);
//printf("%f\n", laserpar[laserIndex]);
}
}
break;
case XML_IN_TAG_END:
//printf("end tag: %s\n", x->a);
break;
case XML_IN_TEXT:
//printf("text: %d bytes\n \"", x->n);
//fwrite(x->a, 1, x->n, stdout);
//printf("\"\n");
break;
}
}
}