File: ProcessCommunicationHandler.java

package info (click to toggle)
dicomscope 3.6.0-28
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,256 kB
  • sloc: java: 22,911; cpp: 5,957; sh: 270; makefile: 45
file content (320 lines) | stat: -rw-r--r-- 11,058 bytes parent folder | download | duplicates (9)
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
/*
 *
 *  Copyright (C) 1999, Institute for MicroTherapy
 *
 *  This software and supporting documentation were developed by
 *
 *    University of Witten/Herdecke
 *    Department of Radiology and MicroTherapy
 *    Institute for MicroTherapy
 *    Medical computer science
 *    
 *    Universitaetsstrasse 142
 *    44799 Bochum, Germany
 *    
 *    http://www.microtherapy.de/go/cs
 *    mailto:computer.science@microtherapy.de
 *
 *  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  AND THE INSTITUTE MAKES  NO 
 *  WARRANTY REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY
 *  OR FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES 
 *  OR ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY 
 *  AND PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
 *
 *  Author :      $Author: kleber $
 *  Last update : $Date: 2001/06/06 10:32:30 $
 *  Revision :    $Revision: 1.1.1.1 $
 *  State:        $State: Exp $
*/


package processCommunication;

import J2Ci.*;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This class handles the communication with the 
 * DICOMscope processes. 
 * 
 * @author Klaus Kleber
 * @since 16.10.2000
 */
public class ProcessCommunicationHandler extends java.lang.Thread
{   
    /**
    * Date/Time format: YYYY-MM-Dd:HH-mm-ss:SSSSSS 
    */ 
    public static final java.text.SimpleDateFormat dateTimeFormaterString = new java.text.SimpleDateFormat("yyyy-MM-dd, HH:mm:ss.SSSSSS");
    
    
    /**
    * Socket
    */
    private Socket socket;
    /**
    * InputStream
    */
    DataInputStream in;
    
    /** 
    * OutputStream
    */
    DataOutputStream out;
    
    
    /**
    * Counter for the applicationID
    */
    private static int processIDCounter = 0;
    
   
    
    /**
    * Constuctor with the specified Socket.
    * @param socket Communication socket
    */
    public ProcessCommunicationHandler(Socket socket)
    {
        this.socket = socket;
    }
    /**
    * Contains the avilable processes. For each processID you can get the name of the process
    */
    private static Hashtable processList = new Hashtable();
    /**
    * Open the Input/OutputStream and waits for messages. Each received message will be
    * answered. This function stops if the skocket will be closed.
    */
    public void run()
    {
        
        try
        {
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        }
        catch(IOException e)
        {
            System.err.println(e);
        }
        try
        {
            while(true)
            {
                handleMessage();
            }
        }
        catch(IOException e)
        {
            //System.err.println(e);
            
        }
        
    }
    /**
    * Receives the message and answered it. 
    * For each received message a DicomScopeMessage will
    * be fired.
    */
    private  void handleMessage() throws IOException
    {
            int processId=-1;
            String text = null;
            
            int messageId  = in.readInt();
            String messageName = ProcessMessageIDs.getMessageName(messageId);
            
            
            int payload = in.readInt();
            int statusId = 0;
            
            switch(messageId)
            {
                case ProcessMessageIDs.REQUEST_APPLICATION_IDENTIFICATIONNUMBER:
                        processId = giveNewProcessID();
                        int processType = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.ASSIGN_APPLICATION_IDENTIFICATION_NUMBER);
                        out.writeInt(4);
                        out.writeInt(processId);
                        insertProcess(processId, processType);
                        break;
                case ProcessMessageIDs.APPLICATION_TERMINATES: 
                        processId = in.readInt();
                        statusId = in.readInt();
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                        //  The processId of the process will be removed at the end of this function         
                        
                        break;
                case ProcessMessageIDs.RECEIVED_UNENCRYPTED_DICOM_CONNECTION:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.RECEIVED_ENCRYPTED_DICOM_CONNECTION:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.DICOM_CONNECTION_CLOSED:
                        processId = in.readInt();
                        statusId = in.readInt();
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.DICOM_CONNECTION_ABORDED:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.REQUESTED_UNENCRYPTED_DICOM_CONNECTION:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.REQUESTED_ENCRYPTED_DICOM_CONNECTION:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.RECEIVED_DICOM_OBJECT:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
                case ProcessMessageIDs.SENT_DICOM_OBJECT:
                        processId = in.readInt();
                        statusId = in.readInt();
                        text = readText(in);
                        out.writeInt(ProcessMessageIDs.RESPOND_OK);
                        out.writeInt(0);
                       break;
            }
            out.flush();
            if (text == null) text = "";
            String processTypeName = getProcessName(processId);
            int processType= getProcessType(processId);
            String date = getCurrentDateString();
            String statusName = ProcessStatusIDs.getStatusName(statusId);
            
            //  Remove the messageID if process Terminates         
            if (messageId == ProcessMessageIDs.APPLICATION_TERMINATES) removeProcess(processId);
            ProcessController.instance().fireProcessLog(new DicomScopeMessage(  messageId,
                                                                                messageName,
                                                                                processId, 
                                                                                processType,
                                                                                processTypeName,
                                                                                date,
                                                                                statusId,
                                                                                statusName,
                                                                                text));
    
    }
    
    /**
    * Reads a String from the specified DataInputStream. The length of the String
    * will be readed first (int). A String is teminated with one or more Null bytes
    * Thes null bytes will be eleminated.
    * @return The readed String without null-bytes.
    */
    private String readText(DataInputStream in) throws IOException 
    {
        int length = in.readInt();
        byte b[] = new byte[length];
        in.read(b);
        int lastIndex = length;
        while((lastIndex > 0)&& (b[lastIndex-1] == 0x00)) lastIndex--;
        return new String(b, 0,lastIndex);
    }
    
    
    
    /**
    * Removes the process specified by the Id.
    * @param processId Specifies the process to be removed.
    */
    private void removeProcess(int processId)
    {
        processList.remove(new Integer(processId));
    }
    
    /**
    * Insert a process
    * @param processId Unique ID identifying a process.
    * @param processType Id for the process type
    */
    private static synchronized  void insertProcess(int processId,int processType)
    {
        processList.put(new Integer(processId),new Integer(processType) );
        
    }
    
    /**
    * Gets the type of a process with the specified Id.
    * @param processId Id of the process
    * @return The type of the process.
    */
    private static synchronized int getProcessType(int processId)
    {
         return ((Integer) processList.get(new Integer(processId))).intValue();
    }
    
    
    /**
    * Gets the name of a process with the specified Id.
    * @param processId Id of the process
    * @return The name of the process.
    */
    private static synchronized String getProcessName(int processId)
    {
       return  ProcessTypeIDs.getProcessName(getProcessType(processId));
    }
    
    /**
    * Calculates a new unique process id:
    * @return Returns the new process id
    */
    public synchronized static int giveNewProcessID()
    {
        return processIDCounter++;
    }
    
    /**
    * Returns a String with the current date in ISO format.
    * (Date/Time format: YYYY-MM-Dd:HH-mm-ss:SSSSSS )
    * @return String with the current date in ISO format.
    */
    public String getCurrentDateString()
    {
    	Calendar calendar = new GregorianCalendar ();
	return dateTimeFormaterString.format(calendar.getTime());
        
    }
    
}

/*
 *  CVS Log
 *  $Log: ProcessCommunicationHandler.java,v $
 *  Revision 1.1.1.1  2001/06/06 10:32:30  kleber
 *  Init commit for DICOMscope 3.5
 *  Create new CVS
 *
*/