File: communication.cpp

package info (click to toggle)
kaptain 1%3A0.73-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 928 kB
  • ctags: 773
  • sloc: cpp: 4,153; makefile: 14
file content (337 lines) | stat: -rw-r--r-- 7,065 bytes parent folder | download | duplicates (2)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <netdb.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include "global.h"
#include "tools.h"
#include "communication.h"
#include "grammar.h"

#define NO_MORE_IO "At most one i/o channel is allowed.\n"

extern Grammar * yygrammar;


Communicator::Communicator()
{
  reader=0;
  fd_read=-1;
  fd_write=-1;
  input=true;
  output=true;
  ok=false;
}


Communicator::~Communicator()
{
  if (reader)
    delete reader;
  if (fd_read!=-1) close(fd_read);
  if (fd_write!=-1) close(fd_write);
}


bool Communicator::is_stdin()
{
  return fd_read==STDIN_FILENO;
}


bool Communicator::setup_stdio()
{
  if (fd_read!=-1 || fd_write!=-1) {
	 MAKE_WARNING(NO_MORE_IO);
	 return false;
  }

  setup_stdin();
  setup_stdout();
  return true;
}


bool Communicator::setup_socket_server(string port_str)
{
  if (fd_read!=-1 || fd_write!=-1) {
	 MAKE_WARNING(NO_MORE_IO);
	 return false;
  }

  int port;
  /* determining port number */
  if (sscanf(port_str.c_str(), "%d", &port)!=1)
	 {
		ostringstream message;
		message << "Invalid port number `" 
				  << port_str
				  << "'." << endl;
		MAKE_ERROR(message.str());
		return false;
	 }

  int socket_fd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  int sin_size, yes=1;

  /* Create the socket.  */
  socket_fd = socket (AF_INET, SOCK_STREAM, 0);
  if (socket_fd == -1)
	 {
		MAKE_ERROR(string("socket: ")+string(strerror(errno))+string(".\n"));
		return false;
	 }
  
  if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) 
	 {
		MAKE_ERROR(string("setsockopt: ")+string(strerror(errno))+string(".\n"));
		return false;
	 }
  
  my_addr.sin_family = AF_INET;         // host byte order
  my_addr.sin_port = htons(port);       // short, network byte order
  my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
  memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

  /* bind */
  if (bind(socket_fd, (struct sockaddr *)&my_addr, 
			  sizeof (struct sockaddr)) == -1)
	 {
		MAKE_ERROR(string("bind: ")+string(strerror(errno))+string(".\n"));
		return false;
	 }

  /* listen */
  if (listen(socket_fd,1) == -1)
	 {
		MAKE_ERROR(string("listen: ")+string(strerror(errno))+string(".\n"));
		return false;
	 }

  /* accept */
  sin_size = sizeof(struct sockaddr_in);
  fd_read=accept(socket_fd, (struct sockaddr *)& their_addr, 
					  (socklen_t *)&sin_size);
  if (fd_read==-1)
	 {
		MAKE_ERROR(string("accept: ")+string(strerror(errno))+string(".\n"));
		return false;
	 }

  fcntl(fd_read, F_SETFL, O_NONBLOCK);

  //cout << inet_ntoa(their_addr.sin_addr) << endl;
  fd_write=fd_read;

  reader=new QSocketNotifier(fd_read, QSocketNotifier::Read);
  connect(reader, SIGNAL(activated(int)), SLOT(data_arrived(int)));
  ok=true;
  return true;
}


