01: package CVS_Server.Network;
02:
03: /**
04: * This thread processes *all* sockets in its queue.
05: *
06: * It holds one single instance of TransactionClientContext.
07: *
08: */
09:
10: import java.util.Vector;
11: import java.io.*;
12: import java.net.Socket;
13:
14: import CVS_Server.CVSServer;
15:
16: public class ConnectionQueueProcessor extends Thread {
17:
18: private Vector pendingConnections = new Vector(); // of sockets
19: private boolean doTerminate = false;
20:
21: private CVSServer cvsServer;
22: private TransactionClientContext transactionClientContext;
23:
24: public ConnectionQueueProcessor(final CVSServer cvsServer) {
25: this .cvsServer = cvsServer;
26: this .transactionClientContext = new TransactionClientContext();
27: } // Constructor
28:
29: /**
30: * We lock onto this thread itself and wait in the
31: * run method, therefore it must be synchronized.
32: * When a new connection is added to the queue, we notify it.
33: */
34: public synchronized void run() {
35: while (!this .doTerminate) {
36: try {
37: this .wait(2000);
38: } catch (Exception anyEx346) {
39: }
40: if (this .pendingConnections.size() > 0) {
41: Socket socket = (Socket) this .pendingConnections
42: .elementAt(0);
43: this .pendingConnections.removeElementAt(0);
44: this .processConnection(socket);
45: }
46: } // while
47: } // run
48:
49: public synchronized void terminate() {
50: this .doTerminate = true;
51: this .notify();
52: }
53:
54: public synchronized void addConnection(final Socket socket) {
55: this .pendingConnections.add(socket);
56: this .notify();
57: }
58:
59: /**
60: * Processes a socket from the queue.
61: */
62: private void processConnection(final Socket clientSocket) {
63: // State:
64: // - mache ggf nur UNIDIREKTIONALE sockets fuer die Transaktionen
65: // -> TransactionClientContext instance ist hier gehalten
66: // -> muss dann noch auf einen zweiten socket warten
67: //
68: // - einfache requests etc werden hier direkt bearbeitet (single bidi socket)
69: // - broadcast sockets kommen zusaetzlich hinzu, aber nicht hier
70: //
71: // -> Langsam Protokolle konkret festlegen (sequences...)
72: DataInputStream dataIn = null;
73: try {
74: BufferedInputStream bufIn = new BufferedInputStream(
75: clientSocket.getInputStream());
76: dataIn = new DataInputStream(bufIn);
77: final int identifier = dataIn.readInt();
78: switch (identifier) {
79:
80: default:
81: System.out
82: .println("ConnectionQueueProcessor.processConnection: Unknown identifier: "
83: + identifier
84: + " received. No action was taken.");
85: } // switch
86: } catch (Exception anyEx) {
87: anyEx.printStackTrace();
88: } finally {
89: try {
90: dataIn.close();
91: dataIn = null;
92: } catch (Exception e234765) {
93: }
94: }
95: } // processConnection
96:
97: } // ConnectionQueueProcessor
|