<commit_before>// For conditions of distribution and use, see copyright notice in license.txt

#include "StableHeaders.h"
#include "OpenSimProtocolModule.h"
#include "RexProtocolMsgIDs.h"

#include "Poco/Net/NetException.h"

namespace OpenSimProtocol
{
	OpenSimProtocolModule::OpenSimProtocolModule() :
    ModuleInterfaceImpl(Foundation::Module::MT_OpenSimProtocol),
    connected_(false)
    {
    }
    
    // virtual
    OpenSimProtocolModule::~OpenSimProtocolModule()
    {
    }

    // virtual
    void OpenSimProtocolModule::Load()
    {
        LogInfo("System " + Name() + " loaded.");
    }

    // virtual
    void OpenSimProtocolModule::Unload()
    {
        LogInfo("System " + Name() + " unloaded.");
    }

    // virtual
    void OpenSimProtocolModule::Initialize()
    {
        ///\todo Read the template filename from a config file.
		const char *filename = "./data/message_template.msg";
		
		networkManager_ = boost::shared_ptr<NetMessageManager>(new NetMessageManager(filename));
		assert(networkManager_);
		    
		networkManager_->RegisterNetworkListener(this);
        
        // Register event categories.
        eventManager_ = framework_->GetEventManager();
        networkStateEventCategory_ = eventManager_->RegisterEventCategory("NetworkState");
        networkEventInCategory_ = eventManager_->RegisterEventCategory("OpenSimNetworkIn");
        networkEventOutCategory_ = eventManager_->RegisterEventCategory("OpenSimNetworkOut");

        LogInfo("System " + Name() + " initialized.");
    }

    // virtual 
    void OpenSimProtocolModule::Uninitialize()
    {		
		if(connected_)
		    DisconnectFromRexServer();
        
        networkManager_->UnregisterNetworkListener(this);
      
		LogInfo("System " + Name() + " uninitialized.");
    }
    
    // virtual
    void OpenSimProtocolModule::Update(Core::f64 frametime)
    {
        {
            PROFILE(OpenSimProtocolModule_Update);
            if (loginWorker_.IsReady() && loginWorker_.GetState() == Connection::STATE_XMLRPC_REPLY_RECEIVED)
            {
                // XML-RPC reply received; get the login parameters and signal that we're ready to
                // establish an UDP connection.
                clientParameters_ = loginWorker_.GetClientParameters();
                loginWorker_.SetConnectionState(Connection::STATE_INIT_UDP);
            }
            
            if (connected_)
            {
                try
                {
                    networkManager_->ProcessMessages();
                }
                catch(Poco::Net::NetException &e)
                {
                    LogError(e.what());
                    LogInfo("Network error occured. Closing server connection.");
                    DisconnectFromRexServer();
                }
            }
        }
        RESETPROFILER;
    }
    
    //virtual
    void OpenSimProtocolModule::OnNetworkMessageReceived(NetMsgID msgID, NetInMessage *msg)
    {
        // Send a Network event. The message ID functions as the event ID.
        NetworkEventInboundData data(msgID, msg);
        eventManager_->SendEvent(networkEventInCategory_, msgID, &data);
    }
        
    //virtual
    void OpenSimProtocolModule::OnNetworkMessageSent(const NetOutMessage *msg)
    {
        // Send a NetworkOutStats event.
        NetworkEventOutboundData data(msg->GetMessageID(), msg);
        eventManager_->SendEvent(networkEventOutCategory_, msg->GetMessageID(), &data);
    }

    void OpenSimProtocolModule::LoginToServer(
        const std::string& first_name,
		const std::string& last_name,
		const std::string& password,
		const std::string& address,
		int port,
		ConnectionThreadState *thread_state)
	{   
		std::string callMethod = "login_to_simulator";
		loginWorker_.SetupXMLRPCLogin(first_name, last_name, password, address, boost::lexical_cast<std::string>(port), callMethod, thread_state);

		// Start the thread.
		boost::thread(boost::ref(loginWorker_));
	}

