summaryrefslogtreecommitdiff
path: root/java/src/MessageBlock.java
blob: 1741f9bef8095e6512b4b07738335ea8c430ae16 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*************************************************
 *
 * = PACKAGE
 *    JACE.ASX
 *
 * = FILENAME
 *    MessageBlock.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.ASX;

import JACE.OS.*;

/**
 * <hr>
 * <h2>SYNOPSIS</h2>
 *<blockquote>
 *     Object used to store messages in the ASX framework.
 *</blockquote>
 *
 * <h2>DESCRIPTION</h2>
 *<blockquote>
 *     <tt>MessageBlock</tt> is modeled after the message data structures
 *     used in System V STREAMS. A <tt>MessageBlock</tt> is composed of
 *     one or more <tt>MessageBlock</tt>s that are linked together by <em>PREV</em>
 *     and <em>NEXT</em> pointers. In addition, a <tt>MessageBlock</tt> may also be
 *     linked to a chain of other <tt>MessageBlock</tt>s. This structure
 *     enables efficient manipulation of arbitrarily-large messages
 *     <em>without</em> incurring memory copying overhead.
 *</blockquote>
 *
 *@see MessageQueue
 */
public class MessageBlock
{
  /**
   * Create an empty Message Block
   */
  public MessageBlock ()
    {
      this (0);
    }

  /**
   * Create an empty Message Block.
   * Note that this assumes that type of MessageBlock is MB_DATA.
   *@param size size of the Message Block to create.
   */
  public MessageBlock (int size)
    {
      // Note the explicit cast toString() is needed. For some strange
      // reason, it fails otherwise if size == 0.
      this ((new StringBuffer (size)).toString ());
    }

  /**
   * Create a Message Block. Note that this assumes that type of
   * MessageBlock is MB_DATA. 
   *@param data initial data to create a Message Block with.
   */
  public MessageBlock (String data)
    {
      this (MessageType.MB_DATA,
	    null,
	    data);
    }

  /**
   * Create a Message Block.
   *@param type type of the Message Block (must be one of those
   * specified in class Message Type)
   *@param cont next block of data 
   *@param data initial data to create Message Block with
   */
  public MessageBlock (int type,
		       MessageBlock cont,
		       String data)
    {
      this.flags_ = 0;
      this.priority_ = 0;
      this.next_ = null;
      this.prev_ = null;

      this.init (type, cont, data);
    }

  /**
   * Create a Message Block. Note that this assumes that type of
   * MessageBlock is MB_OBJECT.
   *@param obj initial object to create a Message Block with.
   */
  public MessageBlock (Object obj)
  {
    this (MessageType.MB_OBJECT,
	  null,
	  obj);
  }

  /**
   * Create a Message Block.
   *@param type type of the Message Block (must be one of those
   * specified in class Message Type)
   *@param cont next block of data 
   *@param obj initial object to create Message Block with
   */
  public MessageBlock (int type,
		       MessageBlock cont,
		       Object obj)
  {
    this.init (type, cont, obj);
  }

  /* Initialize the Message Block
   *@param data data to initialize Message Block with
   */
  public void init (String data)
    {
      this.base_ = new StringBuffer (data);
    }

  /**
   * Initialize a Message Block.
   *@param type type of the Message Block (must be one of those
   * specified in class Message Type)
   *@param cont next block of data 
   *@param data data to initialize Message Block with
   */
  public void init (int msgType,
		   MessageBlock msgCont,
		   String data)
    {
      if (data.length () == 0)
	this.base_ = new StringBuffer (0);
      else
	this.base_ = new StringBuffer (data);
      this.type_ = msgType;
      this.cont_ = msgCont;
    }

  /**
   * Initialize a Message Block. Note that this assumes that type of
   * MessageBlock is MB_OBJECT.
   *@param obj initial object to initialize a Message Block with.
   */
  public void init (Object obj)
    {
      this.init (MessageType.MB_OBJECT, null, obj);
    }

  /**
   * Initialize a Message Block.
   *@param type type of the Message Block (must be one of those
   * specified in class Message Type)
   *@param cont next block of data 
   *@param obj object to initialize Message Block with
   */
  public void init (int msgType,
		   MessageBlock msgCont,
		   Object obj)
    {
      this.obj_ = obj;
      this.type_ = msgType;
      this.cont_ = msgCont;
      this.flags_ = 0;
      this.priority_ = 0;
      this.next_ = null;
      this.prev_ = null;
    }

  /**
   * Set message flags. Note that the flags will be set on top of
   * already set flags.
   *@param moreFlags flags to set for the Message Block.
   */
  public long setFlags (long moreFlags)
    {
      // Later we might mask more_flags so that user can't change
      // internal ones: more_flags &= ~(USER_FLAGS -1).
      this.flags_ = ACE.SET_BITS (this.flags_, moreFlags);
      return this.flags_;
    }

  /**
   * Unset message flags.
   *@param lessFlags flags to unset for the Message Block.
   */
  public long clrFlags (long lessFlags)
    {
      // Later we might mask more_flags so that user can't change
      // internal ones: less_flags &= ~(USER_FLAGS -1).
      this.flags_ = ACE.CLR_BITS (this.flags_, lessFlags);
      return this.flags_;
    }

  /**
   * Get the message flags.
   *@return Message flags
   */
  public long flags ()
    {
      return this.flags_;
    }

