-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebclient.cpp
More file actions
246 lines (223 loc) · 6.76 KB
/
webclient.cpp
File metadata and controls
246 lines (223 loc) · 6.76 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Web Client
*
*/
//#define WEBCLIENT_DEBUG 1
/* Includes */
#include <stdio.h> /* Input/Output */
#include <iostream>
using namespace std;
#include <string>
#include <string.h>
#include <errno.h> /* Errors */
#include <stdlib.h> /* General Utilities */
#include <fstream>
#include <sstream>
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <sys/wait.h> /* Wait for Process Termination */
#include <termios.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <signal.h>
#include <netdb.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/time.h>
#include "../WEBlib.d/WEBlib.h"
#include "webclient.h"
char htmlcode[2048];
bool trace_html= false;
int webclient_message_queue_id;
// web server error
void wc_error(const char *msg, bool fatal=false) // C library function void perror(const char *str)
{
fprintf(stderr, "[WS ERROR] ");
fflush(stderr);
perror(msg);
if(fatal) exit(EXIT_FAILURE);
}
void web_client(char *servername, unsigned int serverport, char* serverpath)
{
char CloudHost_name[128];
char CloudHost_serverpath[128];
char str[64];
char str1[200];
struct sockaddr_in servaddr;
char **pptr;
struct hostent *hptr;
int r;
unsigned int c_error= 0;
unsigned int status_code;
unsigned int c_total= 0;
fprintf(stdout, "\nWEB client started to server %s:%d%s", servername, serverport, serverpath);
fflush(stdout);
snprintf(CloudHost_name, sizeof(CloudHost_name), "%s:%d", servername, serverport);
snprintf(CloudHost_serverpath, sizeof(CloudHost_serverpath), serverpath);
if((hptr = gethostbyname(servername)) == NULL)
printf("\nERROR gethostbyname\n");
fprintf(stdout, "\nhostname: %s", hptr->h_name);
if (hptr->h_addrtype == AF_INET && (pptr = hptr->h_addr_list) != NULL)
{
fprintf(stdout, "\naddress: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
}
else {
fprintf(stderr, "\n[ERROR]Error call inet_ntop");
fflush(stderr);
}
fflush(stdout);
while(1)
{
// 1. wait for process message
// a process-message means there is a network message to be sent to the cloud
pmessbuf rbuf;
msgrcv(webclient_message_queue_id, &rbuf, sizeof(pmessbuf)-sizeof(long), 1, 0);
// MESSAGE
// HTML payload - Action=xxxx&yyy
htmlcode[0]='\0';
// 1. copy message
snprintf((char *)htmlcode, sizeof(htmlcode),
"POST %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1\r\n"
"Content-length: %d\r\n\r\n"
"%s", CloudHost_serverpath, CloudHost_name, strlen(rbuf.data), rbuf.data);
if(trace_html)
{
fprintf(stdout, "\n%s\n", htmlcode);
fflush(stdout);
}
// 2. create socket to send IP cloud message
// SOCKET
int sockfd = socket(AF_INET, SOCK_STREAM, 0); // AF_INET: IPv4 Internet protocols / SOCK_STREAM: TCP sockect
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(serverport);
inet_pton(AF_INET, str, &servaddr.sin_addr);
// Set time-out before connect
struct timeval timeout;
timeout.tv_sec = 3; // 3 second timeout
timeout.tv_usec = 0;
if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
wc_error("setsockopt failed\n");
if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
wc_error("setsockopt failed\n");
r= connect(sockfd, (struct sockaddr *) & servaddr, sizeof(servaddr));
// (END) SOCKET
c_total ++;
if(r != 0)
{
// callback_timer
if(rbuf.callback) rbuf.callback(-1, (char *) 0);
fprintf(stdout, "-- errno= %d -- %s", errno, strerror(errno));
fflush(stdout);
}
else
{
ssize_t n;
// 3. SEND
n= write(sockfd, htmlcode, strlen(htmlcode));
// 4. RECEIVE response
status_code= 0;
n = read(sockfd, htmlcode, sizeof(htmlcode)-1);
// Check if there are packets left (check it one time - I may need to give some more tries)
if(n>0)
{
htmlcode[n]='\0';
// (DEBUG) Show buffer
if(trace_html)
{
fprintf(stdout, "\n\n%s", htmlcode);
fflush(stdout);
}
status_code= HTTPHeader_status_code(htmlcode);
if(status_code==200 && !XML_CodeComplete(htmlcode) && (size_t)n<(sizeof(htmlcode)-1))
{
ssize_t m;
sprintf(str1, "+");
write(STDOUT_FILENO, str1, strlen(str1));
m = read(sockfd, &htmlcode[n], sizeof(htmlcode)-n-1);
if(m<0) {
snprintf(str1, sizeof(str1), "-- errno (2)= %d --", errno);
write(STDOUT_FILENO, str1, strlen(str1));
}
else n += m;
htmlcode[n]='\0';
}
}
else if(n==0) {
htmlcode[0]='\0';
fprintf(stdout,"\n--- ZERO BYTES read");
fflush(stdout);
}
else // n<0
{
htmlcode[0]='\0';
fprintf(stdout, "-- errno= %d -- %s", errno, strerror(errno));
fflush(stdout);
}
// (END) RECEIVE
if(n>0 && status_code==200 && XML_CodeComplete(htmlcode))
{
char * xmlcode_ptr= XML_CodePtr(htmlcode);
if(rbuf.callback) rbuf.callback(0, xmlcode_ptr);
}
// if(status_code==200 && XML_CodeComplete(htmlcode))
else
{
if(status_code == 0) {
sprintf(str1, " posible time-out");
write(STDOUT_FILENO, str1, strlen(str1));
c_error ++;
double d1= c_error * 100;
double d2= c_total;
sprintf(str1, " %.3f%%", d1/d2); // percentage
write(STDOUT_FILENO, str1, strlen(str1));
}
else if(status_code == 200) {
sprintf(str1, "\n[ERROR] Incomplete message. IGNORED");
write(STDOUT_FILENO, str1, strlen(str1));
}
else {
char status[32];
HTTPHeader_status_str(htmlcode, status);
sprintf(str1, " %s", status);
write(STDOUT_FILENO, str1, strlen(str1));
}
// callback with result= 1 error
if(rbuf.callback) rbuf.callback(1, (char *) 0);
}
}
close(sockfd);
}
}
// Send a message to the cloud
// callback - function called when the cloud responds
int webClient_SendMessage(char *message, void (*callback) (int, char *))
{
pmessbuf sbuf;
sbuf.mtype = 1; // Any value > 0
sbuf.callback= callback;
snprintf(sbuf.data, SZ_PROCESS_MESSAGE, "%s", message);
if(msgsnd(webclient_message_queue_id, &sbuf, sizeof(pmessbuf)-sizeof(long), IPC_NOWAIT) !=0)
{
wc_error("[webClient_SendMessage]");
fflush(stdout); return -1;
}
return 0;
}
void webClient_callback(int result, char *xmlcode)
{
if(result==0 && xmlcode!=0)
{
#ifdef WEBCLIENT_DEBUG
fprintf(stdout, "\nxmlcode %s\n", xmlcode);
fflush(stdout);
#endif
}
}
/* END OF FILE */