summaryrefslogtreecommitdiff
path: root/java/apps/NexusII/src/socketReaderThread.java
blob: 35679c504b4a16c47bf506e8622b734eb23defa6 (plain)
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
/* 
   $RCSfile$
   $Author$

   Last Update: $Date$
   $Revision$

   REVISION HISTORY:
   $Log$
   Revision 1.1  1997/01/31 01:11:11  sumedh
   Added the Nexus II source code files.

# Revision 1.1  1996/12/07  06:27:38  rajeev
# Initial revision
#
# Revision 1.3  1996/12/03  01:35:37  rajeev
# fixed a big bug with respect to trim()
#
# Revision 1.2  1996/12/03  01:01:27  rajeev
# // fixed the bug at line 76
#
# Revision 1.1  1996/12/02  06:08:56  rajeev
# Initial revision
#
# Revision 1.1  1996/12/02  06:02:24  rajeev
# Initial revision
#

*/
// The socketReaderThread is like a Producer who reads from the socket and
// nqs it onto the queue. Thats it ... Big Deal. huh ! 

// This threads task is
// 1. Take things from SocketBuffer.
// 2. Ensure that Full Packets are read 
// 3. Put the packet onto the queue 

//package NexusII.networking ; 

// get hold of java classes
//import NexusII.client.* ; 
//import NexusII.util.* ; 

import java.io.* ; 


public class socketReaderThread extends Producer implements Runnable,consts
{
  DataInputStream is_ ; 
  
  // new constructor 
  // Pass the queue and socketid to the constructor 

  public socketReaderThread(MT_Bounded_Queue q, DataInputStream is) {
    // call the super constructor 
    super(q);
    is_ = is ; 
  }

  // This will just override the run method and thats it 
  // I want to have my own run !

  public void run()  {
    // run indefinitely  -- i am a daemon anyway 
    if(DEBUG) {
      System.out.println("--- This is socketReaderThread --- \n");
    }
    
    for(;;) {

      // read header bytes from stream 
      int field_len = FIELD_LENGTH ; 
      byte[] packetHeader = new byte[PACKET_HEADER_LENGTH];
      try {
	is_.readFully(packetHeader,0,PACKET_HEADER_LENGTH);
      } 
      catch (IOException e) {
	// do something here 
	System.out.println("socketReader: I/O exception in read: I am quitting");
	// what to do here -- right now quit
	return ; 
      }
      if(DEBUG){
	System.out.println("socketReader read:" + new String(packetHeader,0));
      }
      
      // take out the length field from this 
      String length = new String(packetHeader,0,DATA_LEN_OFFSET,field_len);
      
      // Read this much more from the socket 
      if(DEBUG) { 
	System.out.println("The length of packet is " + length);
      }

      Integer Test = new Integer(length.trim());
      int len = Test.intValue();
      if(DEBUG) {
	System.out.println("srt: attempting to read " + Test + " bytes ");
      }

      byte[] packetBody = new byte[len] ; 
      try {
	if(len != 0) 
	  is_.readFully(packetBody,0,len);
      }
      catch (IOException e) {
	// do something here 
	System.out.println("socketReader: I/O exception in read: I am quitting");
	// what to do here -- right now quit
	return ; 
      }
      
      // The header and the body are there now -- so make a packet
      dataPacket packet = new dataPacket(packetHeader,packetBody);
      if(DEBUG)
	System.out.println("srt: Now nq'ing item body " + packet.content() );
      queue_.nq(packet);
      if(DEBUG)
	System.out.println("srt: Done nq'ing..");
      
      // go back to reading the socket now 
    }
    // of for(;;)
  } 
  // of method run 
}