-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
444 lines (361 loc) · 11.7 KB
/
Server.cpp
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
/************************************************************************
* DICOMLIB
* Copyright 2003 Sunnybrook and Women's College Health Science Center
* Implemented by Trevor Morgan ([email protected])
*
* See LICENSE.txt for copyright and licensing info.
*************************************************************************/
/*
Design note.
We make frequent use of callbacks here, implemented using the boost::function library.
This is necessary in a multithreaded, event-driven environment. Basically we're saying
'when event X occurs, call function Y'. Y could be a callback or an overriden virtual function.
In most cases we could use callbacks or virtual functions, and it's a question of choosing
which is simpler. Callbacks make sense for the dicom command handlers, as it means
we can dynamically change the command handlers available by modifying a map of
callbacks; the basic structure is this:
std::map<UID,HandlerFunction> Handlers_;
*/
#include <exception>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include "Server.hpp"
#include "UIDs.hpp"
#include "ImplementationUID.hpp"
#include "pdata.hpp"
#include "TransferSyntax.hpp"
#include "Decoder.hpp"
#include "Cdimse.hpp"
#include "UID.hpp"
#include "ServiceBase.hpp"
#include "ThreadSpecificServer.hpp"
using std::string;using std::cout; using std::endl;
namespace dicom
{
using namespace primitive;
boost::mutex Server::cerr_mutex;
boost::mutex Server::cout_mutex;
void Server::Logger::LogError(std::string Error)
{
boost::mutex::scoped_lock lock(cerr_mutex);
std::cerr << Error << std::endl;
}
void Server::Logger::LogMessage (std::string Message)
{
boost::mutex::scoped_lock lock(cout_mutex);
std::cout << Message << std::endl;
}
Server::Server()
:ServerThread_(NULL)
,KillFlag(false)
,CurrentLogger_(&DefaultLogger_)
{
//is the following line a standard requirment???
//AcceptableAbstractSyntaxes_.insert(UID_VERIFICATION_SOP_CLASS);
}
Server::~Server()
{
this->Stop();
}
namespace
{
/*
If this works nicely, the next step would be to maybe ACCEPT the socket in THIS thread
and destroy it at the end of the function.
*/
void theThreadFunction(Network::AcceptedSocket* socket,Server& server)
{
Implementation::ThreadSpecificServer threadServer(socket,server);
threadServer();
server.allDone();
}
}
/*!
Indicate that the currently executing thread can be safely deleted.
We do this by comparing thread identifiers in a cross-platform way via thread::operator ==
*/
void Server::allDone()
{
boost::mutex::scoped_lock scoped_lock(mutex_);
//boost::thread currentThread; //Default constructor get current thread of execution. //Not true anymore in boost 1_39_0 -Sam
boost::thread::id currentThreadId=boost::this_thread::get_id(); //New practice in boost 1_39_0. -Sam
for(ThreadGroup::iterator i=clientThreads_.begin();i!=clientThreads_.end();i++)
{
if((i->first->get_id())==currentThreadId)
{
i->second=true;//ie, ready for cleanup
return;
}
}
//if we get this far, something horrible has happened!
throw std::runtime_error("Current thread not managed by clientThreads_!");
}
/*!
If we specify cleanAll, then we'll wait for each thread to finish and clean it up.
Otherwise we'll just clean up threads that have reported themselves to be finished.
*/
void Server::threadCleanup(bool cleanAll)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
for(ThreadGroup::iterator i=clientThreads_.begin();i!=clientThreads_.end();)
{
bool threadDone=i->second;
if(threadDone || cleanAll)
{
i->first->join();
clientThreads_.erase(i++); //only iterator being erased gets invalidated, so this is ok.
}
else
++i;
}
}
//!Start listening for connections on 'port'
/*!
Note that this runs in the current thread, and will not exit until the kill
flag is raised. If you want the server to run in the background, use
ServeInNewThread()
*/
void Server::Serve(short port)
{
//PRECONDITIONS:Server is not running.
//here's where we ask the framework to start listening
//on the relevant port, and start up server threads
//as needed.
if(!clientThreads_.empty())
throw std::runtime_error("client threads currently running!");
//Open a socket to listen for new connections.
Network::ServerSocket TheServerSocket(port);
while(ClientConnectionPending(&TheServerSocket))
{
threadCleanup(false);
Network::AcceptedSocket* pAccepter=new Network::AcceptedSocket(TheServerSocket);//blocks, waiting for a client.
boost::shared_ptr<boost::thread> pThread(new boost::thread(boost::bind(&theThreadFunction,pAccepter,boost::ref(*this))));
{
boost::mutex::scoped_lock scoped_lock(mutex_);
clientThreads_.insert(ThreadGroup::value_type(pThread,false));
}
//Note that the socket object must be deleted in the newly created thread.
//see comments in ThreadSpecificServer::operator()
}
//if we get here, the kill flag has been raised, so wait for
//all threads to terminate nicely...
threadCleanup(true);
}
//!Functor that gets passed to new thread.
/*!
Gets called by newly created server thread, and runs
server_ in new thread.
*/
struct ServerThreadStarter
{
Server& server_;
short port_;
ServerThreadStarter(Server& server,short port):server_(server),port_(port){}
void operator()()
{
server_.Serve(port_);
}
};
//!Starts Server running in new thread.
/*!
This is useful if your application is more than just a DICOM
server (for example, a GUI app that can receive DICOM C-STORE
requests) as it enables you to run a server on a different thread
than the main event loop.
*/
void Server::ServeInNewThread(short port)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
if(ServerThread_)
return;//we're already doing it....
KillFlag=false;//Sam add this so that server can be restarted. 8April2009
ServerThreadStarter s(*this,port);
ServerThread_=new boost::thread(s);
}
//!This is kind of pointless as there is only one application context acceptable in the standard.
bool Server::IsAcceptableApplicationContext(const UID& uid)
{
return (uid==APPLICATION_CONTEXT);
}
/*
Callback setting functions.
*/
void Server::SetCheckLocalAETCallback(StringCheckFunction f)
{
boost::mutex::scoped_lock scoped_lock(AETMutex_);
CheckLocalAET=f;
}
void Server::SetCheckRemoteAETCallback(StringCheckFunction2 f)
{
boost::mutex::scoped_lock scoped_lock(AETMutex_);
CheckRemoteAET=f;
}
void Server::AssociationNegotiated(const primitive::AAssociateRQ& request)
{
return CurrentLogger_->AssociationNegotiated(request);
}
void Server::AssociationTerminated()
{
CurrentLogger_->AssociationTerminated();
}
bool Server::IsAcceptableRemoteApplicationTitle(const std::string& title,std::string ip)
{
boost::mutex::scoped_lock scoped_lock(AETMutex_);
return CheckRemoteAET.empty()?
false
:
CheckRemoteAET(title,ip);
}
bool Server::IsAcceptableLocalApplicationTitle(const std::string& title)
{
boost::mutex::scoped_lock scoped_lock(AETMutex_);
return CheckLocalAET.empty()?
false
:
CheckLocalAET(title);
}
void Server::AddHandler(const UID& uid,HandlerFunction Handler)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
Handlers_[uid]=Handler;
}
void Server::AddFindHandler(const UID& uid,CFindFunction Handler)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
FindHandlers_[uid]=Handler;
}
CFindFunction Server::GetFindHandler(const UID& uid)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
std::map<UID,CFindFunction>::iterator I = FindHandlers_.find(uid);
if(I==FindHandlers_.end())
{
LogError("No available handler.");
throw NoAvailableHandler();//or something
}
else
return I->second;
}
HandlerFunction Server::GetHandler(const UID& uid)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
std::map<UID,HandlerFunction>::iterator I = Handlers_.find(uid);
if(I==Handlers_.end())
{
LogError("No available handler.");
throw NoAvailableHandler();
}
else
return I->second;
}
//!i.e, "Do we have a function capable of handling a command of type uid"
bool Server::IsAcceptableAbstractSyntax(const UID& uid)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
if((Handlers_.find(uid)!=Handlers_.end()) || (FindHandlers_.find(uid)!=FindHandlers_.end()))
return true;
if(VERIFICATION_SOP_CLASS==uid)
return true;//we accept this by default.
return false;
//return (Handlers_.find(uid)!=Handlers_.end());
}
/*
currently only support Implicit VR/LittleEndian, but there's
no real technical reason why we couldn't support the others if
needed.
*/
bool Server::CanHandleTransferSyntax(TransferSyntax &TrnSyntax)
{
boost::mutex::scoped_lock scoped_lock(mutex_);
if(TrnSyntax.UID_ == IMPL_VR_LE_TRANSFER_SYNTAX)
return true;
//I think we should also publish that we can handle Explicit Transfer syntax as well, yes no?
if(TrnSyntax.UID_ == EXPL_VR_LE_TRANSFER_SYNTAX)
return true;
return (false);
}
void Server::GetImplementationClass(ImplementationClass &ImpClass)
{
boost::mutex::scoped_lock scoped_lock(mutex_);//do we really need this?
ImpClass.UID_=ImplementationClassUID;
}
void Server::GetImplementationVersion(ImplementationVersion &ImpVersion)
{
boost::mutex::scoped_lock scoped_lock(mutex_);//not sure we really need this.
ImpVersion.Name=ImplementationVersionName;
}
/*!
Waits for a connection to be made, checking every few seconds
for KillFlagRaised.
*/
bool Server::ClientConnectionPending(Network::Socket* pSocket)
{
for(;;)
{
fd_set rfds;//need to reset all this stuff after each call to select.
timeval tv;
FD_ZERO(&rfds);
FD_SET(pSocket->GetSocketDescriptor(), &rfds);
/* Wait up to five seconds. We may want to tune this value.*/
tv.tv_sec = 5;
tv.tv_usec = 0;
if(KillFlagRaised())
{
LogMessage("Kill flag raised...");
break;
}
//select() examines the state of the socket.
int retval = select((int)pSocket->GetSocketDescriptor()+1, &rfds, NULL, NULL, &tv);
if(KillFlagRaised())
break;
if(retval==-1)
throw SystemError("Select");
if (retval)
return true;
}
LogMessage ("Kill flag raised, accepting no more connections.");
return false;
}
/*!
presumably should call RaiseKillFlag and wait until all
threads are stopped?
Should it raise an error if it's not running?
*/
void Server::Stop()
{
boost::mutex::scoped_lock scoped_lock(mutex_);
RaiseKillFlag();
if(ServerThread_)
{
//ServerThread_->join();//Sam comments this out because it deadlock the server all the time.
//The discussion on the web thinks this is a problem only when
//windows are used. But I found it also happens in command line
//programs. -Sam 8Apr2009
delete ServerThread_;
ServerThread_=NULL;
}
}
bool Server::KillFlagRaised()
{
boost::mutex::scoped_lock lock(killflag_mutex);
return KillFlag;
}
void Server::RaiseKillFlag()
{
boost::mutex::scoped_lock lock(killflag_mutex);
KillFlag=true;
}
void Server::LogError(std::string Error)
{
CurrentLogger_->LogError(Error);
}
void Server::LogMessage(std::string Message)
{
CurrentLogger_->LogMessage(Message);
}
void Server::SetLogger(Logger* logger)
{
CurrentLogger_=logger;
}
}//namespace dicom