Esempio n. 1
0
void
Client::Receive()
{
  SocketMessage msg;
  try {
    msg = FetchMessage();
  } catch (Exception& e) {
    if (e.ErrorNumber()==11000) // client has been disconnected
      throw Exception(__PRETTY_FUNCTION__, "Some other socket asked for this client's disconnection. Obtemperating...", Fatal);
  }
  if (msg.GetKey()==MASTER_DISCONNECT) {
    throw Exception(__PRETTY_FUNCTION__, "Master disconnected!", Fatal);
  }
  else if (msg.GetKey()==OTHER_CLIENT_DELETED) {
    throw Exception(__PRETTY_FUNCTION__, "Some other socket asked for this client's disconnection. Obtemperating...", Fatal);
  }
  else if (msg.GetKey()==GET_CLIENT_TYPE) {
    Send(SocketMessage(CLIENT_TYPE, static_cast<int>(GetType())));
  } 
  else if (msg.GetKey()==PING_CLIENT) {
    std::ostringstream os; os << "Pong. My name is " << GetSocketId() << " and I feel fine, thank you!";
    Send(SocketMessage(PING_ANSWER, os.str()));
    PrintInfo("Got a ping, answering...");
  } 
  else if (msg.GetKey()==CLIENTS_LIST) {
    VectorValue vals = msg.GetVectorValue();
    int i = 0; std::ostringstream o; o << "List of members on the socket:\n\t";
    for (VectorValue::const_iterator v=vals.begin(); v!=vals.end(); v++, i++) {
      if (i!=0) o << ", ";
      o << *v;
    }
    PrintInfo(o.str());
  }
  else {
    ParseMessage(msg);
  }
}