Add infos into target window
[ryzomcore.git] / ryzom / server / src / general_utilities_service / gus_utils.cpp
bloba5f98115a54bc540885cd81eae5d38b5aaa7ecd8
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //-----------------------------------------------------------------------------
18 // includes
19 //-----------------------------------------------------------------------------
21 // nel
22 #include "nel/misc/types_nl.h"
23 #include "nel/misc/algo.h"
24 #include "nel/net/unified_network.h"
26 // game share
27 #include "game_share/singleton_registry.h"
29 // local
30 #include "gus_utils.h"
33 //-------------------------------------------------------------------------------------------------
34 // namespaces
35 //-------------------------------------------------------------------------------------------------
37 using namespace std;
38 using namespace NLMISC;
39 using namespace NLNET;
42 //-----------------------------------------------------------------------------
43 // GUS namespace
44 //-----------------------------------------------------------------------------
46 namespace GUS
48 //-----------------------------------------------------------------------------
49 // private routines for layer 5 service tracking
50 //-----------------------------------------------------------------------------
52 typedef std::map<NLNET::TServiceId,CSString> TServiceMap;
53 static TServiceMap ServiceMap;
55 static void cbServiceUp(const std::string& serviceName, NLNET::TServiceId sid, void *)
57 ServiceMap[sid]= serviceName;
60 static void cbServiceDown(const std::string& serviceName, NLNET::TServiceId sid, void *)
62 ServiceMap.erase(sid);
66 //-----------------------------------------------------------------------------
67 // executeRemoteCommand
68 //-----------------------------------------------------------------------------
70 void executeRemoteCommand(NLNET::TServiceId sid,const CSString& cmdLine)
72 // make sure the service we're trying to talk to is up
73 DROP_IF(ServiceMap.find(sid)==ServiceMap.end(),"Failed to send command to unknown remote service: "+NLMISC::toString(sid.get()),return);
75 // display a little debug message...
76 nldebug("Sending command to service: %s(%d): %s",
77 ServiceMap[sid].c_str(),
78 sid.get(),
79 cmdLine.c_str());
81 // compose and send the command execution message
82 CMessage msgout("EXEC_COMMAND");
83 msgout.serial(const_cast<CSString&>(cmdLine));
84 CUnifiedNetwork::getInstance()->send(sid,msgout);
87 void executeRemoteCommand(const char* serviceName,const CSString& cmdLine)
89 uint32 count=0;
91 // iterate over all of the services that are up right now
92 for (TServiceMap::iterator it= ServiceMap.begin(); it!=ServiceMap.end(); ++it)
94 // check whether the srvice matches the srevice name (with wildcards) that we supplied
95 if (NLMISC::testWildCard(it->second,serviceName),NLMISC::testWildCard(it->second.toUpper(),CSString(serviceName).toUpper()))
97 // delegate command execution to executeRemoteCommand(uint16 sid,cmdLine)
98 executeRemoteCommand(it->first,cmdLine);
99 ++count;
102 if (count!=1)
104 nldebug("Command sent to %d services",count);
108 static void cbExecCommandResult(CMessage &msgin, const string &serviceName, NLNET::TServiceId sid)
110 // treat the rely message sent back from a service whom we asked to execute a command
111 NLMISC::InfoLog->displayNL("EXEC_COMMAND_RESULT' Received from: %3d: %s",sid.get(),serviceName.c_str());
113 // retrieve the text from the input message
114 CSString txt;
115 msgin.serial(txt);
117 // divide the text into lines because NeL doesn't like long texts
118 CVectorSString lines;
119 txt.splitLines(lines);
121 // display the lines of text
122 for (uint32 i=0;i<lines.size();++i)
124 NLMISC::InfoLog->displayNL("%s",lines[i].c_str());
129 //-----------------------------------------------------------------------------
130 // code for registering callbacks at service init time
131 //-----------------------------------------------------------------------------
133 // automatically register the callbacks for message management at service init
134 static class CRegisterGusUtilsMessageCallbacks: public IServiceSingleton
136 public:
137 void init()
139 TUnifiedCallbackItem cbArray[] =
141 { "EXEC_COMMAND_RESULT", cbExecCommandResult, },
143 CUnifiedNetwork::getInstance()->addCallbackArray( cbArray, sizeof(cbArray)/sizeof(cbArray[0]) );
145 CUnifiedNetwork::getInstance()->setServiceUpCallback("*",cbServiceUp);
146 CUnifiedNetwork::getInstance()->setServiceDownCallback("*",cbServiceDown);
149 _RegisterGusUtilsMessageCallbacks;
153 //-----------------------------------------------------------------------------