	void OpenSimProtocolModule::LoginToCBServer(
		const std::string& first_name,
		const std::string& last_name,
		const std::string& address,
		int port,
		ConnectionThreadState *thread_state)
	{
		std::string callMethod = "login_to_simulator";
		loginWorker_.SetupXMLRPCLogin(first_name, last_name, "auth_done", address, boost::lexical_cast<std::string>(port), callMethod, thread_state, "openid", "openid", "openid", true); 
		
		// Start the thread.
		boost::thread(boost::ref(loginWorker_));
	}

    
    bool OpenSimProtocolModule::LoginUsingRexAuthentication(
        const std::string& first_name,
		const std::string& last_name,
		const std::string& password,
		const std::string& address,
		int port,
		const std::string& auth_server_address, 
		const std::string& auth_login,
		ConnectionThreadState *thread_state)
	{
		bool authentication = true;
		std::string callMethod = "ClientAuthentication";
		int pos = auth_server_address.find(":");
		std::string auth_port = "";
		std::string auth_address = "";
		
		if (pos != std::string::npos)
		{
			auth_port = auth_server_address.substr(pos+1);
			auth_address = auth_server_address.substr(0,pos);
		}
		else
		{<commit_msg>[Fix] searches port number from end to begin not begin to end. <commit_after>// For conditions of distribution and use, see copyright notice in license.txt

#include "StableHeaders.h"
#include "OpenSimProtocolModule.h"
#include "RexProtocolMsgIDs.h"

#include "Poco/Net/NetException.h"

namespace OpenSimProtocol
{
	OpenSimProtocolModule::OpenSimProtocolModule() :
    ModuleInterfaceImpl(Foundation::Module::MT_OpenSimProtocol),
    connected_(false)
    {
    }
    
    // virtual
    OpenSimProtocolModule::~OpenSimProtocolModule()
    {
    }

    // virtual
    void OpenSimProtocolModule::Load()
    {
        LogInfo("System " + Name() + " loaded.");
    }

    // virtual
    void OpenSimProtocolModule::Unload()
    {
        LogInfo("System " + Name() + " unloaded.");
    }

    // virtual
    void OpenSimProtocolModule::Initialize()
    {
        ///\todo Read the template filename from a config file.
		const char *filename = "./data/message_template.msg";
		
		networkManager_ = boost::shared_ptr<NetMessageManager>(new NetMessageManager(filename));
		assert(networkManager_);
		    
		networkManager_->RegisterNetworkListener(this);
        
        // Register event categories.
        eventManager_ = framework_->GetEventManager();
        networkStateEventCategory_ = eventManager_->RegisterEventCategory("NetworkState");
        networkEventInCategory_ = eventManager_->RegisterEventCategory("OpenSimNetworkIn");
        networkEventOutCategory_ = eventManager_->RegisterEventCategory("OpenSimNetworkOut");

        LogInfo("System " + Name() + " initialized.");
    }

    // virtual 
    void OpenSimProtocolModule::Uninitialize()
    {		
		if(connected_)
		    DisconnectFromRexServer();
        
        networkManager_->UnregisterNetworkListener(this);
      
		LogInfo("System " + Name() + " uninitialized.");
    }
    
    // virtual
    void OpenSimProtocolModule::Update(Core::f64 frametime)
    {
        {
            PROFILE(OpenSimProtocolModule_Update);
            if (loginWorker_.IsReady() && loginWorker_.GetState() == Connection::STATE_XMLRPC_REPLY_RECEIVED)
            {
                // XML-RPC reply received; get the login parameters and signal that we're ready to
                // establish an UDP connection.
                clientParameters_ = loginWorker_.GetClientParameters();
                loginWorker_.SetConnectionState(Connection::STATE_INIT_UDP);
            }
            
            if (connected_)
            {
                try
                {
                    networkManager_->ProcessMessages();
                }
                catch(Poco::Net::NetException &e)
                {
                    LogError(e.what());
                    LogInfo("Network error occured. Closing server connection.");
                    DisconnectFromRexServer();
                }
            }
        }
        RESETPROFILER;
    }
    
    //virtual
    void OpenSimProtocolModule::OnNetworkMessageReceived(NetMsgID msgID, NetInMessage *msg)
    {
        // Send a Network event. The message ID functions as the event ID.
        NetworkEventInboundData data(msgID, msg);
        eventManager_->SendEvent(networkEventInCategory_, msgID, &data);
    }
        
    //virtual
    void OpenSimProtocolModule::OnNetworkMessageSent(const NetOutMessage *msg)
    {
        // Send a NetworkOutStats event.
        NetworkEventOutboundData data(msg->GetMessageID(), msg);
        eventManager_->SendEvent(networkEventOutCategory_, msg->GetMessageID(), &data);
    }

    void OpenSimProtocolModule::LoginToServer(
        const std::string& first_name,
		const std::string& last_name,
		const std::string& password,
		const std::string& address,
		int port,
		ConnectionThreadState *thread_state)
	{   
		std::string callMethod = "login_to_simulator";
		loginWorker_.SetupXMLRPCLogin(first_name, last_name, password, address, boost::lexical_cast<std::string>(port), callMethod, thread_state);

		// Start the thread.
		boost::thread(boost::ref(loginWorker_));
	}

	void OpenSimProtocolModule::LoginToCBServer(
		const std::string& first_name,
		const std::string& last_name,
		const std::string& address,
		int port,
		ConnectionThreadState *thread_state)
	{
		std::string callMethod = "login_to_simulator";
		loginWorker_.SetupXMLRPCLogin(first_name, last_name, "auth_done", address, boost::lexical_cast<std::string>(port), callMethod, thread_state, "openid", "openid", "openid", true); 
		
		// Start the thread.
		boost::thread(boost::ref(loginWorker_));
	}

    
    bool OpenSimProtocolModule::LoginUsingRexAuthentication(
        const std::string& first_name,
		const std::string& last_name,
		const std::string& password,
		const std::string& address,
		int port,
		const std::string& auth_server_address, 
		const std::string& auth_login,
		ConnectionThreadState *thread_state)
	{
		bool authentication = true;
		std::string callMethod = "ClientAuthentication";
		int pos = auth_server_address.rfind(":");
		std::string auth_port = "";
		std::string auth_address = "";
		
		if (pos != std::string::npos)
		{
			auth_port = auth_server_address.substr(pos+1);
			auth_address = auth_server_address.substr(0,pos);
		}
		else
		{
{'fraction_non_alphanumeric': 0.10874818753020783, 'fraction_numerical': 0.0007733204446592557, 'mean_word_length': 2.976172175249808, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2353719', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2836, 'n_words': 564}