Tesseract  3.02
 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 105 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 374 of file svutil.cpp.

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

Destructor.

Definition at line 425 of file svutil.cpp.

425  {
426  delete[] msg_buffer_in_;
427  delete mutex_send_;
428 }

Member Function Documentation

void SVNetwork::Close ( )

Close the connection to the server.

Definition at line 259 of file svutil.cpp.

259  {
260 #ifdef _WIN32
261  closesocket(stream_);
262 #else
263  close(stream_);
264 #endif
265 }
void SVNetwork::Flush ( )

Flush the buffer.

Definition at line 200 of file svutil.cpp.

200  {
201  mutex_send_->Lock();
202  while (msg_buffer_out_.size() > 0) {
203  int i = send(stream_, msg_buffer_out_.c_str(), msg_buffer_out_.length(), 0);
204  msg_buffer_out_.erase(0, i);
205  }
206  mutex_send_->Unlock();
207 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:157
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:165
char * SVNetwork::Receive ( )

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

Definition at line 211 of file svutil.cpp.

211  {
212  char* result = NULL;
213 #ifdef _WIN32
214  if (has_content) { result = strtok (NULL, "\n"); }
215 #else
216  if (buffer_ptr_ != NULL) { result = strtok_r(NULL, "\n", &buffer_ptr_); }
217 #endif
218 
219  // This means there is something left in the buffer and we return it.
220  if (result != NULL) { return result;
221  // Otherwise, we read from the stream_.
222  } else {
223  buffer_ptr_ = NULL;
224  has_content = false;
225 
226  // The timeout length is not really important since we are looping anyway
227  // until a new message is delivered.
228  struct timeval tv;
229  tv.tv_sec = 10;
230  tv.tv_usec = 0;
231 
232  // Set the flags to return when the stream_ is ready to be read.
233  fd_set readfds;
234  FD_ZERO(&readfds);
235  FD_SET(stream_, &readfds);
236 
237  int i = select(stream_+1, &readfds, NULL, NULL, &tv);
238 
239  // The stream_ died.
240  if (i == 0) { return NULL; }
241 
242  // Read the message buffer.
243  i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0);
244 
245  // Server quit (0) or error (-1).
246  if (i <= 0) { return NULL; }
247  msg_buffer_in_[i] = '\0';
248  has_content = true;
249 #ifdef _WIN32
250  return strtok(msg_buffer_in_, "\n");
251 #else
252  // Setup a new string tokenizer.
253  return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
254 #endif
255  }
256 }
#define NULL
Definition: host.h:144
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 193 of file svutil.cpp.

193  {
194  mutex_send_->Lock();
195  msg_buffer_out_.append(msg);
196  mutex_send_->Unlock();
197 }
void Lock()
Locks on a mutex.
Definition: svutil.cpp:157
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:165

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