All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 386 of file svutil.cpp.

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

Destructor.

Definition at line 444 of file svutil.cpp.

444  {
445  delete[] msg_buffer_in_;
446  delete mutex_send_;
447 }

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 }
char * strtok_r(char *s1, const char *s2, char **lasts)
Definition: strtok_r.cpp:38
const int kMaxMsgSize
Definition: svutil.cpp:63
#define NULL
Definition: host.h:144
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: