PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/otp.net/OtpTest1/Test1.cs

http://github.com/gebi/jungerl
C# | 133 lines | 107 code | 25 blank | 1 comment | 17 complexity | 57cdb7bb37b124f00d31862f8ab8d33d MD5 | raw file
Possible License(s): AGPL-1.0, JSON, LGPL-2.1, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Otp;
  5. namespace Otp
  6. {
  7. class Test1
  8. {
  9. static public void Main(String[] args)
  10. {
  11. {
  12. Erlang.Object obj1 = Erlang.Object.Format("{a, b, 10, 2.0}");
  13. Erlang.Object obj2 = Erlang.Object.Format("{a, [b, 1, 2.0, \"abc\"], {1, 2}}");
  14. }
  15. System.Console.Out.WriteLine("Otp test...");
  16. string cookie = OtpNode.defaultCookie;
  17. AbstractConnection.traceLevel = OtpTrace.Type.sendThreshold;
  18. if (args.Length < 1)
  19. {
  20. System.Console.Out.WriteLine(
  21. "Usage: {0} nodename [cookie] [-notrace]\n" +
  22. " nodename - is the name of the remote Erlang node\n" +
  23. " cookie - is the optional cookie string to use\n" +
  24. " -notrace - disable debug trace\n",
  25. Environment.GetCommandLineArgs()[0]);
  26. return;
  27. }
  28. else if (args.Length > 1)
  29. {
  30. cookie = args[1].ToString();
  31. }
  32. for (int i = 0; i < args.Length; i++)
  33. if (args[i].Equals("-notrace"))
  34. {
  35. AbstractConnection.traceLevel = OtpTrace.Type.defaultLevel;
  36. break;
  37. }
  38. String host = System.Net.Dns.GetHostName();
  39. String remote = (args[0].IndexOf('@') < 0) ? args[0] + "@" + host : args[0];
  40. OtpNode node = new OtpNode(false, Environment.UserName + "123@" + host, cookie, true);
  41. System.Console.Out.WriteLine("This node is called {0} and is using cookie='{1}'.",
  42. node.node(), node.cookie());
  43. bool ok = node.ping(remote, 1000);
  44. if (!ok)
  45. {
  46. Console.WriteLine("Can't connect to node " + remote);
  47. return;
  48. }
  49. // If using short names, get the short name of the peer.
  50. remote = node.connection(remote).peer.node();
  51. if (remote != null)
  52. System.Console.Out.WriteLine(" successfully pinged node " + remote + "\n");
  53. else
  54. System.Console.Out.WriteLine(" could not ping node " + remote + "\n");
  55. OtpMbox mbox = null;
  56. try
  57. {
  58. mbox = node.createMbox();
  59. {
  60. Otp.Erlang.Object reply = mbox.rpcCall(
  61. remote, "lists", "reverse", new Otp.Erlang.List("Abcdef!"));
  62. System.Console.Out.WriteLine("<= [REPLY1]:" + (reply == null ? "null" : reply.ToString()));
  63. }
  64. {
  65. Otp.Erlang.Object reply = mbox.rpcCall(
  66. remote, "global", "register_name",
  67. new Otp.Erlang.List(new Otp.Erlang.Atom("me"), mbox.self()));
  68. System.Console.Out.WriteLine("<= [REPLY2]:" + (reply == null ? "null" : reply.ToString()));
  69. }
  70. {
  71. Otp.Erlang.Object reply = mbox.rpcCall(remote, "global", "register_name", new Otp.Erlang.List(new Otp.Erlang.Atom("me"), mbox.self()), 5000);
  72. System.Console.Out.WriteLine("<= [REPLY3]:" + (reply == null ? "null" : reply.ToString()));
  73. }
  74. {
  75. Otp.Erlang.Object reply = mbox.rpcCall(
  76. remote, "io", "format",
  77. new Otp.Erlang.List(
  78. "Test: ~w -> ~w\n",
  79. new Otp.Erlang.List(mbox.self(), new Otp.Erlang.Atom("ok"))
  80. ));
  81. System.Console.Out.WriteLine("<= [REPLY4]:" + (reply == null ? "null" : reply.ToString()));
  82. }
  83. while (true)
  84. {
  85. Otp.Erlang.Object msg = mbox.receive();
  86. if (msg is Otp.Erlang.Tuple)
  87. {
  88. Otp.Erlang.Tuple m = msg as Otp.Erlang.Tuple;
  89. if (m.arity() == 2 && m.elementAt(0) is Otp.Erlang.Pid)
  90. {
  91. mbox.send(m.elementAt(0) as Otp.Erlang.Pid, m.elementAt(1));
  92. }
  93. }
  94. System.Console.Out.WriteLine("IN msg: " + msg.ToString() + "\n");
  95. }
  96. }
  97. catch (System.Exception e)
  98. {
  99. System.Console.Out.WriteLine("Error: " + e.ToString());
  100. }
  101. finally
  102. {
  103. node.closeMbox(mbox);
  104. }
  105. node.close();
  106. }
  107. }
  108. }