1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* 
 * File:   DebugServer.cpp
 * Author: thomas
 * 
 * Created on 10. September 2010, 15:38
 */

#include <iostream>
#include <sstream>

#include <Messages/Messages.pb.h>

#include "DebugServer.h"

#include <Tools/NaoTime.h>
#include <Tools/ThreadUtil.h>

using namespace naoth;

DebugServer::DebugServer()
  : 
  abort(false)
{
  answers = g_async_queue_new();
  g_async_queue_ref(answers);
}

DebugServer::~DebugServer()
{
  // notify the connectionThread to stop
  abort = true;

  // wait for connectionThread to stop
  if(connectionThread.joinable()) {
    connectionThread.join();
  }

  clearQueues();
  comm.disconnect();

  g_async_queue_unref(answers);
}


void DebugServer::start(unsigned short port)
{
  comm.init(port);

  std::cout << "[INFO] Starting debug server thread" << std::endl;
   
  connectionThread = std::thread([this] {this->run();});
  ThreadUtil::setName(connectionThread, "DebugServer");
}

void DebugServer::run()
{
  while(!abort)
  {
    if(comm.isConnected()) 
    {
      try {

        // 1. send out already answered messages
        send();

        // 2. get new commands (maximal 50)
        receive();

      } catch(const char* msg) {
        std::cout << "[WARN] debug server exception: " << msg << std::endl;
        disconnect();
      } catch(...) {
        std::cout << "[WARN] unexpected exception in debug server" << std::endl;
        disconnect();
      }
    }

    // connect again, wait max 1 second until connection is etablished
    // watch connections
    if(!comm.isConnected()) 
    {
      // clear the backlog of old messages before accepting new connections
      if(id_backlog.empty()) {
        comm.connect(1);
      } else {
        clearQueues();
      }
    }

    // always give other thread the possibility to gain control before entering
    // the loop again (wait for 1 ms)
    ThreadUtil::sleep(1); 

    // TODO: do we really need this here?
    std::this_thread::yield();
  } // end while true
}//end run


void DebugServer::receive()
{
  GString* msg = NULL;
  gint32 id;
  unsigned int counter = 0;
  do 
  {
    msg = comm.readMessage(id);
    if(msg != NULL)
    {
      // parse and sort the messages

      DebugMessageIn::Message* message = new DebugMessageIn::Message();
      parseCommand(msg, *message);
      message->id = id;

      id_backlog.insert(id);

      std::size_t p = message->command.find(':');
      std::string base(message->command.substr(0,p));
      std::string subcmd(message->command.substr(p+1));

      if(base == "Cognition") 
      {
        message->command = subcmd;
        received_messages_cognition.push(message);
      } 
      else if (base == "Motion") 
      {
        message->command = subcmd;
        received_messages_motion.push(message);
      } 
      else 
      {
        received_messages_cognition.push(message);
      }
      g_string_free(msg, true);
    }
    counter++;
  } while(msg != NULL && counter < 50); // max of 50 messages
}//end receiveAll


void DebugServer::send()
{
  // NOTE: the size of the queue may change during this loop,
  //       so save it befor the execution
  int size = g_async_queue_length(answers);
  for(int i = 0; i < size && g_async_queue_length(answers) > 0; i++)
  {
    DebugMessageOut::Message* answer = (DebugMessageOut::Message*) g_async_queue_pop(answers);
    id_backlog.erase(answer->id);

    if(answer != NULL)<--- Either the condition 'answer!=NULL' is redundant or there is possible null pointer dereference: answer.
    {
      if(!comm.sendMessage(answer->id, answer->data.data(), answer->data.size()))
      {
        std::cout << "[WARN] could not send message" << std::endl;
        disconnect();
      }
      delete answer;
    }
  }//end for
}//end sendAll

void DebugServer::disconnect()
{
  // stop executing (so it's not messing up with our queues)
  {
    std::lock_guard<std::mutex> lock(m_executing);
    clearQueues();
  }
  // all commands are "answered", disconnect
  comm.disconnect();
}


void DebugServer::getDebugMessageInCognition(DebugMessageIn& buffer)
{
  buffer.messages.clear();
  received_messages_cognition.copy(buffer);
}


void DebugServer::getDebugMessageInMotion(DebugMessageIn& buffer)
{
  buffer.messages.clear();
  received_messages_motion.copy(buffer);
}


void DebugServer::setDebugMessageOut(const DebugMessageOut& buffer)
{
    std::lock_guard<std::mutex> lock(m_executing);

    for(std::list<DebugMessageOut::Message*>::const_iterator iter = buffer.answers.begin(); iter != buffer.answers.end(); ++iter)
    {
      g_async_queue_push(answers, *iter);
    }
}

// TODO: serializer?
void DebugServer::parseCommand(GString* cmdRaw, DebugMessageIn::Message& command) const
{
  naothmessages::CMD cmd;
  command.command = "invalidcommand";

  if(cmd.ParseFromArray(cmdRaw->str, static_cast<int> (cmdRaw->len)))
  {
    command.command = cmd.name();

    for(int i=0; i < cmd.args().size(); i++)
    {
      const naothmessages::CMDArg& arg = cmd.args().Get(i);
      if(arg.has_bytes()) {
        command.arguments[arg.name()] = arg.bytes();
      } else {
        command.arguments[arg.name()] = ""; //arg.name();
      }
    }
  }
}//end parseCommand


void DebugServer::clearQueues()
{
  while (g_async_queue_length(answers) > 0) {
    DebugMessageOut::Message* msg = (DebugMessageOut::Message*) g_async_queue_pop(answers);
    id_backlog.erase(msg->id);
    delete msg;
  }

  received_messages_cognition.clear(id_backlog);
  received_messages_motion.clear(id_backlog);
}