tesseract  3.04.01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVNetwork Class Reference

#include <svutil.h>

Public Member Functions

 SVNetwork (const char *hostname, int port)
 Set up a connection to hostname on port. More...
 
 ~SVNetwork ()
 Destructor. More...
 
void Send (const char *msg)
 Put a message in the messagebuffer to the server and try to send it. More...
 
char * Receive ()
 
void Close ()
 Close the connection to the server. More...
 
void Flush ()
 Flush the buffer. More...
 

Detailed Description

The SVNetwork class takes care of the remote connection for ScrollView This means setting up and maintaining a remote connection, sending and receiving messages and closing the connection. It is designed to work on both Linux and Windows.

Definition at line 107 of file svutil.h.

Constructor & Destructor Documentation

SVNetwork::SVNetwork ( const char *  hostname,
int  port 
)

Set up a connection to hostname on port.

Definition at line 383 of file svutil.cpp.

383  {
384  mutex_send_ = new SVMutex();
385  msg_buffer_in_ = new char[kMaxMsgSize + 1];
386  msg_buffer_in_[0] = '\0';
387 
388  has_content = false;
389  buffer_ptr_ = NULL;
390 
391  struct addrinfo *addr_info = NULL;
392 
393  if (GetAddrInfo(hostname, port, &addr_info) != 0) {
394  std::cerr << "Error resolving name for ScrollView host "
395  << std::string(hostname) << ":" << port << std::endl;
396  }
397 
398  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
399  addr_info->ai_protocol);
400 
401  // If server is not there, we will start a new server as local child process.
402  if (connect(stream_, addr_info->ai_addr, addr_info->ai_addrlen) < 0) {
403  const char* scrollview_path = getenv("SCROLLVIEW_PATH");
404  if (scrollview_path == NULL) {
405 #ifdef SCROLLVIEW_PATH
406 #define _STR(a) #a
407 #define _XSTR(a) _STR(a)
408  scrollview_path = _XSTR(SCROLLVIEW_PATH);
409 #undef _XSTR
410 #undef _STR
411 #else
412  scrollview_path = ".";
413 #endif
414  }
415  const char *prog = ScrollViewProg();
416  std::string command = ScrollViewCommand(scrollview_path);
417  SVSync::StartProcess(prog, command.c_str());
418 
419  // Wait for server to show up.
420  // Note: There is no exception handling in case the server never turns up.
421 
422  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
423  addr_info->ai_protocol);
424 
425  while (connect(stream_, addr_info->ai_addr,
426  addr_info->ai_addrlen) < 0) {
427  std::cout << "ScrollView: Waiting for server...\n";
428 #ifdef _WIN32
429  Sleep(1000);
430 #else
431  sleep(1);
432 #endif
433 
434  stream_ = socket(addr_info->ai_family, addr_info->ai_socktype,
435  addr_info->ai_protocol);
436  }
437  }
438  FreeAddrInfo(addr_info);
439 }
Definition: svutil.h:87
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:75
const int kMaxMsgSize
Definition: svutil.cpp:63
SVNetwork::~SVNetwork ( )

Destructor.

Definition at line 441 of file svutil.cpp.

441  {
442  delete[] msg_buffer_in_;
443  delete mutex_send_;
444 }

Member Function Documentation

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 271 of file svutil.cpp.

271  {
272 #ifdef _WIN32
273  closesocket(stream_);
274 #else
275  close(stream_);
276 #endif
277 }
void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 212 of file svutil.cpp.

212  {
213  mutex_send_->Lock();
214  while (msg_buffer_out_.size() > 0) {
215  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
216  msg_buffer_out_.erase(0, i);
217  }
218  mutex_send_->Unlock();
219 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:169
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:177
char * SVNetwork::Receive ( )

Receive a message from the server. This will always return one line of char* (denoted by
).

Definition at line 223 of file svutil.cpp.

223  {
224  char* result = NULL;
225 #if defined(_WIN32) || defined(__CYGWIN__)
226  if (has_content) { result = strtok (NULL, "\n"); }
227 #else
228  if (buffer_ptr_ != NULL) { result = strtok_r(NULL, "\n", &buffer_ptr_); }
229 #endif
230 
231  // This means there is something left in the buffer and we return it.
232  if (result != NULL) { return result;
233  // Otherwise, we read from the stream_.
234  } else {
235  buffer_ptr_ = NULL;
236  has_content = false;
237 
238  // The timeout length is not really important since we are looping anyway
239  // until a new message is delivered.
240  struct timeval tv;
241  tv.tv_sec = 10;
242  tv.tv_usec = 0;
243 
244  // Set the flags to return when the stream_ is ready to be read.
245  fd_set readfds;
246  FD_ZERO(&readfds);
247  FD_SET(stream_, &readfds);
248 
249  int i = select(stream_+1, &readfds, NULL, NULL, &tv);
250 
251  // The stream_ died.
252  if (i == 0) { return NULL; }
253 
254  // Read the message buffer.
255  i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0);
256 
257  // Server quit (0) or error (-1).
258  if (i <= 0) { return NULL; }
259  msg_buffer_in_[i] = '\0';
260  has_content = true;
261 #ifdef _WIN32
262  return strtok(msg_buffer_in_, "\n");
263 #else
264  // Setup a new string tokenizer.
265  return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
266 #endif
267  }
268 }
const int kMaxMsgSize
Definition: svutil.cpp:63
void SVNetwork::Send ( const char *  msg)

Put a message in the messagebuffer to the server and try to send it.

Definition at line 205 of file svutil.cpp.

205  {
206  mutex_send_->Lock();
207  msg_buffer_out_.append(msg);
208  mutex_send_->Unlock();
209 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:169
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:177

The documentation for this class was generated from the following files: