source: trunk/FACT++/dim/src/examples/test_server.cxx@ 12392

Last change on this file since 12392 was 11881, checked in by tbretz, 13 years ago
Updated to DIM V19r24
File size: 5.8 KB
Line 
1//============================================================================
2// Name : emptyDimFormat.cpp
3// Author : Etienne Lyard etienne.lyard@unige.ch
4// Version : 00
5// Copyright :
6// Description : Demonstrates the occurence of an empty Dim format from the client's perspective
7//============================================================================
8/*
9#include <dic.hxx>
10#include <dis.hxx>
11#include <iostream>
12using namespace std;
13
14class EmptyServiceSubscriber : public DimInfo
15{
16 DimStampedInfo* info;
17 int noLink;
18public:
19 EmptyServiceSubscriber()
20 {
21// info = new DimStampedInfo("TIME/EMPTY", const_cast<char*>(""), this);
22 noLink = -1;
23 info = new DimStampedInfo("TIME/EMPTY", noLink, this);
24 }
25 void infoHandler()
26 {
27 DimInfo* I = getInfo();
28 int data;
29 if (I == info)
30 {
31 data = I->getInt();
32// cout << "EMPTY SERVICE UPDATED. " << "Format: " << I->getFormat() << endl;
33 cout << "EMPTY SERVICE UPDATED. " << data << " Format: " << I->getFormat() << endl;
34 }
35 }
36};
37
38int main(int, const char**)
39{
40 DimServer::start("TIME");
41
42 int emptyFormatVariable = 0;
43 DimService* emptyFormatService = new DimService("TIME/EMPTY", "I:1", &emptyFormatVariable, sizeof(long));
44 EmptyServiceSubscriber mySubscriber;
45 //The three lines below create (most of the time) an empty format on the client side.
46 //We must be able to deal with such cases in our framework because services can be stopped and re-spawned at any time
47 delete emptyFormatService;
48 usleep(1000000);
49 emptyFormatService = new DimService("TIME/EMPTY", "I:1", &emptyFormatVariable, sizeof(long));
50
51 while (1)
52 {
53 emptyFormatService->updateService();
54 usleep(1000000);
55 }
56
57 return 0;
58}
59*/
60
61#include <iostream>
62#include <dis.hxx>
63#ifndef WIN32
64#include <unistd.h>
65#endif
66using namespace std;
67#include <string>
68
69class ErrorHandler : public DimErrorHandler
70{
71 void errorHandler(int severity, int code, char *msg)
72 {
73 int index = 0;
74 char **services;
75 if(code){}
76 cout << severity << " " << msg << endl;
77 services = DimServer::getClientServices();
78 cout<< "from "<< DimServer::getClientName() << " services:" << endl;
79 while(services[index])
80 {
81 cout << services[index] << endl;
82 index++;
83 }
84 }
85public:
86 ErrorHandler() {DimServer::addErrorHandler(this);}
87};
88
89class ExitHandler : public DimExitHandler
90{
91 void exitHandler(int code)
92 {
93 cout << "exit code " << code << endl;
94 }
95public:
96 ExitHandler() {DimServer::addExitHandler(this);}
97};
98
99class CmndServ : public DimCommand, public DimTimer
100{
101 DimService servstr;
102 void commandHandler()
103 {
104 int index = 0;
105 char **services;
106 cout << "Command " << getString() << " received" << endl;
107 servstr.updateService(getString());
108 services = DimServer::getClientServices();
109 cout<< "from "<< DimServer::getClientName() << " services:" << endl;
110 while(services[index])
111 {
112 cout << services[index] << endl;
113 index++;
114 }
115 }
116
117public :
118 CmndServ() : DimCommand("TEST/CMND","C"),
119 servstr("TEST/STRVAL","empty") {};
120/*
121 void handleIt()
122 {
123 int index = 0;
124 char **services;
125 dim_print_date_time();
126 cout << "Command " << getString() << " received" << endl;
127 cout << "time: "<<getTimestamp()<<" millies: "<<getTimestampMillisecs()<<endl;
128 servstr.updateService(getString());
129 }
130*/
131};
132
133/*
134class CmndServMany : public DimCommand
135{
136 void commandHandler()
137 {
138 cout << "Command " << getString() << " received" << endl;
139 }
140public :
141 CmndServMany(char *name) : DimCommand(name,"C") {};
142};
143*/
144
145void add_serv(const int & ival)
146{
147 DimService *abc;
148
149 abc = new DimService("TEST/INTVAL_CONST",(int &)ival);
150}
151
152void add_serv_str(const string & s1)
153{
154 DimService *abc;
155
156 abc = new DimService("TEST/STRINGVAL_CONST",(char *)s1.c_str());
157}
158
159void add_serv_bool(const bool & boolval)
160{
161 DimService *serv;
162
163// serv = new DimService("TEST/BOOLVAL_CONST",(short &)boolval);
164 serv = new DimService("TEST/BOOLVAL_CONST","C:1", (void *)&boolval, 1);
165}
166
167class ServWithHandler : public DimService
168{
169 int value;
170
171 void serviceHandler()
172 {
173 value++;
174// setData(value);
175 }
176public :
177 ServWithHandler(char *name) : DimService(name, value) { value = 0;};
178};
179
180int main()
181{
182 int ival = 0;
183// ErrorHandler errHandler;
184// ExitHandler exHandler;
185// DimServer::setDnsNode("axdes2.cern.ch");
186 string s1;
187 bool boolval;
188 ServWithHandler *testServ;
189 DimServerDns *newDns;
190 char *extraDns = 0;
191 DimService *new_servint;
192
193 DimServer::start("TEST");
194 extraDns = DimUtil::getEnvVar("EXTRA_DNS_NODE");
195 if(extraDns)
196 newDns = new DimServerDns(extraDns, 0, "new_TEST");
197
198/*
199 int i, arr[15000];
200 DimService *servp;
201 DimCommand *cmndp;
202 char str[132];
203*/
204// float farr[4];
205// DimService *farrp;
206
207 s1 = "hello";
208 add_serv(ival);
209 DimService servint("TEST/INTVAL",ival);
210
211 if(extraDns)
212 {
213 new_servint = new DimService(newDns, "new_TEST/INTVAL",ival);
214 }
215
216 add_serv_str(s1);
217 boolval = 0;
218 add_serv_bool(boolval);
219 CmndServ cmdsvr;
220
221 testServ = new ServWithHandler("MY_NEW_TEST_SERVICE_WITH_HANDLER");
222
223// farr[0] = 1.2;
224// farr[1] = 2.3;
225// farrp = new DimService("/PCITCO147/sensors/fan/input","F", farr, sizeof(farr));
226
227
228/*
229// DimServer::autoStartOff();
230 DimServer::start("TEST");
231
232 for(i = 0; i < 15000; i++)
233 {
234 arr[i] = i;
235 sprintf(str,"ServiceManyTest/%05d",i);
236 servp = new DimService(str,arr[i]);
237 servp->setQuality(1);
238 servp->updateService(arr[i]);
239// DimServer::start("TEST");
240 sprintf(str,"CommandManyTest/%05d",i);
241 cmndp = new CmndServMany(str);
242// DimServer::start("TEST");
243 }
244*/
245 while(1)
246 {
247 sleep(5);
248
249/*
250 while(cmdsvr.hasNext())
251 {
252 cmdsvr.getNext();
253 cmdsvr.handleIt();
254 }
255*/
256 s1 = "hello1";
257 if(!boolval)
258 boolval = 1;
259 else
260 boolval = 0;
261 ival++;
262
263 int inCallback = DimServer::inCallback();
264 cout << "main: In callback "<< inCallback << endl;
265 servint.updateService();
266 if(extraDns)
267 new_servint->updateService();
268 }
269 return 0;
270}
271
Note: See TracBrowser for help on using the repository browser.