/************************************************************* * @brief There should always be exactly one local connection * to us (the process that loaded us). *************************************************************/ Connection* KernelSML::GetEmbeddedConnection() { int index = 0 ; for (Connection* pConnection = m_pConnectionManager->GetConnectionByIndex(index) ; pConnection != NULL ; index++) { if (!pConnection->IsRemoteConnection()) return pConnection ; } return NULL ; }
bool RhsListener::ExecuteRhsCommand(AgentSML* pAgentSML, smlRhsEventId eventID, std::string const& functionName, std::string const& arguments, std::string* pResultStr) { bool result = false ; // Get the list of connections (clients) who have registered to implement this right hand side (RHS) function. ConnectionList* pList = GetRhsListeners(functionName.c_str()) ; // If nobody is listening we're done (not a bug as we register for all rhs functions and only forward specific ones that the client has registered) if (!pList || pList->size() == 0) return result ; ConnectionListIter connectionIter = pList->begin() ; // We need the first connection for when we're building the message. Perhaps this is a sign that // we shouldn't have rolled these methods into Connection. Connection* pConnection = *connectionIter ; // Convert eventID to a string char const* event = m_pKernelSML->ConvertEventToString(eventID) ; // Build the SML message we're doing to send. // Pass the agent in the "name" parameter not the "agent" parameter as this is a kernel // level event, not an agent level one (because you need to register with the kernel to get "agent created"). soarxml::ElementXML* pMsg = pConnection->CreateSMLCommand(sml_Names::kCommand_Event) ; if (pAgentSML) pConnection->AddParameterToSMLCommand(pMsg, sml_Names::kParamName, pAgentSML->GetName()) ; pConnection->AddParameterToSMLCommand(pMsg, sml_Names::kParamEventID, event) ; pConnection->AddParameterToSMLCommand(pMsg, sml_Names::kParamFunction, functionName.c_str()) ; pConnection->AddParameterToSMLCommand(pMsg, sml_Names::kParamValue, arguments.c_str()) ; #ifdef _DEBUG // Generate a text form of the XML so we can look at it in the debugger. char* pStr = pMsg->GenerateXMLString(true) ; #endif AnalyzeXML response ; // We want to call embedded connections first, so that we get the best performance // for these functions. I don't want to sort the list or otherwise change it so // instead we'll just use a rather clumsy outer loop to do this. for (int phase = 0 ; phase < 2 && !result ; phase++) { // Only call to embedded connections bool embeddedPhase = (phase == 0) ; // Reset the iterator to the beginning of the list connectionIter = pList->begin(); // Keep looping until we get a result while (connectionIter != pList->end() && !result) { pConnection = *connectionIter ; // We call all embedded connections (same process) first before // trying any remote methods. This ensures that if multiple folks register // for the same function we execute the fastest one (w/o going over a socket for the result). if (pConnection->IsRemoteConnection() && embeddedPhase) { connectionIter++ ; continue ; } // It would be faster to just send a message here without waiting for a response // but that could produce incorrect behavior if the client expects to act *during* // the event that we're notifying them about (e.g. notification that we're in the input phase). bool ok = pConnection->SendMessageGetResponse(&response, pMsg) ; if (ok) { char const* pResult = response.GetResultString() ; if (pResult != NULL) { (*pResultStr) = pResult ; result = true ; } } connectionIter++ ; } } #ifdef _DEBUG // Release the string form we generated for the debugger pMsg->DeleteString(pStr) ; #endif // Clean up delete pMsg ; return result ; }