aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ic/java_src/com/ericsson/otp/ic/Environment.java
blob: bffa0e27e69ea5e6d636176beb36235b70695214 (plain) (blame)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1999-2016. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 *
 */
/**
 * The Environment class for Java IDL
 *
 */
package com.ericsson.otp.ic;

/**

  The Environment class handles communication 
  setup and stub state. The methods of this class
  are specially designed for the generated stubs.
  This class must be used when designing asynchronous
  message passing.

  **/
  

public class Environment {

  // Private variables
  private com.ericsson.otp.erlang.OtpSelf self;
  private com.ericsson.otp.erlang.OtpPeer peer;
  private java.lang.Object server;
  private java.lang.String cookie;
  private com.ericsson.otp.erlang.OtpConnection connection;
  private com.ericsson.otp.erlang.OtpErlangRef send_ref;    /* Client side send reference */
  private com.ericsson.otp.erlang.OtpErlangRef receive_ref; /* Client side received reference */
  private com.ericsson.otp.erlang.OtpErlangPid clientP;
  private com.ericsson.otp.erlang.OtpErlangPid serverP;
  private com.ericsson.otp.erlang.OtpOutputStream os;       /* Output stream */
  private com.ericsson.otp.erlang.OtpInputStream is;        /* Input stream */
  private boolean stopped;
 
  // Private variables used by server only
  private int tag;
  private java.lang.String operation;
  private java.lang.String type;
  private com.ericsson.otp.erlang.OtpErlangRef ref;         /* Server side client reference */
  private com.ericsson.otp.erlang.OtpErlangPid caller;      /* Server side client pid */
 
  // Tags to distiguish client / server environments 
  private boolean clientT;
  private boolean serverT;


  /**
    Client stub side constructor.
    **/
  public Environment(com.ericsson.otp.erlang.OtpSelf _Self,
		     com.ericsson.otp.erlang.OtpPeer _Peer,
		     java.lang.Object _Server) throws java.lang.Exception {
		       
		       init();
		       clientT = true;
		       self = _Self;
		       peer = _Peer;
		       server = _Server;
		       os = new com.ericsson.otp.erlang.OtpOutputStream();
  }


  /**
    Client stub side constructor.
    **/  
  public Environment(java.lang.String _SelfNode,
		     java.lang.String _PeerNode,
		     java.lang.String _Cookie,
		     java.lang.Object _Server) throws java.lang.Exception {
		       
		       init();
		       clientT = true;
		       self = new com.ericsson.otp.erlang.OtpSelf(_SelfNode, _Cookie);
		       peer = new com.ericsson.otp.erlang.OtpPeer(_PeerNode);
		       cookie = _Cookie;
		       server = _Server;
		       os = new com.ericsson.otp.erlang.OtpOutputStream();
  }


  /**
    Client stub side constructor.
    **/  
  public Environment(com.ericsson.otp.erlang.OtpConnection _connection,
		     java.lang.Object _Server) throws java.lang.Exception {
		       
		       init();
		       clientT = true;
		       self = _connection.self();
		       peer = _connection.peer();
		       connection = _connection;
		       server = _Server;
		       os = new com.ericsson.otp.erlang.OtpOutputStream();
  }
  

  /** 
    Server skeleton side constructor.
    **/
  public Environment() throws java.lang.Exception {
    
    init();
    serverT = true;
    stopped = false;
    os = new com.ericsson.otp.erlang.OtpOutputStream();

  }

  
  /* Communication toolbox */
  
  /**
    Client stub side connector.
    **/
  public void connect() throws java.lang.Exception {
    
    if (connection == null)
      connection = self.connect(peer);
 
    clientP = self.createPid(); /* This is not perfect */
    send_ref = self.createRef();
    
  }

  /**
    Reconnects a client by closing existing connection 
    and connecting.
    **/
  public void reconnect() throws java.lang.Exception {

    if (connection.isConnected())
	connection.close();

    connection = self.connect(peer);

  }
  
  /**
    Closes the established connection.
    **/
  public void disconnect() {
    
    connection.close();
    
  }


  /**
    Client side message sender.
    **/
  public void send() throws java.lang.Exception {
    
    if (server instanceof java.lang.String)
      connection.sendBuf((java.lang.String)server, os);
    else
      connection.sendBuf((com.ericsson.otp.erlang.OtpErlangPid)server, os);
    
  }
  
  
  /**
    Client message receiver.
    **/
  public void receive() throws java.lang.Exception {
    
    is = connection.receiveBuf();
    
    if (clientT) { // If client, decode message reference too
      is.read_tuple_head();
      receive_ref = is.read_ref();      
    }
  }


  /**
    Universal message receiver.
    **/
  public void receive(com.ericsson.otp.erlang.OtpConnection _connection) throws java.lang.Exception {
    
    is = _connection.receiveBuf();
    
    if (clientT) { // If client, decode message reference too
      is.read_tuple_head();
      receive_ref = is.read_ref();      
    }
  }

  
  /* Accessors */
   
    /**
       Server RegName/OtpErlangPid accessor. 
       Used to access the server Reg/Pid, which 
       initiated the connection.
       @return java.lang.Object, the server for the active OtpConnection.
    **/
  public java.lang.Object server() {
    
    return server;
    
  }

