1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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"
37 using namespace NLNET
;
38 using namespace NLMISC
;
45 void cbPong(CMessage
&msgin
, const std::string
&serviceName
, TServiceId sid
)
48 msgin
.serial( counter
);
49 TTime pingTime
= CTime::getLocalTime()-pingDate
;
51 nlinfo("Received PONG %u (%u ms)", counter
, pingTime
);
56 pingDate
= CTime::getLocalTime();
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);
76 msgoutfe
.serial (NbId
);
77 for (uint i
= 0; i
< NbId
; i
++)
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';
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);
106 // H_BEFORE (serials);
107 for (uint i
= 0; i
< NbId
; i
++)
108 msgout
.serial( Value
);
111 CUnifiedNetwork::getInstance()->send("FS", msgout
);
115 CMessage msgout("VISION");
118 for (uint j = 0; j < 1000; j++)
121 msgout.setType("VISION");
123 for (uint i = 0; i < Nb; i++)
124 msgout.serial( Value );
125 CUnifiedNetwork::getInstance()->send("FS", msgout);
128 TCPUCycle v2
= CTime::getPerformanceTime ();
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");
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");
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");
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
[] =
184 { "ASK_VISION", cbAskVision
}
187 void cbVar (CConfigFile::CVar
&var
)
189 if (var
.Name
== "NbId") NbId
= var
.asInt ();
194 class CGPMService
: public IService
200 static TTime lastPing
= CTime::getLocalTime();
202 TTime ctime
= CTime::getLocalTime();
204 // check ping every 15 seconds
205 if (ctime - lastPing > 15000)
221 /* //nlassert(false);
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()));