jungerl /lib/otp.net/Otp/OtpConnection.cs

Language C# Lines 569
MD5 Hash 00160d8fcb4a8051b7f34266aa611a97 Estimated Cost $4,437 (why?)
Repository https://github.com/bmizerany/jungerl.git View Raw File View Project SPDX
  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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*``The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved via the world wide web at http://www.erlang.org/.
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
* 
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
* 
 * Converted from Java to C# by Vlad Dumitrescu (vlad_Dumitrescu@hotmail.com)
*/

using System;
using System.Diagnostics;

namespace Otp
{

	/*
	* Maintains a connection between a C# process and a remote Erlang,
	* C# or C node. The object maintains connection state and allows
	* data to be sent to and received from the peer.
	*
	* <p> Once a connection is established between the local node and a
	* remote node, the connection object can be used to send and receive
	* messages between the nodes and make rpc calls (assuming that the
	* remote node is a real Erlang node).
	*
	* <p> The various receive methods are all blocking and will return
	* only when a valid message has been received or an exception is
	* raised.
	*
	* <p> If an exception occurs in any of the methods in this class, the
	* connection will be closed and must be explicitely reopened in order
	* to resume communication with the peer.
	* 
	* <p> It is not possible to create an instance of this class
	* directly. OtpConnection objects are returned by {@link
	* OtpSelf#connect(OtpPeer) OtpSelf.connect()} and {@link
	* OtpSelf#accept() OtpSelf.accept()}.
	**/
	public class OtpConnection:AbstractConnection
	{
		protected internal OtpSelf _self;
		protected internal GenericQueue queue; // messages get delivered here
		
