Source code: com/neuron/jaffer/DSI_Packet.java
1 /*
2 * Copyright (c) 2003 Stewart Allen <stewart@neuron.com>. All rights reserved.
3 * This program is free software. See the 'License' file for details.
4 */
5
6 package com.neuron.jaffer;
7
8 import java.io.*;
9 import java.net.*;
10 import java.util.*;
11
12 // TODO : payload becomes common to ByteReader/ByteWriter
13 class DSI_Packet extends Utility implements DSI_Constants
14 {
15 private static int maxR = 0;
16 private static int maxW = 0;
17
18 private int flags;
19 private int command;
20 private int requestID;
21 private int errDataOff;
22 private int dataLength;
23 private int reserved;
24 private byte payload[];
25 private byte header[];
26 private ByteWriter writer;
27
28 DSI_Packet(int bufsize)
29 {
30 payload = new byte[bufsize+128];
31 header = new byte[16];
32 writer = new ByteWriter(payload);
33 }
34
35 DSI_Packet(InputStream is)
36 throws IOException
37 {
38 this(0x8000);
39 read(is);
40 }
41
42 DSI_Packet(int f, int c, int id, byte d[])
43 throws IOException
44 {
45 this(d.length);
46 flags = f;
47 command = c;
48 requestID = id;
49 errDataOff = 0;
50 dataLength = 0;
51 reserved = 0;
52 writer.writeBytes(d);
53 }
54
55 public void reset()
56 {
57 writer = new ByteWriter(payload);
58 }
59
60 public int getBufferSize()
61 {
62 return payload.length - 128;
63 }
64
65 public void dumpRecvPayload(String prefix)
66 {
67 dump(prefix, payload, dataLength);
68 }
69
70 public void dumpSendPayload(String prefix)
71 {
72 dump(prefix, payload, writer.getSize());
73 }
74
75 public ByteReader getReader()
76 {
77 return new ByteReader(payload, dataLength);
78 }
79
80 public ByteWriter getWriter()
81 {
82 return writer;
83 }
84
85 public int getFlags()
86 {
87 return flags;
88 }
89
90 public int getCommand()
91 {
92 return command;
93 }
94
95 public int getRequestID()
96 {
97 return requestID;
98 }
99
100 public void setRequestID(int id)
101 {
102 requestID = id;
103 }
104
105 public boolean isRequest()
106 {
107 return (flags & 0x1) == DSI_REQUEST;
108 }
109
110 public boolean isReply()
111 {
112 return (flags & 0x1) == DSI_REPLY;
113 }
114
115 public void setRequest()
116 {
117 flags = DSI_REQUEST;
118 }
119
120 public void setReply()
121 {
122 flags = DSI_REPLY;
123 }
124
125 public void setErrorCode(int ec)
126 {
127 errDataOff = ec;
128 }
129
130 public void setDataOffset(int ec)
131 {
132 errDataOff = ec;
133 }
134
135 private void readData(InputStream is, byte b[], int len)
136 throws IOException
137 {
138 if (len > b.length)
139 {
140 throw new IOException("Read Request ("+len+") Exceeds Buffer Size ("+b.length+")!");
141 }
142 int read = 0;
143 while (read < len)
144 {
145 int got = is.read(b, read, len - read);
146 if (got < 0)
147 {
148 throw new EOFException();
149 }
150 read += got;
151 }
152 }
153
154 public void read(InputStream is)
155 throws IOException
156 {
157 readData(is, header, header.length);
158 flags = header[0];
159 command = header[1];
160 requestID = readInt2(header, 2);
161 errDataOff = readInt4(header, 4);
162 dataLength = readInt4(header, 8);
163 reserved = readInt4(header, 12);
164 readData(is, payload, dataLength);
165 //if (dataLength > maxR) { maxR = dataLength; System.out.println("max read now "+maxR); }
166 }
167
168 public void write(OutputStream os)
169 throws IOException
170 {
171 header[0] = (byte)flags;
172 header[1] = (byte)command;
173 writeInt2(header, 2, requestID);
174 writeInt4(header, 4, errDataOff);
175 writeInt4(header, 8, writer.getSize());
176 writeInt4(header,12, reserved);
177 os.write(header);
178 writer.writeTo(os);
179 //dataLength = writer.getSize();
180 os.flush();
181 //if (dataLength > maxW) { maxW = dataLength; System.out.println("max write now "+maxW); }
182 }
183
184 public String toString()
185 {
186 if (command == CMD_COMMAND)
187 {
188 int cmd = isRequest() ? payload[0] & 0xff : 0;
189 if (cmd >= AFP_Constants.COMMAND.length || cmd < 0) { cmd = 0; }
190 return
191 "f="+hex(flags)+",c="+hex(command)+",id="+hex(requestID)+
192 ",edo="+hex(errDataOff)+",rd="+hex(dataLength)+",sd="+hex(writer.getSize())+
193 ",cmd="+AFP_Constants.COMMAND[cmd];
194 }
195 else
196 {
197 return
198 "f="+hex(flags)+",c="+hex(command)+",id="+hex(requestID)+
199 ",edo="+hex(errDataOff)+",rl="+hex(dataLength)+",sd="+hex(writer.getSize())+
200 ",dsi="+COMMAND[command];
201 }
202 }
203 }
204