  /**
   * Get the type of the message.
   *@return message type
   */
  public int msgType ()
    {
      return this.type_;
    }

  /**
   * Set the type of the message.
   *@param t type of the message
   */
  public void msgType (int t)
    {
      this.type_ = t;
    }

  /**
   * Get the class of the message. Note there are two classes,
   * <normal> messages and <high-priority> messages.
   *@return message class
   */
  public int msgClass ()
    {
      return this.msgType () >= MessageType.MB_PRIORITY
	? MessageType.MB_PRIORITY : MessageType.MB_NORMAL;
    }

  /**
   * Find out if the message is a data message.
   *@return true if message is a data message, false otherwise
   */
  public boolean isDataMsg ()
    {
      int mt = this.msgType ();
      return mt == MessageType.MB_DATA
	|| mt == MessageType.MB_PROTO
	  || mt == MessageType.MB_PCPROTO;
    }

  /**
   * Find out if the message is an object message.
   *@return true if message is an object message, false otherwise
   */
  public boolean isObjMsg ()
    {
      int mt = this.msgType ();
      return mt == MessageType.MB_OBJECT
	|| mt == MessageType.MB_PROTO
	  || mt == MessageType.MB_PCPROTO;
    }

  /**
   * Get the priority of the message.
   *@return message priority
   */
  public long msgPriority ()
    {
      return this.priority_;
    }

  /**
   * Set the priority of the message.
   *@param pri priority of the message
   */
  public void msgPriority (long pri)
    {
      this.priority_ = pri;
    }

  /**
   * Get message data. This assumes that msgType is MB_DATA.
   *@return message data
   */
  public String base () 
    {
      // Create a String object to return
      char temp[] = new char [this.base_.length ()];
      this.base_.getChars (0, this.base_.length (), temp, 0);
      return new String (temp);
    }

  /**
   * Set the message data. This assumes that msgType is MB_DATA.
   *@param data message data
   *@param msgFlags message flags
   */
  public void base (String data,
		    long msgFlags)
    {
      this.base_ = new StringBuffer (data);
      this.flags_ = msgFlags;
    }

  /**
   * Get message object. This assumes that msgType is MB_OBJECT.
   *@return message object
   */
  public Object obj () 
    {
      return this.obj_;
    }

  /**
   * Set the message object. This assumes that msgType is MB_OBJECT.
   *@param object message object
   *@param msgFlags message flags
   */
  public void obj (Object obj,
		   long msgFlags)
    {
      this.obj_ = obj;
      this.flags_ = msgFlags;
    }

  // = The following four methods only make sense if the Message_Block
  // is of type MB_DATA and not MB_OBJECT.

  /**
   * Get length of the message. This method only makes sense if the
   * MessageBlock is of type MB_DATA and not MB_OBJECT.
   *@return length of the message.
   */
  public int length ()
    {
      return this.base_.length ();
    }

  /**
   * Set the length of the message. This method only makes sense if the
   * MessageBlock is of type MB_DATA and not MB_OBJECT.
   *@param n message length
   */
  public void length (int n)
    {
      this.base_.setLength (n);
    }

  /**
   * Get size of the allocated buffer for the message. This method
   * only makes sense if the MessageBlock is of type MB_DATA and not
   * MB_OBJECT. 
   *@return size of the message buffer
   */
  public int size ()
    {
      return this.base_.capacity ();
    }

  /**
   * Set the total size of the buffer. This method will grow the
   * buffer if need be. Also, this method only makes sense if the
   * MessageBlock is of type MB_DATA and not MB_OBJECT.
   *@param n size of message buffer
   */
  public void size (int n)
    {
      this.base_.ensureCapacity (n);
    }


  /**
   * Get the continuation field. The coninuation field is used to
   * chain together composite messages.
   *@return the continuation field
   */
  public MessageBlock cont ()
    {
      return this.cont_;
    }

  /**
   * Set the continuation field. The coninuation field is used to
   * chain together composite messages.
   *@param msgCont continuation field
   */
  void cont (MessageBlock msgCont)
    {
      this.cont_ = msgCont;
    }

  /**
   * Get link to next message. The next message points to the
   * <MessageBlock> directly ahead in the MessageQueue.
   *@return next message block
   */
  MessageBlock next ()
    {
      return this.next_;
    }

  /**
   * Set link to next message. The next message points to the
   * <MessageBlock> directly ahead in the MessageQueue.
   *@param msgBlock next message block
   */
  void next (MessageBlock msgBlock)
    {
      this.next_ = msgBlock;
    }

  /**
   * Get link to previous message. The previous message points to the
   * <MessageBlock> directly before in the MessageQueue.
   *@return previous message block
   */
  MessageBlock prev ()
    {
      return this.prev_;
    }

  /**
   * Set link to previous message. The previous message points to the
   * <MessageBlock> directly before in the MessageQueue.
   *@param msgBlock previous message block
   */
  void prev (MessageBlock msgBlock)
    {
      this.prev_ = msgBlock;
    }

  private int type_;		
  // Type of message.

  private long flags_;
  // Misc flags.

  private long priority_;
  // Priority of message.

  private StringBuffer base_;
  // String data of message block (initialized to null).

  private Object obj_;
  // Object data of message block (initialized to null).

  private MessageBlock cont_;
  // Next message block in the chain.

  private MessageBlock next_;	
  // Next message in the list.

  private MessageBlock prev_;	
  // Previous message in the list.

}