comparison OrthancServer/DicomProtocol/DicomServer.cpp @ 1675:131136aeeaa7 db-changes

improved exception handling in the main program
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Oct 2015 13:57:54 +0200
parents d3ba98d6b6e9
children f079f3efe33b
comparison
equal deleted inserted replaced
1674:4fc502d469f4 1675:131136aeeaa7
56 56
57 //std::set< 57 //std::set<
58 }; 58 };
59 59
60 60
61 void DicomServer::ServerThread(DicomServer* server) 61 void DicomServer::ServerThread(DicomServer* server,
62 { 62 T_ASC_Network *network)
63 /* initialize network, i.e. create an instance of T_ASC_Network*. */ 63 {
64 T_ASC_Network *net; 64 LOG(INFO) << "DICOM server started";
65 OFCondition cond = ASC_initializeNetwork 65
66 (NET_ACCEPTOR, OFstatic_cast(int, server->port_), /*opt_acse_timeout*/ 30, &net); 66 while (server->continue_)
67 if (cond.bad()) 67 {
68 { 68 /* receive an association and acknowledge or reject it. If the association was */
69 LOG(ERROR) << "cannot create network: " << cond.text(); 69 /* acknowledged, offer corresponding services and invoke one or more if required. */
70 throw OrthancException(ErrorCode_DicomPortInUse); 70 std::auto_ptr<Internals::CommandDispatcher> dispatcher(Internals::AcceptAssociation(*server, network));
71 } 71
72 72 try
73 LOG(INFO) << "DICOM server started";
74
75 server->started_ = true;
76
77 while (server->continue_)
78 {
79 /* receive an association and acknowledge or reject it. If the association was */
80 /* acknowledged, offer corresponding services and invoke one or more if required. */
81 std::auto_ptr<Internals::CommandDispatcher> dispatcher(Internals::AcceptAssociation(*server, net));
82
83 if (dispatcher.get() != NULL)
84 {
85 if (server->isThreaded_)
86 { 73 {
87 server->bagOfDispatchers_.Add(dispatcher.release()); 74 if (dispatcher.get() != NULL)
75 {
76 if (server->isThreaded_)
77 {
78 server->bagOfDispatchers_.Add(dispatcher.release());
79 }
80 else
81 {
82 IRunnableBySteps::RunUntilDone(*dispatcher);
83 }
84 }
88 } 85 }
89 else 86 catch (OrthancException& e)
90 { 87 {
91 IRunnableBySteps::RunUntilDone(*dispatcher); 88 LOG(ERROR) << "Exception in the DICOM server thread: " << e.What();
92 } 89 }
93 } 90 }
94 } 91
95 92 LOG(INFO) << "DICOM server stopping";
96 LOG(INFO) << "DICOM server stopping"; 93
97 94 if (server->isThreaded_)
98 if (server->isThreaded_) 95 {
99 { 96 server->bagOfDispatchers_.StopAll();
100 server->bagOfDispatchers_.StopAll(); 97 }
101 } 98
102 99 /* drop the network, i.e. free memory of T_ASC_Network* structure. This call */
103 /* drop the network, i.e. free memory of T_ASC_Network* structure. This call */ 100 /* is the counterpart of ASC_initializeNetwork(...) which was called above. */
104 /* is the counterpart of ASC_initializeNetwork(...) which was called above. */ 101 OFCondition cond = ASC_dropNetwork(&network);
105 cond = ASC_dropNetwork(&net); 102 if (cond.bad())
106 if (cond.bad()) 103 {
107 { 104 LOG(ERROR) << "Error while dropping the network: " << cond.text();
108 LOG(ERROR) << "Error while dropping the network: " << cond.text(); 105 }
109 } 106 }
110 }
111 107
112 108
113 DicomServer::DicomServer() : 109 DicomServer::DicomServer() :
114 pimpl_(new PImpl), 110 pimpl_(new PImpl),
115 aet_("ANY-SCP") 111 aet_("ANY-SCP")
120 storeRequestHandlerFactory_ = NULL; 116 storeRequestHandlerFactory_ = NULL;
121 applicationEntityFilter_ = NULL; 117 applicationEntityFilter_ = NULL;
122 checkCalledAet_ = true; 118 checkCalledAet_ = true;
123 clientTimeout_ = 30; 119 clientTimeout_ = 30;
124 isThreaded_ = true; 120 isThreaded_ = true;
125 continue_ = false; 121 continue_ = true;
126 started_ = false;
127 } 122 }
128 123
129 DicomServer::~DicomServer() 124 DicomServer::~DicomServer()
130 { 125 {
131 if (continue_) 126 if (continue_)
306 } 301 }
307 302
308 void DicomServer::Start() 303 void DicomServer::Start()
309 { 304 {
310 Stop(); 305 Stop();
306
307 /* initialize network, i.e. create an instance of T_ASC_Network*. */
308 T_ASC_Network *network;
309 OFCondition cond = ASC_initializeNetwork
310 (NET_ACCEPTOR, OFstatic_cast(int, port_), /*opt_acse_timeout*/ 30, &network);
311 if (cond.bad())
312 {
313 LOG(ERROR) << "cannot create network: " << cond.text();
314 throw OrthancException(ErrorCode_DicomPortInUse);
315 }
316
311 continue_ = true; 317 continue_ = true;
312 started_ = false; 318 pimpl_->thread_ = boost::thread(ServerThread, this, network);
313 pimpl_->thread_ = boost::thread(ServerThread, this);
314
315 while (!started_)
316 {
317 Toolbox::USleep(50000); // Wait 50ms
318 }
319 } 319 }
320 320
321 321
322 void DicomServer::Stop() 322 void DicomServer::Stop()
323 { 323 {