Public Member Functions | Protected Member Functions
SystemBuffer Class Reference

Brief description of class still missing. More...

#include <SystemBuffer.h>

Inheritance diagram for SystemBuffer:
GpCoreTools::DynamicBuffer

List of all members.

Public Member Functions

 SystemBuffer (int fileDescriptor)
 ~SystemBuffer ()

Protected Member Functions

virtual int bytesAvailable (char *buffer, int bytesCount)

Detailed Description

Brief description of class still missing.

Full description of class still missing


Constructor & Destructor Documentation

SystemBuffer::SystemBuffer ( int  fileDescriptor)

Description of constructor still missing

References TRACE.

  : DynamicBuffer(fileDescriptor)
{
  TRACE;
}

Description of destructor still missing

References TRACE.

{
  TRACE;
}

Member Function Documentation

int SystemBuffer::bytesAvailable ( char *  buffer,
int  bytesCount 
) [protected, virtual]

Communication protocol:

'poweroff' Powers off system 'reboot' Reboots system 'diskspace' Return disk space on usb storage 'clearlogs' Clears logs (gps, seiscomp and dascube) 'comments' Returns all user comments 'addcomment LEN' Adds user comment of length LEN 'resynchronize' Restarts all acquitions

Implements GpCoreTools::DynamicBuffer.

References GpCoreTools::DynamicBuffer::read(), GpCoreTools::DynamicBuffer::send(), and TRACE.

{
  TRACE;
  int bytesRead=0;
  while(bytesRead<bytesCount) {
    // Scan for a line
    int newBytesRead;
    for(newBytesRead=bytesRead; newBytesRead<bytesCount && buffer[newBytesRead]!='\n'; newBytesRead++) {}
    if(newBytesRead==bytesCount) {
      return bytesRead; // partial line
    }
    int originalBytesRead=bytesRead; // In case message is spread over more than a single line
    switch(buffer[bytesRead]) {
    case 'a':
      if(File::match(buffer, bytesRead, bytesCount, "addcomment=")) {
        if(addComment(buffer, bytesRead, bytesCount)) {
          newBytesRead=bytesRead;
        } else {
          return originalBytesRead;
        }
      }
      break;
    case 'c':
      if(File::match(buffer, bytesRead, bytesCount, "clearusb")) {
        system("find /mnt/usb/waran/archive -mtime +5 -and -type f -delete");
        system("find /mnt/usb/waran/gps -mtime +5 -and -type f -delete");
        system("find /mnt/usb/waran/logs -mtime +5 -and -type f -delete");
      } else if(File::match(buffer, bytesRead, bytesCount, "comments")) {
        char * com=File::readAll("/mnt/usb/waran/comments.txt");
        if(com) {
          int comSize=strlen(com);
          char buf[32];
          sprintf(buf, "comments=%i\n", comSize);
          send(buf, strlen(buf));
          send(com, comSize);
        } else {
          Log::write(0, "cannot open file '/mnt/usb/waran/comments.txt' for command 'comments'\n");
        }
      }
      break;
    case 'd':
      if(File::match(buffer, bytesRead, bytesCount, "diskspace")) {
        int saved_stdout=dup(STDOUT_FILENO);
        int out_pipe[2];
        if(pipe(out_pipe)==0) {
          dup2(out_pipe[1], STDOUT_FILENO); // sets stdout to write end of the pipe
          close(out_pipe[1]);               // close the fd originally attached to the pipe
          system("df /dev/sdb1 | awk '{if(NR==2) {print \"diskspace=\" $4}}'");
          char buf[32];
          int n=::read(out_pipe[0], buf, 32);
          dup2(saved_stdout, STDOUT_FILENO);  // reconnect stdout
          send(buf, n);
        } else {
          Log::write(0, "cannot open pipe for command 'diskspace'\n");
        }
      }
      break;
    case 'p':
      if(File::match(buffer, bytesRead, bytesCount, "poweroff")) {
        system("/etc/init.d/seiscomp stop");  // seiscomp does not always stop properly, a try to fix it
        sleep(5);
        system("/etc/init.d/warangps stop");
        sleep(5);
        send("halting...\n", 11);
        system("sync");
        sleep(10);       // Make sure than all stations received the power off message (multi-hop routes
                         // might be affected by the removal of this station.
        system("poweroff");
      }
      break;
    case 'r':
      if(File::match(buffer, bytesRead, bytesCount, "reboot")) {
        system("/etc/init.d/seiscomp stop");  // seiscomp does not always stop properly, a try to fix it
        sleep(5);
        system("/etc/init.d/warangps stop");
        sleep(5);
        send("halting...\n", 11);
        system("sync");
        sleep(10);       // Make sure than all stations received the power off message (multi-hop routes
                         // might be affected by the removal of this station.
        system("reboot");
      }
      break;
    default:
      break;
    }
    bytesRead=newBytesRead+1;
    // Skip blanks and additionnal end of line characters
    while(bytesRead<bytesCount && isspace(buffer[bytesRead])) {bytesRead++;}
  }
  return bytesRead;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines