void MulticastHandler::dispatch( boost::asio::ip::address senderAddress, MulticastNetwork::Command command, char *args, unsigned char argsSize, bool forward) { typedef MulticastNetwork::Command Command; m_senderAddress = &senderAddress; bool valid = false; if (Command::Candidate == command && sizeof(uint32_t) == argsSize) { valid = true; on_candidate(ntohl(*reinterpret_cast<uint32_t*>(args))); } else if (Command::ElectGateway == command && 0 == argsSize) { valid = true; on_electGateway(); } else if (Command::Forward == command && 0 < argsSize) { valid = true; on_forward(static_cast<Command>(args[0]), args + 1, argsSize - 1); } else if (Command::Gateway == command && 0 == argsSize) { valid = true; on_gateway(); } else if (Command::Hello == command && sizeof(float) < argsSize) { valid = true; uint32_t b = ntohl(*reinterpret_cast<uint32_t*>(args + argsSize - sizeof(float))); on_hello(std::string(args, argsSize - sizeof(float)), *reinterpret_cast<float*>(&b)); } else if (Command::Message == command && 0 < argsSize) { valid = true; on_message(std::string(args, argsSize)); } else if (Command::RemoteGateway == command && 0 == argsSize) { valid = true; on_remoteGateway(); } else if (Command::SearchBlock == command && sizeof(uint32_t) < argsSize) { valid = true; on_searchBlock(std::string(args, argsSize - sizeof(uint32_t)), *reinterpret_cast<uint32_t*>(args + argsSize - sizeof(uint32_t))); } else if (Command::SearchFileName == command && 0 < argsSize) { valid = true; on_searchFileName(std::string(args, argsSize)); } else if (Command::SearchFileNameReply == command && 0 < argsSize) { valid = true; std::map<std::string, std::pair<float, std::string>> results; int index = 0; while (index < argsSize) { float filesize; int stringLength = 0; for (int i = index + Hash::SIZE + sizeof filesize; i < argsSize; i++) { if (args[i] == '\0') { break; } ++stringLength; } if (index + stringLength + Hash::SIZE + sizeof filesize >= argsSize) { break; } std::string hash(args + index, Hash::SIZE); filesize = *reinterpret_cast<float*>(args + index + Hash::SIZE); std::string filename(args + index + Hash::SIZE + sizeof filesize, stringLength); results[filename] = std::pair<float, std::string>(filesize, hash); index += Hash::SIZE + sizeof filesize + stringLength + 1; } on_searchFileNameReply(results); } if (valid && forward) { m_gateway->forward(*m_senderAddress, command, args, argsSize); } }
bool http_servlet::on_default(request_t& req, response_t& res) { return on_hello(req, res); }