summaryrefslogtreecommitdiff
path: root/java/JACE/ASX/Stream.java
blob: 6a968714ab7500eca2e68e37741f0e30811830b1 (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
/*************************************************
 *
 * = PACKAGE
 *    JACE.ASX
 *
 * = FILENAME
 *    Stream.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.ASX;

import JACE.OS.*;

/**
 *   This class is the primary abstraction for the ASX framework.
 *   It is moduled after System V Stream. <P>
 *
 *   A Stream consists of a stack of Modules, each of which
 *   contains two Tasks.
 *
 *@see Module
 *@see Task
 */

public class Stream
{

  public Stream ()
  {
    this (null, null, null);
  }

  // Create a Stream consisting of <head> and <tail> as the Stream
  // head and Stream tail, respectively.  If these are 0 then the
  // <ACE_Stream_Head> and <ACE_Stream_Tail> are used, respectively.
  // <arg> is the value past in to the open() methods of the tasks.

  public Stream (Object a,
		 Module head,
		 Module tail)
  {
    this.linkedUs_ = null;
    // this.final_close_ = this.lock_;

    if (this.open (a, head, tail) == -1)
      ACE.ERROR ("open" + head.name () + " " +  tail.name ());
  }

  public int push (Module newTop)
  {
    if (this.pushModule  (newTop, 
			  this.streamHead_.next (), 
			  this.streamHead_) == -1)
      return -1;
    else
      return 0;
  }

    // Note that the timeout tv is absolute time
  public int put (MessageBlock mb, TimeValue tv)
  {
    return this.streamHead_.writer ().put (mb, tv);
  }

    // Note that the timeout tv is absolute time
  public MessageBlock get (TimeValue tv) throws InterruptedException
  {
    return this.streamHead_.reader ().getq (tv);
  }

// Return the "top" ACE_Module in a ACE_Stream, skipping over the
// stream_head.

  public Module top ()
  {
    if (this.streamHead_.next () == this.streamTail_)
      return null;
    else
      return this.streamHead_.next ();
  }

// Remove the "top" ACE_Module in a ACE_Stream, skipping over the
// stream_head.

  public int pop (long flags)
  {
    if (this.streamHead_.next () == this.streamTail_)
      return -1;
    else
      {
	// Skip over the ACE_Stream head.
	Module top = this.streamHead_.next ();
	Module newTop = top.next ();

	this.streamHead_.next (newTop);

	// Close the top ACE_Module.

	top.close (flags);

	this.streamHead_.writer ().next (newTop.writer ());
	newTop.reader ().next (this.streamHead_.reader ());

	return 0;
      }
  }

// Remove a named ACE_Module from an arbitrary place in the
// ACE_Stream.

  public int remove (String name, long flags)
  {
    Module prev = null;

    for (Module mod = this.streamHead_; 
	 mod != null; mod = mod.next ())
      if (name.compareTo (mod.name ()) == 0)
	{
	  if (prev == null) // Deleting ACE_Stream Head
	    this.streamHead_.link (mod.next ());
	  else
	    prev.link (mod.next ());
	  
	  mod.close (flags);
	  return 0;
	}
      else
	prev = mod;

    return -1;
  }

  public Module find (String name)
  {
    for (Module mod = this.streamHead_; 
	 mod != null; 
	 mod = mod.next ())
      if (name.compareTo (mod.name ()) == 0)
	return mod;

    return null;
  }

// Actually push a module onto the stack...

  private int pushModule (Module newTop, 
			  Module currentTop, 
			  Module head)
  {
    Task ntReader = newTop.reader ();
    Task ntWriter = newTop.writer ();
    Task ctReader = null;
    Task ctWriter = null;

    if (currentTop != null)
      {
      ctReader = currentTop.reader ();
      ctWriter = currentTop.writer ();
      ctReader.next (ntReader);
      }

    ntWriter.next (ctWriter);

    if (head != null)
      {
	if (head != newTop)
	  head.link (newTop);
      }
    else
      ntReader.next (null);

    newTop.next (currentTop);

    if (ntReader.open (newTop.arg ()) == -1)
      return -1;

    if (ntWriter.open (newTop.arg ()) == -1)
      return -1;
    return 0;
  }

  public synchronized int open (Object a, 
				Module head, 
				Module tail)
  {
    Task h1 = null, h2 = null;
    Task t1 = null, t2 = null;

    if (head == null)
      {
	h1 = new StreamHead ();
	h2 = new StreamHead ();
	head = new Module ("ACEStreamHead", h1, h2, a);
      }

    if (tail == null)
      {
	t1 = new StreamTail ();
	t2 = new StreamTail ();
	tail = new Module ("ACEStreamTail", 
			   t1, t2, a);
      }

    // Make sure *all* the allocation succeeded!
    if (h1 == null || h2 == null || head == null
	|| t1 == null || t2 == null || tail == null)
      {
      // Close up!
	head.close (0);
	tail.close (0);
	return -1;
      }

    this.streamHead_ = head;
    this.streamTail_ = tail;

    if (this.pushModule (this.streamTail_,
			 null, null) == -1)
      return -1;
    else if (this.pushModule (this.streamHead_, 
			      this.streamTail_, 
			      this.streamHead_) == -1)
      return -1;
    else
      return 0;
  }

  public synchronized int close (long flags)
  {
    if (this.streamHead_ != null
	&& this.streamTail_ != null)
      {
	// Don't bother checking return value here.
	this.unlinkInternal ();

	int result = 0;

	// Remove and cleanup all the intermediate modules.

	while (this.streamHead_.next () != this.streamTail_)
	  {
	    if (this.pop (flags) == -1)
	      result = -1;
	  }
  
	// Clean up the head and tail of the stream.
	if (this.streamHead_.close (flags) == -1)
	  result = -1;
	if (this.streamTail_.close (flags) == -1)
	  result = -1;

	this.streamHead_ = null;
	this.streamTail_ = null;

	// Tell all threads waiting on the close that we are done.
	//      this.final_close_.broadcast ();
	return result;
      }
    return 0;
  }

  public int control (int cmd, Object a) throws InterruptedException
  {
    IOCntlMsg ioc = new IOCntlMsg (cmd);

    // Create a data block that contains the user-supplied data.
    MessageBlock db = 
      new MessageBlock (MessageType.MB_IOCTL, 
			null, 
			a);

    // Create a control block that contains the control field and a
    // pointer to the data block.
    MessageBlock cb = 
      new MessageBlock (MessageType.MB_IOCTL, 
			db, 
			(Object) ioc);

    int result = 0;

    if (this.streamHead_.writer ().put (cb, null) == -1)
      result = -1;
    else if ((cb = this.streamHead_.reader ().getq (null)) == null)
      result = -1;
    else
      result = ((IOCntlMsg ) cb.obj ()).rval ();

    return result;
  }

// Link two streams together at their bottom-most Modules (i.e., the
// one just above the Stream tail).  Note that all of this is premised
// on the fact that the Stream head and Stream tail are non-NULL...
// This must be called with locks held.

  private int linkInternal (Stream us)
  {
    this.linkedUs_ = us;
    // Make sure the other side is also linked to us!
    us.linkedUs_ = this;

    Module myTail = this.streamHead_;

    if (myTail == null)
      return -1;

    // Locate the module just above our Stream tail. 
    while (myTail.next () != this.streamTail_)
      myTail = myTail.next ();

    Module otherTail = us.streamHead_;

    if (otherTail == null)
      return -1;

  // Locate the module just above the other Stream's tail.
    while (otherTail.next () != us.streamTail_)
      otherTail = otherTail.next ();

    // Reattach the pointers so that the two streams are linked!
    myTail.writer ().next (otherTail.reader ());
    otherTail.writer ().next (myTail.reader ());
    return 0;
  }

  public synchronized int link (Stream us)
  {
    return this.linkInternal (us);
  }  

// Must be called with locks held...

  private int unlinkInternal ()
  {
  // Only try to unlink if we are in fact still linked!
    
    if (this.linkedUs_ != null)
    {
      Module myTail = this.streamHead_;

      // Only relink if we still exist!
      if (myTail != null)
	{
	  // Find the module that's just before our stream tail.
	  while (myTail.next () != this.streamTail_)
	    myTail = myTail.next ();

	  // Restore the writer's next() link to our tail.
	  myTail.writer ().next (this.streamTail_.writer ());
	}

      Module otherTail = this.linkedUs_.streamHead_;

      // Only fiddle with the other side if it in fact still remains.
      if (otherTail != null)
	{
	  while (otherTail.next () != this.linkedUs_.streamTail_)
	    otherTail = otherTail.next ();

	  otherTail.writer ().next (this.linkedUs_.streamTail_.writer ());
	  
	}

      // Make sure the other side is also aware that it's been unlinked!
      this.linkedUs_.linkedUs_ = null;

      this.linkedUs_ = null;
      return 0;
    }
    else
      return -1;
  }

  public synchronized int unlink ()
  {
    return this.unlinkInternal ();
  }

  public void dump ()
  {
    ACE.DEBUG ("-------- module links --------");

    for (Module mp = this.streamHead_; ; mp = mp.next ())
      {
	ACE.DEBUG ("module name = " + mp.name ());
	if (mp == this.streamTail_)
	  break;
      }
    
    ACE.DEBUG ("-------- writer links --------");
    
    Task tp;
    
    for (tp = this.streamHead_.writer (); ; tp = tp.next ())
      {
	ACE.DEBUG ("writer queue name = " + tp.name ());
	tp.dump ();
	ACE.DEBUG ("-------\n");
	if (tp == this.streamTail_.writer ()
	    || (this.linkedUs_ != null && tp == this.linkedUs_.streamHead_.reader ()))
	  break;
      }

    ACE.DEBUG ("-------- reader links --------\n");
    for (tp = this.streamTail_.reader (); ; tp = tp.next ())
      {
	ACE.DEBUG ("reader queue name = " + tp.name ());
	tp.dump ();
	ACE.DEBUG ("-------\n");
	if (tp == this.streamHead_.reader ()
	    || (this.linkedUs_ != null && tp == this.linkedUs_.streamHead_.writer ()))
	  break;
      }
  }

  Module streamHead_ = null;
  // Pointer to the head of the stream.

  Module streamTail_ = null;
  // Pointer to the tail of the stream.

  Stream linkedUs_ = null;
  // Pointer to an adjoining linked stream.

  // = Synchronization objects used for thread-safe streams.
  //  ACE_SYNCH_MUTEX lock_;
  // Protect the stream against race conditions.
  
  //  ACE_SYNCH_CONDITION final_close_;
  // Use to tell all threads waiting on the close that we are done.

}