-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNCKServer.cpp
More file actions
675 lines (551 loc) · 25 KB
/
Copy pathNCKServer.cpp
File metadata and controls
675 lines (551 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/***********************************************************************
NCKServer - Server to run a multi-user version of the Nanotech
Construction Kit.
Copyright (c) 2017-2025 Oliver Kreylos
The Nanotech Construction Kit is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Nanotech Construction Kit is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Nanotech Construction Kit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "NCKServer.h"
#include <unistd.h>
#include <iostream>
#include <Misc/StandardValueCoders.h>
#include <Misc/ConfigurationFile.h>
#include <Misc/MessageLogger.h>
#include <Misc/Marshaller.h>
#include <Misc/CommandDispatcher.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Realtime/Time.h>
#include <Geometry/GeometryValueCoders.h>
#include <Collaboration2/DataType.icpp>
#include <Collaboration2/MessageWriter.h>
#include <Collaboration2/NonBlockSocket.h>
#include <Collaboration2/Server.h>
#include <Collaboration2/Plugins/MetadosisServer.h>
#include "IO.h"
#include "Simulation.h"
#include "Config.h"
namespace Collab {
namespace Plugins {
/**********************************
Methods of class NCKServer::Client:
**********************************/
NCKServer::Client::Client(void)
:pickIdMap(17)
{
}
/**************************
Methods of class NCKServer:
**************************/
void NCKServer::sendMessage(unsigned int clientId,bool broadcast,unsigned int messageId,const void* messageStructure)
{
/* Create a message writer: */
MessageWriter message(MessageBuffer::create(serverMessageBase+messageId,protocolTypes.calcSize(serverMessageTypes[messageId],messageStructure)));
/* Write the message structure into the message: */
protocolTypes.write(serverMessageTypes[messageId],messageStructure,message);
/* Broadcast or send the message: */
if(broadcast)
broadcastMessage(clientId,message.getBuffer());
else
server->queueMessage(clientId,message.getBuffer());
}
void* NCKServer::simulationThreadMethod(void)
{
/* Run the simulation thread until told to shut down: */
Realtime::TimePointMonotonic timer;
while(true)
{
/* Check if the simulation thread should be paused: */
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
/* Check if the simulation thread should go back to sleep after having been woken up for an I/O operation: */
if(pauseSimulationThreadAfterIO)
{
pauseSimulationThread=true;
pauseSimulationThreadAfterIO=false;
}
/* Sleep until woken up: */
while(pauseSimulationThread)
{
pauseSimulationThreadCond.wait(pauseSimulationThreadLock);
/* Reset the simulation timers: */
timer.set();
}
}
/* Bail out if shutting down: */
if(!keepSimulationThreadRunning)
break;
/* Update the simulation to the current time: */
Scalar deltaT(double(timer.setAndDiff()));
sim->advance(deltaT);
/* Sleep until at least the minimum simulation interval has passed: */
Realtime::TimePointMonotonic::sleep(timer+Realtime::TimeVector(0,1000000)); // 1ms minimum update interval
}
return 0;
}
void NCKServer::sessionChangedCallback(SessionID sessionId,void* userData)
{
/* Notify the state machine that the session became valid: */
NCKServer* thisPtr=static_cast<NCKServer*>(userData);
thisPtr->server->getDispatcher().signal(thisPtr->sessionChangedSignalKey,0);
}
void NCKServer::frontendSessionChangedCallback(Threads::EventDispatcher::SignalEvent& event)
{
/* Broadcast a session update notification to all connected clients: */
SessionUpdateNotificationMsg message;
message.sessionId=sim->getSessionId();
message.domain=sim->getDomain();
message.unitTypes=sim->getUnitTypes();
sendMessage(0,true,SessionUpdateNotification,&message);
}
void NCKServer::sendSimulationUpdateCallback(Threads::EventDispatcher::TimerEvent& event)
{
/* Check if there is a new valid simulation state: */
if(sim->lockNewState()&&sim->isLockedStateValid())
{
/* Reduce the locked simulation state for network transmission: */
const UnitStateArray& states=sim->getLockedState();
reducedStates.sessionId=states.sessionId;
reducedStates.timeStamp=states.timeStamp;
reducedStates.states.clear();
reducedStates.states.reserve(states.states.size());
ReducedUnitState r;
for(UnitStateArray::UnitStateList::const_iterator sIt=states.states.begin();sIt!=states.states.end();++sIt)
{
r.set(*sIt);
reducedStates.states.push_back(r);
}
/* Broadcast a simulation state update notification to all connected clients: */
sendMessage(0,true,SimulationUpdateNotification,&reducedStates);
}
}
MessageContinuation* NCKServer::setParametersRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object and its TCP socket: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
/* Read the new simulation parameters and update the simulation: */
SimulationInterface::Parameters newParameters;
protocolTypes.read(socket,clientMessageTypes[SetParametersRequest],&newParameters);
sim->setParameters(newParameters);
/* Forward the new simulation parameters to all other connected clients: */
sendMessage(clientId,true,SetParametersNotification,&newParameters);
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::pointPickRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
PointPickRequestMsg message;
protocolTypes.read(socket,clientMessageTypes[PointPickRequest],&message);
/* Forward the request to the simulation: */
PickID serverPickId=sim->pick(message.pickPosition,message.pickRadius,message.pickOrientation,message.pickConnected);
/* Enter the pick ID pair into the client's active pick map: */
nckClient->pickIdMap[message.pickId]=serverPickId;
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::rayPickRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
RayPickRequestMsg message;
protocolTypes.read(socket,clientMessageTypes[RayPickRequest],&message);
/* Forward the request to the simulation: */
PickID serverPickId=sim->pick(message.pickPosition,message.pickDirection,message.pickOrientation,message.pickConnected);
/* Enter the pick ID pair into the client's active pick map: */
nckClient->pickIdMap[message.pickId]=serverPickId;
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::pasteUnitRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
PasteUnitRequestMsg message;
protocolTypes.read(socket,clientMessageTypes[PasteUnitRequest],&message);
/* Forward the request to the simulation: */
PickID serverPickId=sim->paste(message.position,message.orientation,message.linearVelocity,message.angularVelocity);
/* Enter the pick ID pair into the client's active pick map: */
nckClient->pickIdMap[message.pickId]=serverPickId;
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::createUnitRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
CreateUnitRequestMsg message;
protocolTypes.read(socket,clientMessageTypes[CreateUnitRequest],&message);
/* Forward the request to the simulation: */
PickID serverPickId=nckClient->pickIdMap.getEntry(message.pickId).getDest();
sim->create(serverPickId,message.unitTypeId,message.position,message.orientation,message.linearVelocity,message.angularVelocity);
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::setUnitStateRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
SetUnitStateRequestMsg message;
protocolTypes.read(socket,clientMessageTypes[SetUnitStateRequest],&message);
/* Forward the request to the simulation: */
PickID serverPickId=nckClient->pickIdMap.getEntry(message.pickId).getDest();
sim->setState(serverPickId,message.position,message.orientation,message.linearVelocity,message.angularVelocity);
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::copyUnitRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
PickID clientPickId;
protocolTypes.read(socket,clientMessageTypes[CopyUnitRequest],&clientPickId);
/* Forward the request to the simulation: */
PickID serverPickId=nckClient->pickIdMap.getEntry(clientPickId).getDest();
sim->copy(serverPickId);
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::destroyUnitRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
PickID clientPickId;
protocolTypes.read(socket,clientMessageTypes[DestroyUnitRequest],&clientPickId);
/* Forward the request to the simulation: */
PickID serverPickId=nckClient->pickIdMap.getEntry(clientPickId).getDest();
sim->destroy(serverPickId);
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::releaseRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object, its TCP socket, and the NCK client object: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
Client* nckClient=client->getPlugin<Client>(pluginIndex);
/* Read the request message: */
PickID clientPickId;
protocolTypes.read(socket,clientMessageTypes[ReleaseRequest],&clientPickId);
/* Find the client pick ID in the pick ID map: */
Client::PickIDMap::Iterator pimIt=nckClient->pickIdMap.findEntry(clientPickId);
/* Forward the request to the simulation: */
sim->release(pimIt->getDest());
/* Remove the pick ID from the pick ID map: */
nckClient->pickIdMap.removeEntry(pimIt);
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::loadStateRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
/* Access the base client state object and its TCP socket: */
Server::Client* client=server->getClient(clientId);
NonBlockSocket& socket=client->getSocket();
/* Read the request message: */
MetadosisProtocol::StreamID streamId;
protocolTypes.read(socket,clientMessageTypes[LoadStateRequest],&streamId);
/* Ask the simulation to load the incoming stream: */
sim->loadState(*metadosis->acceptInStream(clientId,streamId));
/* Check if the simulation is currently asleep: */
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
if(pauseSimulationThread)
{
/* Wake up the simulation until the I/O operation is completed: */
pauseSimulationThread=false;
pauseSimulationThreadAfterIO=true;
pauseSimulationThreadCond.signal();
}
}
/* Done with message: */
return 0;
}
MessageContinuation* NCKServer::saveStateRequestCallback(unsigned int messageId,unsigned int clientId,MessageContinuation* continuation)
{
// DEBUGGING
std::cout<<"NCKServer: Received save state request from client"<<std::endl;
/* Create a Metadosis outstream and schedule it to be forwarded to the requesting client: */
MetadosisServer::OutStreamPtr stateStream=metadosis->createOutStream();
MetadosisProtocol::StreamID streamId=metadosis->forwardInStream(clientId,stateStream->getInStream());
/* Notify the client that a state file is coming: */
sendMessage(clientId,false,SaveStateReply,&streamId);
/* Ask the simulation to save to the outstream: */
sim->saveState(*stateStream);
/* Check if the simulation is currently asleep: */
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
if(pauseSimulationThread)
{
/* Wake up the simulation until the I/O operation is completed: */
pauseSimulationThread=false;
pauseSimulationThreadAfterIO=true;
pauseSimulationThreadCond.signal();
}
}
/* Done with message: */
return 0;
}
void NCKServer::setUpdateRateCommandCallback(const char* argumentBegin,const char* argumentEnd)
{
/* Read the requested update rate: */
double updateRate=Misc::ValueCoder<double>::decode(argumentBegin,argumentEnd);
if(updateRate>0.0)
{
/* Set the update rate: */
simulationUpdateRate=updateRate;
/* Check if there are any clients connected: */
if(!clients.empty())
{
/* There is no API to change a running timer's interval, so we have to remove it and then recreate it: */
server->getDispatcher().removeTimerEventListener(sendSimulationUpdateTimerKey);
Threads::EventDispatcher::Time updateInterval(1.0/simulationUpdateRate);
sendSimulationUpdateTimerKey=server->getDispatcher().addTimerEventListener(Threads::EventDispatcher::Time::now(),updateInterval,Threads::EventDispatcher::wrapMethod<NCKServer,&NCKServer::sendSimulationUpdateCallback>,this);
}
}
else
std::cout<<"Invalid simulation update rate "<<updateRate<<" requested"<<std::endl;
}
void NCKServer::loadFileCommandCallback(const char* argumentBegin,const char* argumentEnd)
{
/* Open the requested file: */
std::string fileName(argumentBegin,argumentEnd);
IO::FilePtr file=IO::openFile(fileName.c_str());
file->setEndianness(Misc::LittleEndian);
#if 0
/* Broadcast a session invalid notification to all connected clients: */
{
MessageWriter sessionInvalidNotification(MessageBuffer::create(serverMessageBase+SessionInvalidNotification,0));
broadcastMessage(0,sessionInvalidNotification.getBuffer());
}
#endif
/* Ask the simulation to load the requested file: */
sim->loadState(*file);
/* Check if the simulation is currently asleep: */
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
if(pauseSimulationThread)
{
/* Wake up the simulation until the I/O operation is completed: */
pauseSimulationThread=false;
pauseSimulationThreadAfterIO=true;
pauseSimulationThreadCond.signal();
}
}
}
void NCKServer::saveFileCommandCallback(const char* argumentBegin,const char* argumentEnd)
{
/* Open the requested file: */
std::string fileName(argumentBegin,argumentEnd);
IO::FilePtr file=IO::openFile(fileName.c_str(),IO::File::WriteOnly);
file->setEndianness(Misc::LittleEndian);
/* Ask the simulation to save current state to the requested file: */
sim->saveState(*file);
/* Check if the simulation is currently asleep: */
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
if(pauseSimulationThread)
{
/* Wake up the simulation until the I/O operation is completed: */
pauseSimulationThread=false;
pauseSimulationThreadAfterIO=true;
pauseSimulationThreadCond.signal();
}
}
}
NCKServer::NCKServer(Server* sServer)
:PluginServer(sServer),
metadosis(MetadosisServer::requestServer(server)),
simulationUpdateRate(60),
sim(0),
keepSimulationThreadRunning(false),pauseSimulationThread(true),pauseSimulationThreadAfterIO(false),
sessionChangedSignalKey(0),sendSimulationUpdateTimerKey(0)
{
/* Depend on Metadosis protocol: */
metadosis->addDependentPlugin(this);
/* Read server-side configuration: */
Misc::ConfigurationFileSection serverConfig=server->getPluginConfig(this);
serverConfig.updateValue("./simulationUpdateRate",simulationUpdateRate);
Box domain(Point::origin,Point(100,100,100));
serverConfig.updateValue("./domain",domain);
/* Open the main configuration file: */
Misc::ConfigurationFile configFile(NCK_CONFIG_ETCDIR "/" NCK_CONFIG_CONFIGFILENAME);
Misc::ConfigurationFileSection rootSection=configFile.getSection("NewNanotechConstructionKit");
/* Create the simulation object: */
sim=new Simulation(rootSection,domain);
/* Install a callback to be notified when the simulation session changed: */
sim->setSessionChangedCallback(&NCKServer::sessionChangedCallback,this);
/* Register a signal with the server's event dispatcher: */
sessionChangedSignalKey=server->getDispatcher().addSignalListener(Threads::EventDispatcher::wrapMethod<NCKServer,&NCKServer::frontendSessionChangedCallback>,this);
/* Register pipe commands: */
server->getCommandDispatcher().addCommandCallback("NCK::setUpdateRate",Misc::CommandDispatcher::wrapMethod<NCKServer,&NCKServer::setUpdateRateCommandCallback>,this,"<update rate in Hz>","Sets the rate at which state updates are sent to clients");
server->getCommandDispatcher().addCommandCallback("NCK::loadFile",Misc::CommandDispatcher::wrapMethod<NCKServer,&NCKServer::loadFileCommandCallback>,this,"<unit file name>","Loads the NCK unit file of the given name");
server->getCommandDispatcher().addCommandCallback("NCK::saveFile",Misc::CommandDispatcher::wrapMethod<NCKServer,&NCKServer::saveFileCommandCallback>,this,"<unit file name>","Saves the current simulation state to an NCK unit file of the given name");
}
NCKServer::~NCKServer(void)
{
if(!simulationThread.isJoined())
{
/* Stop the simulation thread: */
keepSimulationThreadRunning=false;
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
pauseSimulationThread=false;
pauseSimulationThreadCond.signal();
}
simulationThread.join();
}
/* Delete the simulation object: */
delete sim;
/* Remove all event listeners: */
server->getDispatcher().removeSignalListener(sessionChangedSignalKey);
if(sendSimulationUpdateTimerKey!=0)
server->getDispatcher().removeTimerEventListener(sendSimulationUpdateTimerKey);
/* Remove the pipe command: */
server->getCommandDispatcher().removeCommandCallback("NCK::setUpdateRate");
server->getCommandDispatcher().removeCommandCallback("NCK::loadFile");
server->getCommandDispatcher().removeCommandCallback("NCK::saveFile");
/* Release dependence on Metadosis protocol: */
metadosis->removeDependentPlugin(this);
}
const char* NCKServer::getName(void) const
{
return protocolName;
}
unsigned int NCKServer::getVersion(void) const
{
return protocolVersion;
}
unsigned int NCKServer::getNumClientMessages(void) const
{
return NumClientMessages;
}
unsigned int NCKServer::getNumServerMessages(void) const
{
return NumServerMessages;
}
void NCKServer::setMessageBases(unsigned int newClientMessageBase,unsigned int newServerMessageBase)
{
/* Call the base class method: */
PluginServer::setMessageBases(newClientMessageBase,newServerMessageBase);
/* Register message handlers: */
server->setMessageHandler(clientMessageBase+SetParametersRequest,Server::wrapMethod<NCKServer,&NCKServer::setParametersRequestCallback>,this,getClientMsgSize(SetParametersRequest));
server->setMessageHandler(clientMessageBase+PointPickRequest,Server::wrapMethod<NCKServer,&NCKServer::pointPickRequestCallback>,this,getClientMsgSize(PointPickRequest));
server->setMessageHandler(clientMessageBase+RayPickRequest,Server::wrapMethod<NCKServer,&NCKServer::rayPickRequestCallback>,this,getClientMsgSize(RayPickRequest));
server->setMessageHandler(clientMessageBase+PasteUnitRequest,Server::wrapMethod<NCKServer,&NCKServer::pasteUnitRequestCallback>,this,getClientMsgSize(PasteUnitRequest));
server->setMessageHandler(clientMessageBase+CreateUnitRequest,Server::wrapMethod<NCKServer,&NCKServer::createUnitRequestCallback>,this,getClientMsgSize(CreateUnitRequest));
server->setMessageHandler(clientMessageBase+SetUnitStateRequest,Server::wrapMethod<NCKServer,&NCKServer::setUnitStateRequestCallback>,this,getClientMsgSize(SetUnitStateRequest));
server->setMessageHandler(clientMessageBase+CopyUnitRequest,Server::wrapMethod<NCKServer,&NCKServer::copyUnitRequestCallback>,this,getClientMsgSize(CopyUnitRequest));
server->setMessageHandler(clientMessageBase+DestroyUnitRequest,Server::wrapMethod<NCKServer,&NCKServer::destroyUnitRequestCallback>,this,getClientMsgSize(DestroyUnitRequest));
server->setMessageHandler(clientMessageBase+ReleaseRequest,Server::wrapMethod<NCKServer,&NCKServer::releaseRequestCallback>,this,getClientMsgSize(ReleaseRequest));
server->setMessageHandler(clientMessageBase+LoadStateRequest,Server::wrapMethod<NCKServer,&NCKServer::loadStateRequestCallback>,this,getClientMsgSize(LoadStateRequest));
server->setMessageHandler(clientMessageBase+SaveStateRequest,Server::wrapMethod<NCKServer,&NCKServer::saveStateRequestCallback>,this,getClientMsgSize(SaveStateRequest));
}
void NCKServer::start(void)
{
/* Start the simulation thread in paused mode: */
keepSimulationThreadRunning=true;
simulationThread.start(this,&NCKServer::simulationThreadMethod);
}
void NCKServer::clientConnected(unsigned int clientId)
{
/* Check if this is the first client to connect: */
if(clients.empty())
{
/* Unpause the simulation thread: */
Misc::logNote("NCK: Unpausing simulation thread");
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
pauseSimulationThread=false;
pauseSimulationThreadCond.signal();
}
/* Add an event listener for regular simulation state update messages: */
Threads::EventDispatcher::Time updateInterval(1.0/simulationUpdateRate);
sendSimulationUpdateTimerKey=server->getDispatcher().addTimerEventListener(Threads::EventDispatcher::Time::now(),updateInterval,Threads::EventDispatcher::wrapMethod<NCKServer,&NCKServer::sendSimulationUpdateCallback>,this);
}
/* Add the new client to the list of clients using this protocol: */
addClientToList(clientId);
/* Associate a client structure with the new client: */
Server::Client* client=server->getClient(clientId);
client->setPlugin(pluginIndex,new Client);
/* Send the current simulation parameters to the new client: */
sendMessage(clientId,false,SetParametersNotification,&sim->getParameters());
/* Check if the simulation session is valid: */
if(sim->isSessionValid())
{
/* Send a session update notification to the new client: */
SessionUpdateNotificationMsg message;
message.sessionId=sim->getSessionId();
message.domain=sim->getDomain();
message.unitTypes=sim->getUnitTypes();
sendMessage(clientId,false,SessionUpdateNotification,&message);
}
}
void NCKServer::clientDisconnected(unsigned int clientId)
{
/* Remove the client from the list of clients using this protocol: */
removeClientFromList(clientId);
/* Stop the client's remaining active drag operations: */
Server::Client* client=server->getClient(clientId);
Client* nckClient=client->getPlugin<Client>(pluginIndex);
for(Client::PickIDMap::Iterator pimIt=nckClient->pickIdMap.begin();!pimIt.isFinished();++pimIt)
sim->release(pimIt->getDest());
/* Check if this is the last client to disconnect: */
if(clients.empty())
{
/* Pause the simulation thread: */
Misc::logNote("NCK: Pausing simulation thread");
{
Threads::MutexCond::Lock pauseSimulationThreadLock(pauseSimulationThreadCond);
pauseSimulationThread=true;
}
/* Remove the simulation update event listener: */
server->getDispatcher().removeTimerEventListener(sendSimulationUpdateTimerKey);
}
}
/***********************
DSO loader entry points:
***********************/
extern "C" {
PluginServer* createObject(PluginServerLoader& objectLoader,Server* server)
{
return new NCKServer(server);
}
void destroyObject(PluginServer* object)
{
delete object;
}
}
}
}