Update lua versions
[ryzomcore.git] / nel / samples / net / service / chat_service.cpp
blobc2efc71f4dca13f324bd11d23b67947fb87ad413
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/>.
17 #include <string>
19 #include "nel/misc/common.h"
20 #include "nel/misc/path.h"
21 #include "nel/misc/system_info.h"
23 // contains the service base class
24 #include "nel/net/service.h"
26 using namespace std;
27 using namespace NLMISC;
28 using namespace NLNET;
30 // a service is a process, a program. the goal is to automatically use
31 // common initialization and features (like command line, file check,
32 // debug features, signal redirection and more).
34 // a service has a listen socket for external connection. when a message
35 // comes on the socket, the message is automatically updated and the
36 // associated callback is called.
38 // to create a service, you have to inherit from IService and to implement
39 // a few functions (init, update, release). you are not forced to implement
40 // these functions if you have nothing to do in them.
42 // to launch a service, the naming_service must run.
45 // this stupid example creates a chat service. it's a 1 person chat, it
46 // means that you can send strings to the service and it sends them back to you
47 // (and only to you) so you can't see strings from other people :)
48 // (i warned you! it's really stupid!)
50 class CChatService : public IService
52 public:
54 // FILE *fp;
56 void init ()
58 // this function is called after all standard service initialization.
59 // put here your code that inits your application.
61 nlinfo ("init() was called");
63 // fp = nlfopen (NLMISC::CFile::findNewFile("stat.csv"), "wt");
66 bool update ()
68 // this function is called every "loop". you return true if you want
69 // to continue or return false if you want to exit the service.
70 // the loop is called evenly (by default, at least one time per second).
72 nlinfo ("update() was called");
74 /* static uint ii = 0;
75 if(ii++ == 15)
77 nlstop;
78 }*/
80 //////debug to test log flood for memory leak
82 // DebugLog->addNegativeFilter("flood");
83 // InfoLog->addNegativeFilter("flood");
84 // WarningLog->addNegativeFilter("flood");
86 /* string s;
88 uint v = (uint)frand(80), i;
89 for (i = 0; i < v; i++)
91 s+='a'+rand()%26;
94 static uint32 val = 0;
95 for(i = 0; i < 10; i++)
97 nldebug ("debg flood %d %s", val++, s.c_str());
98 nlinfo ("info flood %d %s", val++, s.c_str());
99 nlwarning ("warn flood %d %s", val++, s.c_str());
100 // nldebug ("debg flood %10d %s %d %d", val++, bytesToHumanReadable(CSystemInfo::getAllocatedSystemMemory ()).c_str(), UserSpeedLoop, NetSpeedLoop);
101 // nlinfo ("info flood %10d %s %d %d", val++, bytesToHumanReadable(CSystemInfo::getAllocatedSystemMemory ()).c_str(), UserSpeedLoop, NetSpeedLoop);
102 // nlwarning ("warn flood %10d %s %d %d", val++, bytesToHumanReadable(CSystemInfo::getAllocatedSystemMemory ()).c_str(), UserSpeedLoop, NetSpeedLoop);
105 /////////
106 // fprintf (fp, "%d;%d;%d\n", CMemUtils::getAllocatedSystemMemory (), UserSpeedLoop, NetSpeedLoop);
107 // fflush (fp);
108 return true;
111 void release ()
113 // this function is called before all standard service release code.
114 // put here your code that releases your application.
116 // fclose (fp);
118 nlinfo ("release() was called");
123 // each time the message CHAT is received, this function is called.
124 // the first param contains parameters of the message. the second one is the
125 // identifier of who sent this message
126 static void cbChat (CMessage &msgin, const std::string &serviceName, TServiceId sid)
128 // get the chat string of the client
129 string chat;
130 msgin.serial (chat);
132 // create the message to send to the other
133 CMessage msgout ("CHAT");
134 msgout.serial (chat);
136 // send it back to the sender
137 CUnifiedNetwork::getInstance()->send (sid, msgout);
141 // this array contains all callback functions. it associates the callbackname (messagename),
142 // with a callback
143 TUnifiedCallbackItem CallbackArray[] =
145 { "CHAT", cbChat }
149 // this macro is the "main". the first param is the class name inherited from IService.
150 // the second one is the name of the service used to register and find the service
151 // using the naming service. the third one is the port where the listen socket will
152 // be created. If you put 0, the system automatically finds a port.
153 NLNET_SERVICE_MAIN (CChatService, "CS", "chat_service", 0, CallbackArray, "", "");