Update lua versions
[ryzomcore.git] / nel / samples / net / net_layer5 / gpm_service.cpp
blob7ba503479fc9ab89c626c9e0beb6cd89cab46d3f
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
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/>.
19 * Layer 4 and Service example, ping server.
21 * This ping service expects pings, sends pongs back.
23 * To run this program, ensure there is a file "ping_service.cfg"
24 * containing the location of the naming service (NSHost, NSPort)
25 * in the working directory. The naming service must be running.
29 // We're using the NeL Service framework, and layer 5
30 #include "nel/net/service.h"
31 #include "nel/misc/time_nl.h"
32 #include "nel/misc/displayer.h"
33 #include "nel/misc/command.h"
34 #include "nel/misc/hierarchical_timer.h"
36 using namespace std;
37 using namespace NLNET;
38 using namespace NLMISC;
40 uint32 NbId = 0;
43 TTime pingDate;
45 void cbPong(CMessage &msgin, const std::string &serviceName, TServiceId sid)
47 uint32 counter;
48 msgin.serial( counter );
49 TTime pingTime = CTime::getLocalTime()-pingDate;
51 nlinfo("Received PONG %u (%u ms)", counter, pingTime);
54 void sendPing()
56 pingDate = CTime::getLocalTime();
57 uint32 counter = 0;
58 CMessage msgout("PING");
59 msgout.serial( counter );
60 nlinfo( "Send PING 0");
61 CUnifiedNetwork::getInstance()->send("PS", msgout);
66 void cbPos(CMessage &msgin, const std::string &serviceName, TServiceId sid)
68 CMessage msgout("ACK_POS");
69 CUnifiedNetwork::getInstance()->send("PLS", msgout);
71 TCPUCycle v1 = CTime::getPerformanceTime ();
73 CMessage msgoutfe("POS", false, CMessage::UseDefault, NbId*8);
75 uint64 id = rand();
76 msgoutfe.serial (NbId);
77 for (uint i = 0; i < NbId; i++)
79 msgoutfe.serial (id);
80 id++;
83 TCPUCycle v2 = CTime::getPerformanceTime ();
85 CUnifiedNetwork::getInstance()->send("FS", msgoutfe);
87 TCPUCycle v3 = CTime::getPerformanceTime ();
89 nlinfo( "Received POS, Sending POS to FS (serial %.2fs, send %.2fs)", CTime::ticksToSecond (v2-v1), CTime::ticksToSecond (v3-v2));
92 void cbAskVision(CMessage &msgin, const std::string &serviceName, TServiceId sid)
94 uint32 Value = '0ACE';
96 // H_BEFORE (Vision);
98 TCPUCycle v1 = CTime::getPerformanceTime ();
100 // H_BEFORE (CMessage);
101 CMessage msgout("VISION", false, CMessage::UseDefault, 10000000);
102 // H_AFTER (CMessage);
103 // H_BEFORE (serial);
104 msgout.serial(NbId);
105 // H_AFTER (serial);
106 // H_BEFORE (serials);
107 for (uint i = 0; i < NbId; i++)
108 msgout.serial( Value );
109 // H_AFTER(serials);
110 // H_BEFORE (send);
111 CUnifiedNetwork::getInstance()->send("FS", msgout);
112 // H_AFTER (send);
115 CMessage msgout("VISION");
116 uint32 Nb = 10;
118 for (uint j = 0; j < 1000; j++)
120 msgout.clear();
121 msgout.setType("VISION");
122 msgout.serial(Nb);
123 for (uint i = 0; i < Nb; i++)
124 msgout.serial( Value );
125 CUnifiedNetwork::getInstance()->send("FS", msgout);
128 TCPUCycle v2 = CTime::getPerformanceTime ();
130 // H_AFTER (Vision);
132 // ca prend bcp de cpu un info...
133 nlinfo("Sent Vision with %d values in %.2fms", NbId, CTime::ticksToSecond (v2-v1)*1000.0f);
137 void cbUpPS(const std::string &serviceName, TServiceId sid, void *arg)
139 nlinfo("Ping Service connecting");
140 sendPing();
143 void cbDownPS(const std::string &serviceName, TServiceId sid, void *arg)
145 nlinfo("Ping Service disconnecting");
149 void cbUpFS(const std::string &serviceName, TServiceId sid, void *arg)
151 nlinfo("F Service connecting");
152 sendPing();
155 void cbDownFS(const std::string &serviceName, TServiceId sid, void *arg)
157 nlinfo("F Service disconnecting");
161 void cbUpService(const std::string &serviceName, TServiceId sid, void *arg)
163 nlinfo("Service %s %d is up", serviceName.c_str(), sid.get());
165 CMessage msgout("TOTO");
166 uint32 i = 10;
167 msgout.serial(i);
168 CUnifiedNetwork::getInstance()->send(sid, msgout);
171 void cbDownService(const std::string &serviceName, TServiceId sid, void *arg)
173 nlinfo("Service %s %d is down", serviceName.c_str(), sid.get());
178 * Callback array for messages received from a client
180 TUnifiedCallbackItem CallbackArray[] =
182 { "PONG", cbPong },
183 { "POS", cbPos },
184 { "ASK_VISION", cbAskVision }
187 void cbVar (CConfigFile::CVar &var)
189 if (var.Name == "NbId") NbId = var.asInt ();
190 else nlstop;
194 class CGPMService : public IService
196 public:
198 bool update()
200 static TTime lastPing = CTime::getLocalTime();
202 TTime ctime = CTime::getLocalTime();
204 // check ping every 15 seconds
205 if (ctime - lastPing > 15000)
207 sendPing();
208 lastPing = ctime;
212 return true;
216 * Initialization
218 void init()
220 // nlerror ("oups");
221 /* //nlassert(false);
222 char *p=0;
223 p[0]=0;
224 printf(p);
226 ConfigFile.setCallback ("NbId", cbVar);
227 cbVar (ConfigFile.getVar ("NbId"));
229 // Connect to the ping service
230 CUnifiedNetwork *instance = CUnifiedNetwork::getInstance();
232 instance->setServiceUpCallback("PS", cbUpPS, NULL);
233 instance->setServiceDownCallback("PS", cbDownPS, NULL);
235 instance->setServiceUpCallback("FS", cbUpFS, NULL);
236 instance->setServiceDownCallback("FS", cbDownFS, NULL);
238 instance->setServiceUpCallback("*", cbUpService, NULL);
239 instance->setServiceDownCallback("*", cbDownService, NULL);
245 * Declare a service with the class IService, the names "PS" (short) and "ping_service" (long).
246 * The port is automatically allocated (0) and the main callback array is CallbackArray.
248 NLNET_SERVICE_MAIN( CGPMService, "GPMS", "gpm_service", 0, CallbackArray, "", "" )
250 NLMISC_COMMAND (wait, "", "<time>")
252 if(args.size() != 1) return false;
254 nlSleep (atoi (args[0].c_str()));
256 return true;