void NeighbourDiscovery::sendBeacons() { Logger::getInstance()->setThreadName(std::this_thread::get_id(), "Beacon Sender"); LOG(14) << "Creating send beacons thread"; // Get node configuration std::string nodeId = m_config.getNodeId(); std::string nodeAddress = m_config.getNodeAddress(); uint16_t nodePort = m_config.getNodePort(); #ifndef LEPTON // Generate this node address information. LOG(64) << "Starting socket into " << nodeAddress << ":0"; // Create the socket. Socket s = Socket(false); if (!s) { // Stop the application LOG(1) << "Cannot create socket into sendBeacons thread, reason: " << s.getLastError(); g_stop = true; } else { // Bind the socket. if (!s.bind(nodeAddress, 0)) { // Stop the application LOG(1) << "Cannot bind socket to " << nodeAddress << ", reason: " << s.getLastError(); g_stop = true; } else { // Generate the destination address. s.setDestination(m_config.getDiscoveryAddress(), m_config.getDiscoveryPort()); #endif // Create the beacon with our information. LOG(64) << "Sending beacons to " << m_config.getDiscoveryAddress() << ":" << m_config.getDiscoveryPort(); int sleepTime = m_config.getDiscoveryPeriod(); g_startedThread++; while (!g_stop.load()) { std::this_thread::sleep_for(std::chrono::seconds(sleepTime)); Beacon b = Beacon(nodeId, nodeAddress, nodePort, m_listeningEndpointsTable->getValues()); LOG(21) << "Sending beacon from " << nodeId << " " << nodeAddress << ":" << nodePort; std::string rawBeacon = b.getRaw(); if (s.canSend(m_config.getSocketTimeout())) { if (!(s << rawBeacon)) { LOG(4) << "Error sending beacon " << s.getLastError(); } } } s.close(); #ifndef LEPTON } #endif LOG(14) << "Exit Beacon sender thread."; g_stopped++; #ifndef LEPTON } #endif }
bool ProcessMgmtPktSendRequest ( int mgmtMask ) { if ( mgmtMask & MGMT_PKT_BEACON ) { Beacon (); } if ( mgmtMask & MGMT_PKT_AUTH ) { Auth (); } if ( mgmtMask & MGMT_PKT_ASSO) { Associate (); } if ( mgmtMask & MGMT_PKT_TEST) { TestPkt (); } return true; }
void NeighbourDiscovery::receiveBeacons() { // Get node configuration Logger::getInstance()->setThreadName(std::this_thread::get_id(), "Beacon Receiver"); LOG(15) << "Starting receiver beacon thread"; std::string nodeId = m_config.getNodeId(); std::string nodeAddress = m_config.getNodeAddress(); uint16_t discoveryPort = m_config.getDiscoveryPort(); std::string discoveryAddress = m_config.getDiscoveryAddress(); bool testMode = m_config.getNeighbourTestMode(); #ifndef LEPTON // Generate this node address information. LOG(65) << "Starting socket into " << discoveryAddress << ":" << discoveryPort; // Create the socket. Socket s = Socket(false); if (!s) { // Stop the application LOG(1) << "Cannot create socket into receiveBeacons thread reason: " << s.getLastError(); g_stop = true; } else { if (!s.setReuseAddress()) { // Stop the application LOG(1) << "Cannot set flag REUSEADDR to socket, reason: " << s.getLastError(); g_stop = true; } else { // Bind the socket. if (!s.bind(discoveryAddress, discoveryPort)) { // Stop the application LOG(1) << "Cannot bind the socket to " << discoveryAddress << ":" << discoveryPort << " reason: " << s.getLastError(); g_stop = true; } else { // Join the multicast group. if (!s.joinMulticastGroup(nodeAddress)) { // Stop the application LOG(1) << "Cannot join the multicast group, reason: " << s.getLastError(); g_stop = true; } else { // Add a timeout to the recv socket s.setRcvTimeOut(m_config.getSocketTimeout()); #endif g_startedThread++; while (!g_stop.load()) { uint32_t beaconLength = 65507; std::string buffer; StringWithSize sws = StringWithSize(buffer, beaconLength); if (s.canRead(m_config.getSocketTimeout())) { s >> sws; // Create a thread to add the new neighbour and let this // receiving more beacons Beacon b = Beacon(buffer); if (b.getNodeId() != nodeId || testMode) { LOG(21) << "Received beacon from " << b.getNodeId() << " " << b.getNodeAddress() << ":" << b.getNodePort(); std::thread([b, this]() { m_neighbourTable->update(std::make_shared<Neighbour>( b.getNodeId(), b.getNodeAddress(), b.getNodePort(), b.getEndpoints())); }).detach(); } } } // Leave from the multicast group if (false /*!s.leaveMulticastGroup()*/) { LOG(4) << "Cannot leave the multicast group, reason: " << s.getLastError(); } #ifndef LEPTON } } } #endif s.close(); #ifndef LEPTON }