1 /*
2  *
3  *  Copyright (C) 1998-2018, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module: dcmpstat
15  *
16  *  Author: Marco Eichelberg
17  *
18  *  Purpose:
19  *    classes: DVPSIPCMessage
20  *
21  */
22 
23 #ifndef DVPSMSG_H
24 #define DVPSMSG_H
25 
26 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
27 #include "dcmtk/dcmpstat/dpdefine.h"
28 #include "dcmtk/dcmdata/dctypes.h"    /* for Uint32 */
29 #include "dcmtk/ofstd/ofstring.h"     /* for class OFString */
30 
31 class DcmTransportConnection;
32 
33 /** class for IPC message exchange between different processes of the
34  *  DICOMscope application
35  */
36 class DCMTK_DCMPSTAT_EXPORT DVPSIPCMessage
37 {
38 public:
39 
40   /// default constructor
41   DVPSIPCMessage();
42 
43   /// copy constructor
44   DVPSIPCMessage(const DVPSIPCMessage& copy);
45 
46   /// destructor
47   virtual ~DVPSIPCMessage();
48 
49   /// copy assignment operator
50   DVPSIPCMessage& operator=(const DVPSIPCMessage&);
51 
52   /** sets the message type
53    *  @param msgtype new message type
54    */
setMessageType(Uint32 msgtype)55   void setMessageType(Uint32 msgtype) { messageType = msgtype; }
56 
57   /** returns the message type
58    *  @return message type
59    */
getMessageType()60   Uint32 getMessageType() { return messageType; }
61 
62   /** adds a character string into the message payload.
63    *  @param str zero terminated string, may be NULL (in which case an empty string is added)
64    */
65   void addStringToPayload(const char *str);
66 
67   /** adds an integer into the message payload.
68    *  @param value to write
69    */
70   void addIntToPayload(Uint32 i);
71 
72   /** extracts a string from the message payload
73    *  and copies it into the given str object.
74    *  @param str string is written into this parameter
75    *  @return OFTrue if successful, OFFalse otherwise
76    */
77   OFBool extractStringFromPayload(OFString& str);
78 
79   /** extracts an integer from the message payload.
80    *  @param i integer is written into this parameter
81    *  @return OFTrue if successful, OFFalse otherwise
82    */
83   OFBool extractIntFromPayload(Uint32& i);
84 
85   /** rewinds the read offset to the beginning of the message payload
86    */
87   void rewindPayload();
88 
89   /** removes all payload
90    */
91   void erasePayload();
92 
93   /** sends the current message over the given transport connection.
94    *  @param connection transport connection to be used
95    *  @return OFTrue if successful, OFFalse otherwise.
96    */
97   OFBool send(DcmTransportConnection &connection);
98 
99   /** receives a messages from the given transport connection
100    *  and stores it in the current object, replacing any existing
101    *  payload.
102    *  @param connection transport connection to be used
103    *  @return OFTrue if successful, OFFalse otherwise.
104    */
105   OFBool receive(DcmTransportConnection &connection);
106 
107 
108   // constants for message type
109   static const Uint32 OK;
110   static const Uint32 requestApplicationID;
111   static const Uint32 assignApplicationID;
112   static const Uint32 applicationTerminates;
113   static const Uint32 receivedUnencryptedDICOMConnection;
114   static const Uint32 receivedEncryptedDICOMConnection;
115   static const Uint32 connectionClosed;
116   static const Uint32 connectionAborted;
117   static const Uint32 requestedUnencryptedDICOMConnection;
118   static const Uint32 requestedEncryptedDICOMConnection;
119   static const Uint32 receivedDICOMObject;
120   static const Uint32 sentDICOMObject;
121 
122   // message status constants
123   static const Uint32 statusOK;      // OK
124   static const Uint32 statusWarning; // warning
125   static const Uint32 statusError;   // error
126 
127   // client type constants
128   static const Uint32 clientOther;    // client is of unspecified type
129   static const Uint32 clientStoreSCP; // client is Store SCP
130   static const Uint32 clientStoreSCU; // client is Store SCU
131   static const Uint32 clientPrintSCP; // client is Print SCP
132   static const Uint32 clientPrintSCU; // client is Print SCU
133   static const Uint32 clientQRSCP;    // client is Query/Retrieve (Find/Move/Get) SCP
134 
135 private:
136 
137   /** resize payload if necessary such that at least i bytes can be written
138    *  @param i number of bytes required in buffer
139    */
140   void resizePayload(size_t i);
141 
142   /// type of message
143   Uint32 messageType;
144 
145   /// number of bytes actually used in payload
146   Uint32 payloadUsed;
147 
148   /// number of bytes allocated in payload
149   Uint32 payloadAllocated;
150 
151   /// read offset into payload in bytes
152   Uint32 payloadReadOffset;
153 
154   /// pointer to raw payload data in big endian byte order
155   unsigned char *payload;
156 };
157 
158 
159 /** a client for IPC message exchange between different processes of the
160  *  DICOMscope application
161  */
162 class DCMTK_DCMPSTAT_EXPORT DVPSIPCClient
163 {
164 public:
165 
166   /** constructor
167    *  @param clientType type of client application, see constants defined in DVPSIPCMessage
168    *  @param txt textual description of client application
169    *  @param thePort TCP/IP port on which the server is listening
170    *  @param keepOpen flag indicating whether the connection should be kept
171    *    open all the time or should be opened/closed for each transaction.
172    */
173   DVPSIPCClient(Uint32 clientType, const char *txt, int thePort, OFBool keepOpen);
174 
175   /// destructor
176   virtual ~DVPSIPCClient();
177 
178   /** sends ApplicationTerminates notification to server.
179    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
180    */
181   void notifyApplicationTerminates(Uint32 status);
182 
183   /** sends ReceivedUnencryptedDICOMConnection notification to server.
184    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
185    *  @param txt textual description of notification for server
186    */
187   void notifyReceivedUnencryptedDICOMConnection(Uint32 status, const char *txt);
188 
189   /** sends ReceivedEncryptedDICOMConnection notification to server.
190    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
191    *  @param txt textual description of notification for server
192    */
193   void notifyReceivedEncryptedDICOMConnection(Uint32 status, const char *txt);
194 
195   /** sends ConnectionClosed notification to server.
196    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
197    */
198   void notifyConnectionClosed(Uint32 status);
199 
200   /** sends ConnectionAborted notification to server.
201    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
202    *  @param txt textual description of notification for server
203    */
204   void notifyConnectionAborted(Uint32 status, const char *txt);
205 
206   /** sends RequestedUnencryptedDICOMConnection notification to server.
207    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
208    *  @param txt textual description of notification for server
209    */
210   void notifyRequestedUnencryptedDICOMConnection(Uint32 status, const char *txt);
211 
212   /** sends RequestedEncryptedDICOMConnection notification to server.
213    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
214    *  @param txt textual description of notification for server
215    */
216   void notifyRequestedEncryptedDICOMConnection(Uint32 status, const char *txt);
217 
218   /** sends ReceivedDICOMObject notification to server.
219    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
220    *  @param txt textual description of DICOM object
221    */
222   void notifyReceivedDICOMObject(Uint32 status, const char *txt);
223 
224   /** sends SentDICOMObject notification to server.
225    *  @param Uint32 message status, see constants defined in DVPSIPCMessage
226    *  @param txt textual description of DICOM object
227    */
228   void notifySentDICOMObject(Uint32 status, const char *txt);
229 
230   /** checks whether the message server has been found active
231    *  upon creation of this object.
232    *  @return OFTrue if server is active, OFFalse otherwise.
233    */
isServerActive()234   OFBool isServerActive() { return serverActive; }
235 
236 private:
237 
238   /// private undefined copy constructor
239   DVPSIPCClient(const DVPSIPCClient& copy);
240 
241   /// private undefined copy assignment operator
242   DVPSIPCClient& operator=(const DVPSIPCClient&);
243 
244   /** request connection to server, store in 'connection' if successful.
245    */
246   void requestConnection();
247 
248   /** perform message transaction with server. If serverActive is false,
249    *  no connection attempt is performed.
250    *  @param msg contains message to be sent, overwritten with message
251    *    received from server if successful
252    *  @return OFTrue if successful, OFFalse otherwise
253    */
254   OFBool performTransaction(DVPSIPCMessage& msg);
255 
256   /// TCP/IP port number on which the server is listening
257   int port;
258 
259   /// true if the first connection attempt has succeeded, false otherwise
260   OFBool serverActive;
261 
262   /// application ID assigned by the server
263   Uint32 applicationID;
264 
265   /// flag indicating whether we want to keep the connection open
266   OFBool keepConnectionOpen;
267 
268   /// current transport connection
269   DcmTransportConnection *connection;
270 };
271 
272 
273 #endif
274