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

import JACE.OS.*;

/**
 * <hr>
 * <h2>SYNOPSIS</h2>
 *<blockquote>
 *     Provides an abstraction for managing a bi-directional flow of
 * messages. 
 *</blockquote>
 *
 * <h2>DESCRIPTION</h2>
 *<blockquote>
 *   This is based on the Module concept in System V Streams,
 *   which contains a pair of Tasks, one for handling upstream
 *   processing, one for handling downstream processing.
 *</blockquote>
 */
public class Module
{
  // = Initialization and termination methods.

  /**
   * Create an empty Module.
   */
  public Module ()
  {
    // Do nothing...
    this.name ("<unknown>");
  }

  /*
   * Create an initialized module.
   *@param modName identity of the module.
   *@param writerQ writer task of the module.
   *@param readerQ reader task of the module.
   *@param flags Module flags
   */
  public Module (String modName,
		 Task writerQ,
		 Task readerQ,
		 Object flags)
  {
    this.open (modName, writerQ, readerQ, flags);
  }
		
  /*
   * Create an initialized module.
   *@param modName identity of the module.
   *@param writerQ writer task of the module.
   *@param readerQ reader task of the module.
   *@param flags Module flags
   */
  public void open (String modName,
		   Task writerQ,
		   Task readerQ,
		   Object arg)
  {
    this.name (modName);
    this.arg_ = arg;

    if (writerQ == null)
      writerQ = new ThruTask ();
    if (readerQ == null)
      readerQ = new ThruTask ();

    this.reader (readerQ);
    this.writer (writerQ);

    // Setup back pointers.
    readerQ.module (this);
    writerQ.module (this);
  }


  /*
   * Set the writer task.
   *@param q the writer task
   */
  public void writer (Task q)
  { 
    this.qPair_[1] = q;
    if (q != null)
      q.flags (ACE.CLR_BITS (q.flags (), TaskFlags.ACE_READER));
  }

  /*
   * Set the reader task.
   *@param q the reader task
   */
  public void reader (Task q) 
  {
    this.qPair_[0] = q;
    if (q != null)
      q.flags (ACE.SET_BITS (q.flags (), TaskFlags.ACE_READER));
  }

  /*
   * Link this Module on top of Module.
   *@param m the module to link this on top of.
   */
  public void link (Module m)
  {
    this.next (m);
    this.writer ().next (m.writer ());
    m.reader ().next (this.reader ());
  }

  /*
   * Set and get pointer to sibling Task in Module.
   *@param orig the task to get the sibling for
   *@return the sibling of the task
   */
  public Task sibling (Task orig)
  {
  if (this.qPair_[0] == orig)
    return this.qPair_[1];
  else if (this.qPair_[1] == orig)
    return this.qPair_[0];
  else
    return null;
  }

  /*
   * Close down the module and its tasks.
   *@param flags Module flags
   *@return 0 on success, -1 on failure
   */
  public int close (long flags)
  {
    Task readerQ = this.reader ();
    Task writerQ = this.writer ();
    int result = 0;

    if (readerQ != null)
      {
	if (readerQ.close (flags) == -1)
	  result = -1;
	readerQ.flush (flags);
	readerQ.next (null);
      }

    if (writerQ != null)
      {
	if (writerQ.close (flags) == -1)
	  result = -1;
	writerQ.flush (flags);
	writerQ.next (null);
      }

    return result;
  }

  /* 
   * Get the argument passed to tasks.
   *@return the argument passed to tasks.
   */
  public Object arg ()
  {
    return this.arg_;
  }

  /* 
   * Set the argument to be passed to tasks.
   *@param a the argument to be passed to tasks.
   */
  public void arg (Object a)
  {
    this.arg_ = a;
  }

  /* 
   * Get the name of the module.
   *@return the name of the module.
   */
  public String name ()
  {
    return this.name_;
  }

  /* 
   * Set the name of the module.
   *@param n the name of the module.
   */
  public void name (String n)
  {
    this.name_ = n;
  }

  /* 
   * Get the writer task of the module.
   *@return the writer task of the module.
   */
  public Task writer ()
  { 
    return this.qPair_[1];
  }

  /* 
   * Get the reader task of the module.
   *@return the reader task of the module.
   */
  public Task reader ()
  {
    return this.qPair_[0];
  }

  /*
   * Get the next pointer to the module above in the stream.
   *@return the next pointer to the module above in the stream.
   */
  public Module next ()
  {
    return this.next_;
  }

  /*
   * Set the next pointer to the module above in the stream.
   *@param m the next pointer to the module above in the stream.
   */
  public void next (Module m) 
  {
    this.next_ = m;
  }

  private Task qPair_[] = new Task[2];
  // Pair of Tasks that form the "read-side" and "write-side" of the
  // ACE_Module partitioning.

  private String name_ = null;
  // Name of the ACE_Module.

  private Module next_;
  // Next ACE_Module in the stack.

  private Object arg_;
  // Argument passed through to the reader and writer task when they
  // are opened.

}