  /**
    Caller identity accessor. Used by a server stub to access the 
    caller identity of the received message.
    @return OtpErlangPid, the caller identity.
    **/
  public com.ericsson.otp.erlang.OtpErlangPid caller_pid() {
    
    return clientP;
    
  }


  /**
    Received message reference accessor.  Used by a server stub to access the 
    reference of the received message.
    @return OtpErlangRef, the reference of the received message.
    **/
  public com.ericsson.otp.erlang.OtpErlangRef received_ref() {
    
    return receive_ref;
    
  }


  /* Encoders */

  /**
    Client Pid Encoder.  Used by a server stub to encode the 
    enclosed client process identity.
    **/
  public void write_client_pid() {

    os.write_pid(clientP.node(),clientP.id(),clientP.serial(),clientP.creation());
    
  }

  /** 
    Client Ref Encoder. Used by a server stub to encode the 
    enclosed client message reference.
    **/
  public void write_client_ref() {
    
    os.write_ref(send_ref.node(),send_ref.id(),send_ref.creation());
    
  }



  /* Field access functions */

  /**
    Output Stream accessor. 
    @return OtpOutputStream, the enclosed output stream.
    **/
  public com.ericsson.otp.erlang.OtpOutputStream getOs() {
    return os;
  }

  /**
    Input Stream accessor. 
    @return OtpInputStream, the enclosed input stream.
    **/
  public com.ericsson.otp.erlang.OtpInputStream getIs() {
    return is;
  }

  /**
    Server skeleton side client (caller) pid accessor.
    @return OtpErlangPid, the caller process identity.
    **/
  public com.ericsson.otp.erlang.OtpErlangPid getScaller() {
    return caller;
  }

  /**
    Server skeleton side client call reference accessor.
    @return OtpErlangRef, the latest call message reference.
    **/
  public com.ericsson.otp.erlang.OtpErlangRef getSref() {
    return ref;
  }
  


  /* Field modifiers */
  


  /* Decoders */
  
  /**
    Decodes the message head from existing stream.
    Assignes message data to private variables of the Environment Object.
    **/
  public void uHead() throws java.lang.Exception {
    uHead(is);
  }

  /**
    Decodes the message head and writes over input stream.
    Assignes message data to private variables of the Environment Object.
    **/
  public void uHead(com.ericsson.otp.erlang.OtpInputStream _is) throws java.lang.Exception {
    
    is = _is;
    is.read_tuple_head();
    type = is.read_atom();
    
    if (type.equals("$gen_call")) {  // Call type operation
      is.read_tuple_head();
      caller = is.read_pid();
      ref = is.read_ref();
      tag = is.peek();
      
      switch (tag) {
      case com.ericsson.otp.erlang.OtpExternal.atomTag:
      case com.ericsson.otp.erlang.OtpExternal.atomUtf8Tag:
      case com.ericsson.otp.erlang.OtpExternal.smallAtomUtf8Tag:
	operation = is.read_atom();
	break;
      default:
	is.read_tuple_head();
	operation = is.read_atom();
      }
    } else {  // Cast type operation
      tag = is.peek();
      switch (tag) {
      case com.ericsson.otp.erlang.OtpExternal.atomTag:
      case com.ericsson.otp.erlang.OtpExternal.atomUtf8Tag:
      case com.ericsson.otp.erlang.OtpExternal.smallAtomUtf8Tag:
	operation = is.read_atom();
	break;
      default:
	is.read_tuple_head();
	operation = is.read_atom();
      }
    }
  }

  /**
    Operation label accessor.
    @return int, the label hash value.
    **/
  public int uLabel(java.util.Dictionary _operations) {

    java.lang.Integer __label =
      (java.lang.Integer) _operations.get(operation);
    
    if(__label == null) 
      return -1;

    return __label.intValue();
  }



  /* Controllers */

  /**
    Operation controller.
    @return boolean, true if the operation variable found in Environment class
    is supported in the input operation dictionary, false otherwize.
    **/
  public boolean validOp(java.util.Dictionary _operations) {
    
    if((_operations.get(operation)) == null)
      return false;
    
    return true; 
  }


  /**
    Server stop request controller.
    @return boolean, true if there is a client request for the server
    to be stopped, false otherwize.
    **/
    public boolean isStopped() {
	return stopped;
    };


  
  /* Destroy functions */ 

    /* 
     Creates and sends a stop message.
     Called by client stub to terminate the server.
    */
  public void client_stop_server() 
      throws java.lang.Exception {

     // Message header assembly
     os.reset();
     os.write_tuple_head(2);
     os.write_atom("$gen_cast");

     os.write_atom("stop");

     send();
	
  }

  /*
  Sets the stop flag for the server.
  Called by server skeleton when stop message is received.
  */
  public void server_stop_server() {

      // Note at server is dead !
      stopped = true;
  }


  /* Private methods */

  /**
    Private variable initialization.
    **/
  public void init() {

    clientT = false;
    serverT = false;
    stopped = false;
    self = null;
    peer = null;
    server = null;
    cookie = null;
    connection = null;
    clientP = null;
    serverP = null;
    send_ref = null;
    receive_ref = null;
    os = null;
    is = null;

    tag = -1;
    operation = null;
    type = null;

  };

}