		/*
		* Accept an incoming connection from a remote node. Used by {@link
		* OtpSelf#accept() OtpSelf.accept()} to create a connection
		* based on data received when handshaking with the peer node, when
		* the remote node is the connection intitiator.
		*
		* @exception C#.io.IOException if it was not possible to connect to the peer.
		*
		* @exception OtpAuthException if handshake resulted in an authentication error
		*/
		// package scope
		internal OtpConnection(OtpSelf self, System.Net.Sockets.TcpClient s):base(self, s)
		{
			this._self = self;
			this.queue = new GenericQueue();

			System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Start));
			t.IsBackground = true;
			t.Name = "connection "+self.node()+" | "+s.ToString();
			t.Start();
		}


		/*
		* Intiate and open a connection to a remote node.
		*
		* @exception C#.io.IOException if it was not possible to connect to the peer.
		*
		* @exception OtpAuthException if handshake resulted in an authentication error.
		*/
		// package scope
		internal OtpConnection(OtpSelf self, OtpPeer other):base(self, other)
		{
			this._self = self;
			this.queue = new GenericQueue();

			System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Start));
			t.IsBackground = true;
			t.Name = "connection2 "+self.node()+" -> "+other.node();
			t.Start();
		}

		public override void  deliver(System.Exception e)
		{
			queue.put(e);
		}

		public override void  deliver(OtpMsg msg)
		{
			queue.put(msg);
		}

		/*
		* Get information about the node at the peer end of this
		* connection.
		* 
		* @return the {@link OtpPeer Node} representing the peer node.
		**/
		public virtual OtpPeer getPeer()
		{
			return peer;
		}
		
		/*
		* Get information about the node at the local end of this
		* connection.
		* 
		* @return the {@link OtpSelf Node} representing the local node.
		**/
		public virtual new OtpSelf self()
		{
			return _self;
		}
		
		/*
		* Return the number of messages currently waiting in the receive
		* queue for this connection.
		*/
		public virtual int msgCount()
		{
			return queue.getCount();
		}
		
		/*
		* Receive a message from a remote process. This method blocks
		* until a valid message is received or an exception is raised.
		*
		* <p> If the remote node sends a message that cannot be decoded
		* properly, the connection is closed and the method throws an
		* exception.
		*
		* @return an object containing a single Erlang term.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is
		* received from a process on the peer node.
		*
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		**/
		public virtual Erlang.Object receive()
		{
			try
			{
				return receiveMsg().getMsg();
			}
			catch (Erlang.DecodeException e)
			{
				close();
				throw new System.IO.IOException(e.Message);
			}
		}
		
		/*
		* Receive a message from a remote process. This method blocks at
		* most for the specified time, until a valid message is received or
		* an exception is raised.
		*
		* <p> If the remote node sends a message that cannot be decoded
		* properly, the connection is closed and the method throws an
		* exception.
		*
		* @param timeout the time in milliseconds that this operation will
		* block. Specify 0 to poll the queue.
		*
		* @return an object containing a single Erlang term.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is
		* received from a process on the peer node.
		*
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		*
		* @exception InterruptedException if no message if the method
		* times out before a message becomes available.
		**/
		public virtual Erlang.Object receive(long timeout)
		{
			try
			{
				return receiveMsg(timeout).getMsg();
			}
			catch (Erlang.DecodeException e)
			{
				close();
				throw new System.IO.IOException(e.Message);
			}
		}
		
		/*
		* Receive a raw (still encoded) message from a remote process.
		* This message blocks until a valid message is received or an
		* exception is raised.
		*
		* <p> If the remote node sends a message that cannot be decoded
		* properly, the connection is closed and the method throws an
		* exception.
		*
		* @return an object containing a raw (still encoded) Erlang term.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is received from a
		* process on the peer node, or if the connection is lost for any
		* reason.
		*
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		**/
		public virtual OtpInputStream receiveBuf()
		{
			return receiveMsg().getMsgBuf();
		}
		
		
		/*
		* Receive a raw (still encoded) message from a remote process. This
		* message blocks at most for the specified time until a valid
		* message is received or an exception is raised.
		*
		* <p> If the remote node sends a message that cannot be decoded
		* properly, the connection is closed and the method throws an
		* exception.
		*
		* @param timeout the time in milliseconds that this operation will
		* block. Specify 0 to poll the queue.
		*
		* @return an object containing a raw (still encoded) Erlang term.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is received from a
		* process on the peer node, or if the connection is lost for any
		* reason.
		*
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		*
		* @exception InterruptedException if no message if the method
		* times out before a message becomes available.
		**/
		public virtual OtpInputStream receiveBuf(long timeout)
		{
			return receiveMsg(timeout).getMsgBuf();
		}
		
		/*
		* Receive a messge complete with sender and recipient information.
		*
		* @return an {@link OtpMsg OtpMsg} containing the header
		* information about the sender and recipient, as well as the actual
		* message contents.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is received from a
		* process on the peer node, or if the connection is lost for any
		* reason.
		*
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		**/
		public virtual OtpMsg receiveMsg()
		{
			System.Object o = queue.get();
			
			if (o is OtpMsg)
			{
				return ((OtpMsg) o);
			}
			else if (o is System.IO.IOException)
			{
				throw (System.IO.IOException) o;
			}
			else if (o is Erlang.Exit)
			{
				throw (Erlang.Exit) o;
			}
			else if (o is OtpAuthException)
			{
				throw (OtpAuthException) o;
			}
			
			return null;
		}
		
		
		/*
		* Receive a messge complete with sender and recipient information.
		* This method blocks at most for the specified time.
		*
		* @param timeout the time in milliseconds that this operation will
		* block. Specify 0 to poll the queue.
		*
		* @return an {@link OtpMsg OtpMsg} containing the header
		* information about the sender and recipient, as well as the actual
		* message contents.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is received from a
		* process on the peer node, or if the connection is lost for any
		* reason.
		* 
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		*
		* @exception InterruptedException if no message if the method
		* times out before a message becomes available.
		**/
		public virtual OtpMsg receiveMsg(long timeout)
		{
			System.Object o = queue.get(timeout);
			
			if (o is OtpMsg)
			{
				return ((OtpMsg) o);
			}
			else if (o is System.IO.IOException)
			{
				throw (System.IO.IOException) o;
			}
			else if (o is Erlang.Exit)
			{
				throw (Erlang.Exit) o;
			}
			else if (o is OtpAuthException)
			{
				throw (OtpAuthException) o;
			}
			
			return null;
		}
		
		/*
		* Send a message to a process on a remote node.
		*
		* @param dest the Erlang PID of the remote process.
		* @param msg the message to send.
		
		* @exception C#.io.IOException if the connection is not active
		* or a communication error occurs.
		**/
		public virtual void  send(Erlang.Pid dest, Erlang.Object msg)
		{
			// encode and send the message
			base.sendBuf(this._self.pid(), dest, new OtpOutputStream(msg));
		}
		
		
		/*
		* Send a message to a named process on a remote node.
		*
		* @param dest the name of the remote process.
		* @param msg the message to send.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		**/
		public virtual void  send(System.String dest, Erlang.Object msg)
		{
			// encode and send the message
			base.sendBuf(this._self.pid(), dest, new OtpOutputStream(msg));
		}
		
		
		/*
		* Send a pre-encoded message to a named process on a remote node.
		*
		* @param dest the name of the remote process.
		* @param payload the encoded message to send.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		**/
		public virtual void  sendBuf(System.String dest, OtpOutputStream payload)
		{
			base.sendBuf(this._self.pid(), dest, payload);
		}
		
		
		/*
		* Send a pre-encoded message to a process on a remote node.
		*
		* @param dest the Erlang PID of the remote process.
		* @param msg the encoded message to send.
		*
		* @exception C#.io.IOException if the connection is not active
		* or a communication error occurs.
		**/
		public virtual void  sendBuf(Erlang.Pid dest, OtpOutputStream payload)
		{
			base.sendBuf(this._self.pid(), dest, payload);
		}
		
		
		/*
		* Send an RPC request to the remote Erlang node. This convenience
		* function creates the following message and sends it to 'rex' on
		* the remote node:
		* 
		* <pre>
		* { self, { call, Mod, Fun, Args, user }}
		* </pre>
		*
		* <p> Note that this method has unpredicatble results if the remote
		* node is not an Erlang node. </p>
		*
		* @param mod the name of the Erlang module containing the function to be called.
		* @param fun the name of the function to call.
		* @param args an array of Erlang terms, to be used as arguments to the function.
		*
		* @exception C#.io.IOException if the connection is not active
		* or a communication error occurs.
		**/
		public virtual void  sendRPC(System.String mod, System.String fun, Erlang.Object[] args)
		{
			sendRPC(mod, fun, new Erlang.List(args));
		}
		
		
		/*
		* Send an RPC request to the remote Erlang node. This convenience
		* function creates the following message and sends it to 'rex' on
		* the remote node:
		* 
		* <pre>
		* { self, { call, Mod, Fun, Args, user }}
		* </pre>
		*
		* <p> Note that this method has unpredicatble results if the remote
		* node is not an Erlang node. </p>
		*
		* @param mod the name of the Erlang module containing the function to be called.
		* @param fun the name of the function to call.
		* @param args a list of Erlang terms, to be used as arguments to the function.
		*
		* @exception C#.io.IOException if the connection is not active
		* or a communication error occurs.
		**/
		public virtual void  sendRPC(System.String mod, System.String fun, Erlang.List args)
		{
			Erlang.Object[] rpc = new Erlang.Object[2];
			Erlang.Object[] call = new Erlang.Object[5];
			
			/*{self, { call, Mod, Fun, Args, user}} */
			
			call[0] = new Erlang.Atom("call");
			call[1] = new Erlang.Atom(mod);
			call[2] = new Erlang.Atom(fun);
			call[3] = args;
			call[4] = new Erlang.Atom("user");
			
			rpc[0] = this._self.pid();
			rpc[1] = new Erlang.Tuple(call);
			
			send("rex", new Erlang.Tuple(rpc));
		}
		
		
		/*
		* Receive an RPC reply from the remote Erlang node. This
		* convenience function receives a message from the remote node, and
		* expects it to have the following format:
		* 
		* <pre>
		* {rex, Term}
		* </pre>
		*
		* @return the second element of the tuple if the received message
		* is a two-tuple, otherwise null. No further error checking is
		* performed.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		*
		* @exception Erlang.Exit if an exit signal is
		* received from a process on the peer node.
		*
		* @exception OtpAuthException if the remote node
		* sends a message containing an invalid cookie.
		**/
		public virtual Erlang.Object receiveRPC()
		{
			
			Erlang.Object msg = receive();
            Debug.WriteLine("receiveRPC: " + msg.ToString());

			if (msg is Erlang.Tuple)
			{
				Erlang.Tuple t = (Erlang.Tuple) msg;
				if (t.arity() == 2)
					return t.elementAt(1);
				// obs: second element
			}
			
			return null;
		}
		
		
		/*
		* Create a link between the local node and the specified process on
		* the remote node. If the link is still active when the remote
		* process terminates, an exit signal will be sent to this
		* connection. Use {@link #unlink unlink()} to remove the link.
		*
		* @param dest the Erlang PID of the remote process.
		*
		* @exception C#.io.IOException if the connection is not active
		* or a communication error occurs.
		**/
		public virtual void  link(Erlang.Pid dest)
		{
			base.sendLink(this._self.pid(), dest);
		}
		
		
		/*
		* Remove a link between the local node and the specified process on
		* the remote node. This method deactivates links created with
		* {@link #link link()}.
		*
		* @param dest the Erlang PID of the remote process.
		*
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		**/
		public virtual void  unlink(Erlang.Pid dest)
		{
			base.sendUnlink(this._self.pid(), dest);
		}
		
		
		/*
		* Send an exit signal to a remote process.
		* 
		* @param dest the Erlang PID of the remote process.
		* @param reason a string describing the exit reason.
		* 
		* @exception C#.io.IOException if the connection is not active or
		* a communication error occurs.
		**/
		public virtual void  exit(Erlang.Pid dest, System.String reason)
		{
			base.sendExit2(this._self.pid(), dest, reason);
		}
	}
}
Back to Top