//////////////////////////////////////////////////////////////////////////////////// /// /// \brief Method to add a new connection manually to a component at an /// IP address. /// /// \param[in] networkIP IP Address of component. /// \param[in] jausID JAUS ID of component. /// \param[in] port Network port to use (default is 3794). /// /// \return True on success, false on failure. /// //////////////////////////////////////////////////////////////////////////////////// bool JUDP::AddConnection(const CxUtils::IP4Address& networkIP, const Address& jausID, const unsigned short port) { bool result = false; if(jausID.IsValid() && !jausID.IsBroadcast()) { Mutex::ScopedLock lock(&mClientsMutex); std::map<Address, CxUtils::UdpClient*>::iterator citer; citer = mClients.find(jausID); if(citer == mClients.end()) { CxUtils::UdpClient* udp = mMulticast.CreateNewDestination(networkIP, port); if(udp) { mClients[jausID] = udp; mUpdateTimes[jausID] = Time::GetUtcTimeMs(); mPermanentConnections[jausID] = true; udp = NULL; result = true; } else { if(mDebugMessagesFlag) { Mutex::ScopedLock lock(&mDebugMessagesMutex); std::cout << "[" << GetServiceID().ToString() << "-" << mComponentID.ToString() << "] - New Connection Made to " << jausID << " at IP:" << networkIP.mString << std::endl; } } } } return result; }
//////////////////////////////////////////////////////////////////////////////////// /// /// \return ID of the controlling component. /// //////////////////////////////////////////////////////////////////////////////////// void AccessControl::SetControllerID(const Address& id) { if(id.IsValid() && !id.IsBroadcast()) { Mutex::ScopedLock lock(&mControlMutex); mControllerID = id; } }
//////////////////////////////////////////////////////////////////////////////////// /// /// \brief Set the ID of controlling component. /// /// \param[in] ID of the controlling component. /// //////////////////////////////////////////////////////////////////////////////////// void AccessControl::SetControllerID(const Address& id) { if(id.IsValid() && !id.IsBroadcast()) { WriteLock wLock(mControlMutex); mControllerID = id; } }
//////////////////////////////////////////////////////////////////////////////////// /// /// \brief Sets the source ID of the message. /// /// \param[in] src Source ID. Must be valid and non-broadcast. /// /// \return OK on success, FAILURE on error. /// //////////////////////////////////////////////////////////////////////////////////// int Message::SetSourceID(const Address& src) { if(src.IsValid() && !src.IsBroadcast()) { mSourceID = src; return OK; } return FAILURE; }
//////////////////////////////////////////////////////////////////////////////////// /// /// \brief Initializes the Transport Service based on configuration values /// set. /// /// \param[in] componentID ID of the component using the Transport Service. /// /// \return True on success, false on failure. /// //////////////////////////////////////////////////////////////////////////////////// bool JUDP::Initialize(const Address& componentID) { if(componentID.IsValid() == false || componentID.IsBroadcast()) { return false; } Shutdown(); // Save ID. mComponentID = componentID; CxUtils::IP4Address::List hostnames; CxUtils::IP4Address::List::iterator eth0; CxUtils::Socket::GetHostAddresses(hostnames); if(hostnames.size() == 0 && mDebugMessagesFlag) { Mutex::ScopedLock lock(&mDebugMessagesMutex); std::cout << "[JUDP-" << mComponentID.ToString() << "] - No Network Interface Found.\n"; } unsigned int count = 0; for(eth0 = hostnames.begin(); eth0 != hostnames.end(); eth0++, count++) { // Use any or specified interface. if(mHostIP.mString == "0.0.0.0" || mHostIP.mString == "127.0.0.1" || mHostIP.mString.empty() || mHostIP == *eth0) { // Initialize UDP receiving. if(mInput.InitializeMulticastSocket(Port, mMulticastIP, true)) { mMulticast.SetNetworkInterface(*eth0); if(mMulticast.InitializeMulticastSocket(mMulticastIP, Port, mTimeToLive, mUseBroadcastingFlag, 0)) { SetComponentID(componentID); mPrimaryThread.CreateThread(JUDP::ReceiveThread, this); CxUtils::SleepMs(250); mSecondaryThread.CreateThread(JUDP::ReceiveThread, this); // Set thread names. mPrimaryThread.SetThreadName(std::string("JUDP 1 ") + componentID.ToString()); mSecondaryThread.SetThreadName(std::string("JUDP 2 ") + componentID.ToString()); return true; } } /* mPrimarySocket.SetNetworkInterface(mHostIP); mSecondarySocket.SetNetworkInterface(mHostIP); // Try initialize on 3794 first, but accept any available. if(mPrimarySocket.InitializeMulticastSocket(mMulticastIP, Port, mTimeToLive, mUseBroadcastingFlag, Port, true) && mSecondarySocket.InitializeMulticastSocket(mMulticastIP, Port, mTimeToLive, mUseBroadcastingFlag, 0, false)) { SetComponentID(componentID); mPrimaryThread.CreateThread(JUDP::PrimaryThread, this); std::string threadName = std::string("JUDP 1 ") + componentID.ToString(); mPrimaryThread.SetThreadName(threadName); // Have a thread to discover new UDP clients that are transmitting. mSecondaryThread.CreateThread(JUDP::SecondaryThread, this); threadName = std::string("JUDP 2 ") + componentID.ToString(); mSecondaryThread.SetThreadName(threadName); return true; } */ } } Shutdown(); return false; }