bool Communicator::setup_socket_client(string url)
{
  if (fd_read!=-1 || fd_write!=-1) {
	 MAKE_WARNING(NO_MORE_IO);
	 return false;
  }

  unsigned int port;
  size_t separ;
  string host, port_str;
  /* separating host and port */
  separ=url.find(':');
  if (separ==url.npos) 
	 {
		ostringstream message;
		message << "Could not determine server address and port from `" 
				  << url
				  << "' (host:port)." << endl;
		MAKE_ERROR(message.str());
		return false;
	 }
  
  /* determining port number */
  host=url.substr(0,separ);
  port_str=url.substr(separ+1);
  if (sscanf(port_str.c_str(), "%u", &port)!=1)
	 {
		ostringstream message;
		message << "Invalid port number `" 
				  << port_str
				  << "'." << endl;
		MAKE_ERROR(message.str());
		return false;
	 }

  /* connect to the server */
  struct hostent* hostinfo;
  struct sockaddr_in their_addr;

  hostinfo = gethostbyname (host.c_str());
  if (hostinfo == NULL)
	 {
		MAKE_ERROR(string("Couldn't resolve host name `")
					  +host+string("'.\n"));
		return false;
	 }

  fd_read = socket (AF_INET, SOCK_STREAM, 0);
  if (fd_read == -1)
	 {
		MAKE_ERROR(string("socket: ")+string(strerror(errno))+string(".\n"));
		return false;
	 }

  their_addr.sin_family = AF_INET;    // host byte order 
  their_addr.sin_port = htons(port);  // short, network byte order 
  their_addr.sin_addr = *((struct in_addr *)hostinfo->h_addr);
  memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct 

  if (::connect (fd_read, (struct sockaddr *)&their_addr, 
					  sizeof (struct sockaddr)) == -1) 
	 {
		MAKE_ERROR(string("Couldn't connect to `")
					  +url+string("'. ")+string(strerror(errno))+string("\n"));
		return false;
	 }

  fd_write=fd_read;
  fcntl(fd_read, F_SETFL, O_NONBLOCK);
  
  reader=new QSocketNotifier(fd_read, QSocketNotifier::Read);
  connect(reader, SIGNAL(activated(int)), SLOT(data_arrived(int)));
  ok=true;
  return true;
}


bool Communicator::setup_progio(string command)
{
  if (fd_read!=-1 || fd_write!=-1) {
	 MAKE_WARNING(NO_MORE_IO);
	 return false;
  }

  int left[2];
  int right[2];
  pid_t pid;

  pipe (left);
  pipe (right);

  pid = fork ();
  if (pid == (pid_t) 0) 
    {
      close (left[1]);
      close (right[0]);
      dup2 (left[0], STDIN_FILENO);
      dup2 (right[1], STDOUT_FILENO);
      execlp("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
    }
  else 
    {
      close (left[0]);
		close (right[1]);
		fd_read=right[0];
		fd_write=left[1];
    }

  if (fd_read!=-1 && fd_write!=-1)
	 {
		fcntl(fd_read, F_SETFL, O_NONBLOCK);
		reader=new QSocketNotifier(fd_read, QSocketNotifier::Read);
		connect(reader, SIGNAL(activated(int)), SLOT(data_arrived(int)));
		ok=true;
		return true;
	 }
  MAKE_ERROR("Unable to create pipe to `"+command+"': "+strerror(errno)+".\n");
  return false;
}


bool Communicator::setup_stdin()
{
  fd_read=STDIN_FILENO; // stdin
  fcntl(fd_read, F_SETFL, O_NONBLOCK | O_RDONLY );
  reader=new QSocketNotifier(fd_read, QSocketNotifier::Read);
  connect(reader, SIGNAL(activated(int)), SLOT(data_arrived(int)));
  return true;
}


bool Communicator::setup_stdout()
{
  fd_write=STDOUT_FILENO;
  ok=true;
  return true;
}


bool Communicator::setup_filein(string)
{
  return false;
}


bool Communicator::setup_fileout(string)
{
  return false;
}


void Communicator::send(string message)
{
  if (ok && output)
	 {
		int res=write(fd_write, message.c_str(), message.size());
		if (res==-1)
		  {
			 cerr << (string("Error: Unable to send data: ")
						 +string(strerror(errno))+string(".\n"));
			 exit(-1);
			 close(fd_write);
			 ok=false;
		  }
	 }
}


void Communicator::data_arrived(int)
{
#define BUFF_SIZE 255
  char buff[BUFF_SIZE+1];
  ssize_t n;
  do
    {
      n=read(fd_read, buff, BUFF_SIZE);
		if (n==-1)
		  {
			 break;
		  }
		buff[n]=0;
      message+=string(buff);
    }
  while (n==BUFF_SIZE);

  size_t line;
  while ( (line=message.find('\n'))!=message.npos)
	 {
		string first_line=message.substr(0,line+1);
		if (input)
		  yygrammar->recieve(first_line);
		message=message.substr(line+1);

	 }
}