DirectInput.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// DirectInput.cc

#include "lib.h"
#include "log.h"
#include "DirectInput.h"

using namespace std;

DirectInput::DirectInput()
  : state(ACCEPTING)
  , monitorAccept(-1)
  , monitorSocket(-1)
  , index(0)
{
15 16 17 18
  logWrite(COMMAND_INPUT, "Creating the command accept socket");
  monitorAccept = createServer(global::monitorServerPort,
                               "Command accept socket (No incoming command "
                               "connections will be accepted.");
19 20 21 22
}

DirectInput::~DirectInput()
{
23
  logWrite(COMMAND_INPUT, "Destroying DirectInput()");
24 25
  if (monitorAccept != -1)
  {
26
    clearDescriptor(monitorAccept);
27 28 29 30
    close(monitorAccept);
  }
  if (monitorSocket != -1)
  {
31
    clearDescriptor(monitorSocket);
32 33 34 35 36 37
    close(monitorSocket);
  }
}

void DirectInput::nextCommand(fd_set * readable)
{
38 39
  currentCommand.reset(NULL);
//  logWrite(COMMAND_INPUT, "Before ACCEPTING check");
40 41 42
  if (state == ACCEPTING && monitorAccept != -1
      && FD_ISSET(monitorAccept, readable))
  {
43 44 45 46 47 48 49 50
    logWrite(COMMAND_INPUT, "Creating the command socket");
    monitorSocket = acceptServer(monitorAccept, NULL,
                                 "Command socket (Incoming command "
                                 "connection was not accepted)");
    if (monitorSocket != -1)
    {
      state = HEADER;
    }
51
  }
52 53 54
//  logWrite(COMMAND_INPUT, "Before HEADER check");
  if (state == HEADER && monitorSocket != -1
      && FD_ISSET(monitorSocket, readable))
55 56 57 58 59
  {
    int error = recv(monitorSocket, headerBuffer + index,
                     Header::headerSize - index, 0);
    if (error == Header::headerSize - index)
    {
60
//      logWrite(COMMAND_INPUT, "Finished reading a command header");
61 62 63 64 65 66 67 68 69 70
      loadHeader(headerBuffer, &commandHeader);
      index = 0;
      state = BODY;
    }
    else if (error > 0)
    {
      index += error;
    }
    else if (error == 0)
    {
71 72
      logWrite(EXCEPTION, "Read count of 0 returned (state BODY): %s",
               strerror(errno));
73 74 75 76
      disconnect();
    }
    else if (error == -1)
    {
77 78
      logWrite(EXCEPTION, "Failed read on monitorSocket "
               "(state HEADER) index=%d: %s", index,
79 80 81
               strerror(errno));
    }
  }
82 83 84
//  logWrite(COMMAND_INPUT, "Before BODY check");
  if (state == BODY && monitorSocket != -1
      && FD_ISSET(monitorSocket, readable))
85
  {
86 87 88 89 90 91
    int error = 0;
    if (index != commandHeader.size)
    {
      error = recv(monitorSocket, bodyBuffer + index,
                   commandHeader.size - index, 0);
    }
92 93
    if (error == commandHeader.size - index)
    {
94 95
      logWrite(COMMAND_INPUT, "Finished reading a command of type: %d",
               commandHeader.type);
96 97
//      logWrite(COMMAND_INPUT, "Finished reading a command: CHECKSUM=%d",
//               checksum());
98 99 100 101 102 103 104
      if (global::replayArg == REPLAY_SAVE
          && (commandHeader.type == NEW_CONNECTION_COMMAND
              || commandHeader.type == DELETE_CONNECTION_COMMAND
              || commandHeader.type == SENSOR_COMMAND))
      {
        replayWriteCommand(headerBuffer, bodyBuffer, commandHeader.size);
      }
105 106 107 108 109 110 111 112 113 114
      currentCommand = loadCommand(&commandHeader, bodyBuffer);
      index = 0;
      state = HEADER;
    }
    else if (error > 0)
    {
      index += error;
    }
    else if (error == 0)
    {
115 116
      logWrite(EXCEPTION, "Read count of 0 returned (state BODY): %s",
               strerror(errno));
117 118 119 120
      disconnect();
    }
    else if (error == -1)
    {
121 122
      logWrite(EXCEPTION, "Failed read on monitorSocket "
               "(state BODY) index=%d: %s", index,
123 124 125
               strerror(errno));
    }
  }
126 127
//  logWrite(EXCEPTION, "monitorSocket: %d, FD_ISSET: %d", monitorSocket,
//           FD_ISSET(monitorSocket, & global::readers));
128 129 130 131 132 133 134 135 136
}

int DirectInput::getMonitorSocket(void)
{
  return monitorSocket;
}

void DirectInput::disconnect(void)
{
137
  logWrite(COMMAND_INPUT, "Disconnecting from command socket");
138 139 140 141
  if (monitorSocket != -1)
  {
    clearDescriptor(monitorSocket);
    close(monitorSocket);
142
    monitorSocket = -1;
143 144 145
  }
  index = 0;
  state = ACCEPTING;
146
  currentCommand.reset(NULL);
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
int DirectInput::checksum(void)
{
  int flip = 1;
  int total = 0;
  int i = 0;
  for (i = 0; i < Header::headerSize; ++i)
  {
    total += (headerBuffer[i] & 0xff) * flip;
//    flip *= -1;
  }
  for (i = 0; i < commandHeader.size; ++i)
  {
    total += (bodyBuffer[i] & 0xff) * flip;
//    flip *= -1;
  }
  return total;
}