summaryrefslogtreecommitdiff
path: root/java/netsvcs/Naming/NameRequest.java
blob: f8a3579fa358e49474930edb6f91b3e149ea04f4 (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
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
321
322
323
324
325
326
327
328
329
330
331
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Naming
 *
 * = FILENAME
 *    NameRequest.java
 *
 * Used by both client and naming server as detailed in
 * the NameHandler.  This structure transfers information
 * including name, value, type, and request type.
 *
 *@see netsvcs.Naming.NameHandler
 *
 *@author Everett Anderson
 *
 *************************************************/
package netsvcs.Naming;

import java.io.*;
import java.util.*;
import JACE.OS.*;
import JACE.Connection.*;
import JACE.Reactor.*;
import JACE.ASX.*;
import JACE.SOCK_SAP.*;

public class NameRequest
{
  /**
   * Different types of requests 
   */
  public static final int BIND = 1; 
  public static final int REBIND = 2; 
  public static final int RESOLVE = 3; 
  public static final int UNBIND  = 4; 
  public static final int LIST_NAMES = 5;
  public static final int LIST_VALUES = 13;
  public static final int LIST_TYPES = 21;
  public static final int LIST_NAME_ENTRIES = 6;
  public static final int LIST_VALUE_ENTRIES = 14;
  public static final int LIST_TYPE_ENTRIES = 22;
  public static final int MAX_ENUM = 11; 

  /**
   * Default constructor
   */
  public NameRequest () 
    {
      this.name_ = this.value_ = this.type_ = null;
      this.length_ = 32;
    } 

  /** 
   * Constructor
   *
   * @param requestType      Type of request this is (BIND, REBIND, etc)
   * @param name             Key to bind
   * @param value            Value to bind
   * @param type             Type to couple with the value
   * @param timeout          Timer information (not really used in JACE yet)
   */
  public NameRequest(int requestType,
		     String name,
		     String value,
		     String type,
		     TimeValue timeout)
    {
      this.requestType_ = requestType;

      if (timeout == null) {

	this.blockForever_ = 1;
	this.secTimeout_ = 0;
	this.usecTimeout_ = 0;
      } else {

	this.blockForever_ = 0;
	this.secTimeout_ = (int)timeout.sec();
	this.usecTimeout_ = (int)timeout.getMilliTime() * 1000;
      }

      // This is necessary to make sure null pointer exceptions are
      // avoided.  It makes it more consistent later on
      if (name == null)
	this.name_ = new String("");
      else
	this.name_ = new String(name);
      if (value == null)
	this.value_ = new String("");
      else
	this.value_ = new String(value);
      if (type == null)
	this.type_ = new String("");
      else
	this.type_ = new String(type);

      // Set the length
      this.calculateLength();
    }

  /** 
   * Calculate the transmission length (bytes) of this structure
   */
  private void calculateLength()
    {
      // The type is sent as an 8 bit data type (chars in the C++ version),
      // but the name and value are sent as 16 bit chars (ACE_USHORT16's in C++)

      this.length_ = 34 + this.type_.length() + 2 * (this.name_.length() +
						     this.value_.length());

    }

  /**
   * Return the transmission length
   */
  public int length() 
    { return this.length_; }

  /**
   * Return the name/key
   */
  public String name()
    { return new String(this.name_); }

  /**
   * Set the name/key
   * @param name    Name to set to
   */
  public void name(String name)
    {
      if (name == null)
	this.name_ = new String("");
      else
	this.name_ = new String(name);

      this.calculateLength();
    }

  /**
   * Return the value
   */
  public String value()
    { return new String(this.value_); }

  /** 
   * Set the value 
   * @param value   New value
   */
  public void value(String value)
    {
      if (value == null)
	this.value_ = new String("");
      else
	this.value_ = new String(value);

      this.calculateLength();
    }

  /**
   * Return the type
   */
  public String type()
    { return new String(this.type_); }

  /**
   * Set the type
   * @param type    New type
   */
  public void type(String type)
    {
      if (type == null)
	this.type_ = new String("");
      else
	this.type_ = new String(type);

      this.calculateLength();
    }

  /**
   * Fill the fields of this instance with data from the socket
   *
   *@param sock     Socket to read from
   */
  public void streamInFrom (JACE.SOCK_SAP.SOCKStream sock) throws IOException
    {
      DataInputStream dis = new DataInputStream(sock.inputStream());

      this.streamInFrom(dis);
    }

  /**
   * Fill the fields of this instance from the given DataInputStream
   *
   *@param dis     DataInputStream to read from
   */
  public void streamInFrom (DataInputStream dis) throws IOException
    {
      // Read the length (32 bits)
      length_ = dis.readInt();

      // Read the request type
      requestType_ = dis.readInt();

      // Can we block forever to fulfill this request?  (unused)
      blockForever_ = dis.readInt();

      // How long until we should time out this request?  (unused)
      secTimeout_ = dis.readInt();
      usecTimeout_ = dis.readInt();

      // The sizes are in bytes, and there are two bytes per char
      // (ACE_USHORT16 in C++ land)
      int nameLen = dis.readInt() / 2;
      int valueLen = dis.readInt() / 2;

      int typeLen = dis.readInt();

      // Read the name -- just read chars since they're 16 bits.
      // Hopefully the SOCKStream has buffered the data
      char buf[] = new char[nameLen];
      for (int i = 0; i < nameLen; i++) {
	buf[i] = dis.readChar();
      }
      this.name_ = new String(buf);

      // Read the value
      buf = new char[valueLen];
      for (int i = 0; i < valueLen; i++)
	buf[i] = dis.readChar();
      this.value_ = new String(buf);
      
      // Read the type -- now we can use readFully since 
      // the type was sent as 8 bit chars
      byte tbuf[] = new byte[typeLen];
      dis.readFully(tbuf);
      this.type_ = new String(tbuf);

      // Skip the null char at the end
      dis.skipBytes(2);
    }
  
  /**
   * Send this NameRequest out to the given SOCKStream
   *
   *@param sock    SOCKStream to send to
   */
  public void streamOutTo (JACE.SOCK_SAP.SOCKStream sock) throws IOException
    {
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(bout);

      dos.writeInt(length_);
      dos.writeInt(requestType_);
      dos.writeInt(blockForever_);
      dos.writeInt(secTimeout_);
      dos.writeInt(usecTimeout_);

      // Byte sizes are sent, and the name and value are stored as
      // 16 bit char arrays (ACE_USHORT16 arrays in C++ version)
      dos.writeInt(this.name_.length() * 2);
      dos.writeInt(this.value_.length() * 2);
      dos.writeInt(this.type_.length());

      // Making sure the name_ wasn't null comes in handy
      // in situations like this
      dos.writeChars(this.name_);
      dos.writeChars(this.value_);
      dos.writeBytes(this.type_);

      // Null termination
      dos.writeChar(0);

      // Send it for real
      dos.flush();

      byte[] array = bout.toByteArray();

      sock.sendN(array, 0, array.length);
    }

  /**
   * Set the requestType
   *@param type   Type to set to
   */
  public void requestType(int type)
    {
      this.requestType_ = type;
    }

  /**
   * Get requestType
   */
  public int requestType()
    {
      return this.requestType_;
    }

  /**
   * Can we block forever to fulfill the request? (unused)
   */
  public boolean blockForever()
    {
      return (this.blockForever_ != 0) ? true : false;
    }

  /** 
   * Allowed timeout (unused)
   */
  public int secTimeout()
    {
      return this.secTimeout_;
    }

  int length_;
  int requestType_;
  int blockForever_;
  int secTimeout_;
  int usecTimeout_;

  String name_;
  String value_;
  String type_;
};