Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / samples / net / net_layer5 / flood_service.cpp
blob16eb858c60a88fddc609afcfc89f7a4b236484b9
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"
33 #include "nel/misc/thread.h"
35 using namespace std;
36 using namespace NLNET;
37 using namespace NLMISC;
41 const uint NumThreads = 10;
42 volatile TTime PingDates[NumThreads];
43 volatile bool ServiceReady = false;
45 void cbPong(CMessage &msgin, const std::string &serviceName, TServiceId sid)
47 uint32 counter;
48 msgin.serial( counter );
49 TTime pingTime = CTime::getLocalTime()-PingDates[counter];
50 PingDates[counter] = 0;
51 nlinfo("Received PONG %u (%u ms)", counter, pingTime);
53 CMessage msgout("ACK_POS");
54 CUnifiedNetwork::getInstance()->send("PLS", msgout);
57 void sendPing(uint i)
59 PingDates[i] = CTime::getLocalTime();
60 uint32 counter = i;
61 CMessage msgout("PING");
62 msgout.serial( counter );
63 nlinfo( "Send PING %d", counter);
64 CUnifiedNetwork::getInstance()->send("PS", msgout);
65 nlinfo( "PING %d sent", counter);
68 class CPinger : public IRunnable
70 private:
71 static volatile uint _PingerCount;
72 uint _PingerId;
74 public:
75 CPinger()
77 _PingerId = _PingerCount++;
80 void run()
82 uint i;
83 uint totalPing = 0;
85 while (!ServiceReady)
87 nlSleep(10);
90 while (totalPing < 200)
92 i = rand()*200/RAND_MAX+200;
93 nlSleep(i);
94 if (PingDates[_PingerId] == 0)
96 sendPing(_PingerId);
97 ++totalPing;
103 volatile uint CPinger::_PingerCount = 0;
106 void cbPos(CMessage &msgin, const std::string &serviceName, TServiceId sid)
108 CMessage msgout("POS");
109 CUnifiedNetwork::getInstance()->send("GPMS", msgout);
111 nlinfo( "Received POS from %s, send POS to GPMS", serviceName.c_str());
114 void cbAckPos(CMessage &msgin, const std::string &serviceName, TServiceId sid)
116 nlinfo( "Received ACK_POS from %s", serviceName.c_str());
120 void cbUpPS(const std::string &serviceName, TServiceId sid, void *arg)
122 nlinfo("Ping Service connecting");
125 void cbDownPS(const std::string &serviceName, TServiceId sid, void *arg)
127 nlinfo("Ping Service disconnecting");
131 void cbUpService(const std::string &serviceName, TServiceId sid, void *arg)
133 nlinfo("Service %s %d is up", serviceName.c_str(), sid.get());
136 void cbDownService(const std::string &serviceName, TServiceId sid, void *arg)
138 nlinfo("Service %s %d is down", serviceName.c_str(), sid.get());
143 * Callback array for messages received from a client
145 TUnifiedCallbackItem CallbackArray[] =
147 { "PONG", cbPong },
148 { "POS", cbPos },
149 { "ACK_POS", cbAckPos }
154 class CFloodService : public IService
156 public:
158 bool update()
160 ServiceReady = true;
161 return true;
165 * Initialization
167 void init()
169 // Connect to the ping service
170 CUnifiedNetwork *instance = CUnifiedNetwork::getInstance();
172 instance->setServiceUpCallback("PS", cbUpPS, NULL);
173 instance->setServiceDownCallback("PS", cbDownPS, NULL);
175 instance->setServiceUpCallback("*", cbUpService, NULL);
176 instance->setServiceDownCallback("*", cbDownService, NULL);
178 uint i;
180 for (i=0; i<NumThreads; ++i)
182 IRunnable *runnable = (IRunnable *)(new CPinger());
183 IThread *thread = IThread::create(runnable);
184 thread->start();
191 * Declare a service with the class IService, the names "PS" (short) and "ping_service" (long).
192 * The port is automatically allocated (0) and the main callback array is CallbackArray.
194 NLNET_SERVICE_MAIN( CFloodService, "FLS", "flood_service", 0, CallbackArray, "", "" )