1 | #include <dic.hxx>
|
---|
2 | #include <dis.hxx>
|
---|
3 | #include <dim.h>
|
---|
4 | #include <iostream>
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | static int no_link = 0xdeaddead;
|
---|
8 | static char from_node[64], to_node[64], bridge_name[64];
|
---|
9 |
|
---|
10 | class BridgeService: public DimInfo, public SLLItem
|
---|
11 | {
|
---|
12 | char srv_name[256];
|
---|
13 | char srv_format[256];
|
---|
14 | int declared;
|
---|
15 | DimService *srv;
|
---|
16 | void *srv_data, *data;
|
---|
17 | int srv_size;
|
---|
18 | int cmnd;
|
---|
19 | int found;
|
---|
20 | int copyFlag;
|
---|
21 |
|
---|
22 | void infoHandler() {
|
---|
23 | char *server;
|
---|
24 | data = DimInfo::getData();
|
---|
25 | srv_size = DimInfo::getSize();
|
---|
26 |
|
---|
27 | // Sometimes gets a packet from DIS_DNS not understood why!!!"
|
---|
28 | server = DimClient::getServerName();
|
---|
29 | if(strstr(server,"DIS_DNS") != 0)
|
---|
30 | {
|
---|
31 | dim_print_date_time();
|
---|
32 | cout << "received from " << server << " size = " << srv_size << endl;
|
---|
33 | if(strstr(srv_name,"DIS_DNS") == 0)
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | srv_data = data;
|
---|
37 | if(*(int *)srv_data == no_link)
|
---|
38 | {
|
---|
39 | if(srv)
|
---|
40 | {
|
---|
41 | delete(srv);
|
---|
42 | srv = 0;
|
---|
43 | }
|
---|
44 | declared = 0;
|
---|
45 | // cout << "Disconnecting bridge for: " << srv_name << endl;
|
---|
46 | }
|
---|
47 | else if(!declared)
|
---|
48 | {
|
---|
49 | // DimServer::setDnsNode(to_node);
|
---|
50 | srv = new DimService(srv_name, srv_format, srv_data, srv_size);
|
---|
51 | if(copyFlag)
|
---|
52 | srv->setData(srv_data, srv_size);
|
---|
53 | DimServer::start(bridge_name);
|
---|
54 | declared = 1;
|
---|
55 | // DimClient::setDnsNode(from_node);
|
---|
56 | }
|
---|
57 | else
|
---|
58 | {
|
---|
59 | if(srv)
|
---|
60 | {
|
---|
61 | if(copyFlag)
|
---|
62 | {
|
---|
63 | srv->setData(srv_data, srv_size);
|
---|
64 | srv->updateService();
|
---|
65 | }
|
---|
66 | else
|
---|
67 | {
|
---|
68 | srv->updateService(srv_data, srv_size);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public:
|
---|
75 | BridgeService(char *name, char *format, int copy):
|
---|
76 | DimInfo(name, &no_link, sizeof(no_link)), declared(0), srv(0), copyFlag(copy)
|
---|
77 | {
|
---|
78 | strcpy(srv_format, format);
|
---|
79 | strcpy(srv_name, name);
|
---|
80 | cmnd = 0;
|
---|
81 | found = 1;
|
---|
82 | // cout << "Bridging Service: " << name << endl;
|
---|
83 | }
|
---|
84 | BridgeService(char *name, char *format, int rate, int copy):
|
---|
85 | DimInfo(name, rate, &no_link, sizeof(no_link)), declared(0), srv(0), copyFlag(copy)
|
---|
86 | {
|
---|
87 | strcpy(srv_format, format);
|
---|
88 | strcpy(srv_name, name);
|
---|
89 | cmnd = 0;
|
---|
90 | found = 1;
|
---|
91 | // cout << "Bridging Service: " << name << ", rate = " << rate << " seconds" << endl;
|
---|
92 | }
|
---|
93 | ~BridgeService()
|
---|
94 | {
|
---|
95 | if(declared)
|
---|
96 | {
|
---|
97 | if(srv)
|
---|
98 | {
|
---|
99 | delete(srv);
|
---|
100 | srv = 0;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | declared = 0;
|
---|
104 | // cout << "Stopped bridge for: " << srv_name << endl;
|
---|
105 | }
|
---|
106 | char *getName() { return srv_name; };
|
---|
107 | void clear() { found = 0;};
|
---|
108 | void set() {found = 1;};
|
---|
109 | int find() {return found;};
|
---|
110 | int isCmnd() { return cmnd;};
|
---|
111 | };
|
---|
112 |
|
---|
113 | class BridgeCommand: public DimCommand, public SLLItem
|
---|
114 | {
|
---|
115 | char srv_name[256];
|
---|
116 | char srv_format[256];
|
---|
117 | int declared;
|
---|
118 | DimService *srv;
|
---|
119 | void *srv_data;
|
---|
120 | int srv_size;
|
---|
121 | int cmnd;
|
---|
122 | int found;
|
---|
123 |
|
---|
124 | void commandHandler() {
|
---|
125 | srv_data = DimCommand::getData();
|
---|
126 | srv_size = DimCommand::getSize();
|
---|
127 | DimClient::sendCommandNB(srv_name, srv_data, srv_size);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public:
|
---|
131 | BridgeCommand(char *name, char *format):
|
---|
132 | DimCommand(name, format)
|
---|
133 | {
|
---|
134 | DimServer::start(bridge_name);
|
---|
135 | cmnd = 1;
|
---|
136 | found = 1;
|
---|
137 | strcpy(srv_name, name);
|
---|
138 | // cout << "Bridging Command: " << name << endl;
|
---|
139 | }
|
---|
140 | char *getName() { return srv_name; };
|
---|
141 | void clear() { found = 0;};
|
---|
142 | void set() {found = 1;};
|
---|
143 | int find() {return found;};
|
---|
144 | int isCmnd() { return cmnd;};
|
---|
145 | };
|
---|
146 |
|
---|
147 | void print_usage()
|
---|
148 | {
|
---|
149 | cout << "Usage: DimBridge [from_node] to_node services [time_interval] [-copy]" << endl;
|
---|
150 | cout << " from_node - by default DIM_DNS_NODE" << endl;
|
---|
151 | cout << " to_node - the complete node name of the new DNS" << endl;
|
---|
152 | cout << " services - the list of service names (wildcards allowed)" << endl;
|
---|
153 | cout << " time_interval - the interval in seconds to be used for updating the services" << endl;
|
---|
154 | cout << " -copy - copy internally the service data" << endl;
|
---|
155 | }
|
---|
156 |
|
---|
157 | int main(int argc, char **argv)
|
---|
158 | {
|
---|
159 | char services[132];
|
---|
160 | DimBrowser dbr;
|
---|
161 | char *service, *format, *p;
|
---|
162 | int type, known;
|
---|
163 | BridgeService *ptrs, *aux_ptrs;
|
---|
164 | BridgeCommand *ptrc, *aux_ptrc;
|
---|
165 | SLList lists, listc;
|
---|
166 | int rate = 0;
|
---|
167 | int copyFlag = 0;
|
---|
168 |
|
---|
169 | //dic_set_debug_on();
|
---|
170 | if(argc < 3)
|
---|
171 | {
|
---|
172 | print_usage();
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 | else if( argc == 3)
|
---|
176 | {
|
---|
177 | strcpy(from_node, DimClient::getDnsNode());
|
---|
178 | strcpy(to_node, argv[1]);
|
---|
179 | strcpy(services, argv[2]);
|
---|
180 | }
|
---|
181 | else if (argc == 4)
|
---|
182 | {
|
---|
183 | if(sscanf(argv[3],"%d", &rate))
|
---|
184 | {
|
---|
185 | strcpy(from_node, DimClient::getDnsNode());
|
---|
186 | strcpy(to_node, argv[1]);
|
---|
187 | strcpy(services, argv[2]);
|
---|
188 | }
|
---|
189 | else if(argv[3][0] == '-')
|
---|
190 | {
|
---|
191 | rate = 0;
|
---|
192 | strcpy(from_node, DimClient::getDnsNode());
|
---|
193 | strcpy(to_node, argv[1]);
|
---|
194 | strcpy(services, argv[2]);
|
---|
195 | copyFlag = 1;
|
---|
196 | }
|
---|
197 | else
|
---|
198 | {
|
---|
199 | rate = 0;
|
---|
200 | strcpy(from_node, argv[1]);
|
---|
201 | strcpy(to_node, argv[2]);
|
---|
202 | strcpy(services, argv[3]);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | else if(argc == 5)
|
---|
206 | {
|
---|
207 | if(sscanf(argv[4],"%d", &rate))
|
---|
208 | {
|
---|
209 | strcpy(from_node, argv[1]);
|
---|
210 | strcpy(to_node, argv[2]);
|
---|
211 | strcpy(services, argv[3]);
|
---|
212 | }
|
---|
213 | else if(argv[4][0] == '-')
|
---|
214 | {
|
---|
215 | copyFlag = 1;
|
---|
216 | if(sscanf(argv[3],"%d", &rate))
|
---|
217 | {
|
---|
218 | strcpy(from_node, DimClient::getDnsNode());
|
---|
219 | strcpy(to_node, argv[1]);
|
---|
220 | strcpy(services, argv[2]);
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | rate = 0;
|
---|
225 | strcpy(from_node, argv[1]);
|
---|
226 | strcpy(to_node, argv[2]);
|
---|
227 | strcpy(services, argv[3]);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else if(argc == 6)
|
---|
232 | {
|
---|
233 | strcpy(from_node, argv[1]);
|
---|
234 | strcpy(to_node, argv[2]);
|
---|
235 | strcpy(services, argv[3]);
|
---|
236 | sscanf(argv[4],"%d", &rate);
|
---|
237 | copyFlag = 1;
|
---|
238 | }
|
---|
239 | else
|
---|
240 | {
|
---|
241 | cout << "Bad parameters" << endl;
|
---|
242 | return 0;
|
---|
243 | }
|
---|
244 |
|
---|
245 | cout << "Starting DimBridge from "<<from_node<<" to "<<to_node<<" for "<< services;
|
---|
246 | if(rate)
|
---|
247 | cout << " interval=" << rate;
|
---|
248 | if(copyFlag)
|
---|
249 | cout << " (internal data copy)";
|
---|
250 | cout << endl;
|
---|
251 |
|
---|
252 | strcpy(bridge_name,"Bridge_");
|
---|
253 | strcat(bridge_name, from_node);
|
---|
254 | if( (p = strchr(bridge_name,'.')) )
|
---|
255 | *p = '\0';
|
---|
256 | #ifndef WIN32
|
---|
257 | sprintf(p,"_%d",getpid());
|
---|
258 | #else
|
---|
259 | sprintf(p,"_%d",_getpid());
|
---|
260 | #endif
|
---|
261 | DimClient::setDnsNode(from_node);
|
---|
262 | DimServer::setDnsNode(to_node);
|
---|
263 | while(1)
|
---|
264 | {
|
---|
265 | ptrs = (BridgeService *)lists.getHead();
|
---|
266 | while(ptrs)
|
---|
267 | {
|
---|
268 | ptrs->clear();
|
---|
269 | ptrs = (BridgeService *)lists.getNext();
|
---|
270 | }
|
---|
271 | ptrc = (BridgeCommand *)listc.getHead();
|
---|
272 | while(ptrc)
|
---|
273 | {
|
---|
274 | ptrc->clear();
|
---|
275 | ptrc = (BridgeCommand *)listc.getNext();
|
---|
276 | }
|
---|
277 | dbr.getServices(services);
|
---|
278 | while( (type = dbr.getNextService(service, format)) )
|
---|
279 | {
|
---|
280 | known = 0;
|
---|
281 | ptrs = (BridgeService *)lists.getHead();
|
---|
282 | while(ptrs)
|
---|
283 | {
|
---|
284 | if(!strcmp(ptrs->getName(), service))
|
---|
285 | {
|
---|
286 | known = 1;
|
---|
287 | ptrs->set();
|
---|
288 | break;
|
---|
289 | }
|
---|
290 | ptrs = (BridgeService *)lists.getNext();
|
---|
291 | }
|
---|
292 | ptrc = (BridgeCommand *)listc.getHead();
|
---|
293 | while(ptrc)
|
---|
294 | {
|
---|
295 | if(!strcmp(ptrc->getName(), service))
|
---|
296 | {
|
---|
297 | known = 1;
|
---|
298 | ptrc->set();
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | ptrc = (BridgeCommand *)listc.getNext();
|
---|
302 | }
|
---|
303 | if(strstr(service,"DIS_DNS"))
|
---|
304 | known = 1;
|
---|
305 | if(!known)
|
---|
306 | {
|
---|
307 | if(type == DimSERVICE)
|
---|
308 | {
|
---|
309 | if(!rate)
|
---|
310 | ptrs = new BridgeService(service, format, copyFlag);
|
---|
311 | else
|
---|
312 | ptrs = new BridgeService(service, format, rate, copyFlag);
|
---|
313 | lists.add(ptrs);
|
---|
314 | }
|
---|
315 | else if (type == DimCOMMAND)
|
---|
316 | {
|
---|
317 | // DimClient::setDnsNode(to_node);
|
---|
318 | ptrc = new BridgeCommand(service, format);
|
---|
319 | listc.add(ptrc);
|
---|
320 | // DimClient::setDnsNode(from_node);
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 | ptrs = (BridgeService *)lists.getHead();
|
---|
325 | while(ptrs)
|
---|
326 | {
|
---|
327 | aux_ptrs = 0;
|
---|
328 | if(!ptrs->find())
|
---|
329 | {
|
---|
330 | lists.remove(ptrs);
|
---|
331 | aux_ptrs = ptrs;
|
---|
332 | }
|
---|
333 | ptrs = (BridgeService *)lists.getNext();
|
---|
334 | if(aux_ptrs)
|
---|
335 | {
|
---|
336 | delete aux_ptrs;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | ptrc = (BridgeCommand *)listc.getHead();
|
---|
340 | while(ptrc)
|
---|
341 | {
|
---|
342 | aux_ptrc = 0;
|
---|
343 | if(!ptrc->find())
|
---|
344 | {
|
---|
345 | listc.remove(ptrc);
|
---|
346 | aux_ptrc = ptrc;
|
---|
347 | }
|
---|
348 | ptrc = (BridgeCommand *)listc.getNext();
|
---|
349 | if(aux_ptrc)
|
---|
350 | {
|
---|
351 | delete aux_ptrc;
|
---|
352 | }
|
---|
353 | }
|
---|
354 | sleep(5);
|
---|
355 | }
|
---|
356 | return 1;
|
---|
357 | }
|
---|