PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/OtpTest/Test.cs

https://github.com/saleyn/otp.net
C# | 142 lines | 117 code | 23 blank | 2 comment | 25 complexity | 5ec7947eab69bf2b2215aa0f290d4837 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Otp;
  5. namespace Otp
  6. {
  7. class Test
  8. {
  9. private static void OnReadWrite(AbstractConnection con, AbstractConnection.Operation op,
  10. long lastBytes, long totalBytes, long totalMsgs)
  11. {
  12. System.Console.WriteLine(String.Format("{0} {1} bytes (total: {2} bytes, {3} msgs)",
  13. (op == AbstractConnection.Operation.Read ? "Read " : "Written "),
  14. lastBytes, totalBytes, totalMsgs));
  15. }
  16. static public void Main(String[] args)
  17. {
  18. System.Console.Out.WriteLine("Otp test...");
  19. string cookie = OtpNode.defaultCookie;
  20. string host = System.Net.Dns.GetHostName();
  21. string remote = (args[0].IndexOf('@') < 0) ? args[0] + "@" + host : args[0];
  22. string nodename = Environment.UserName + "123@" + host;
  23. AbstractConnection.traceLevel = OtpTrace.Type.sendThreshold;
  24. if (args.Length < 1)
  25. {
  26. System.Console.Out.WriteLine(
  27. "Usage: {0} remotenode [cookie] [-notrace]\n" +
  28. " nodename - is the name of the remote Erlang node\n" +
  29. " cookie - is the optional cookie string to use\n" +
  30. " -name - this node name\n" +
  31. " -wire - wire-level tracing\n" +
  32. " -notrace - disable debug trace\n",
  33. Environment.GetCommandLineArgs()[0]);
  34. return;
  35. }
  36. else if (args.Length > 1 && args[1][0] != '-')
  37. {
  38. cookie = args[1].ToString();
  39. }
  40. for (int i = 0; i < args.Length; i++) {
  41. if (args[i].Equals("-wire"))
  42. AbstractConnection.traceLevel = OtpTrace.Type.wireThreshold;
  43. else if (args[i].Equals("-notrace"))
  44. AbstractConnection.traceLevel = OtpTrace.Type.defaultLevel;
  45. else if (args[i].Equals("-name") && i+1 < args.Length) {
  46. nodename = args[i++ + 1];
  47. if (nodename.IndexOf('@') < 0)
  48. nodename += '@' + host;
  49. }
  50. }
  51. OtpNode node = new OtpNode(false, nodename, cookie, true);
  52. System.Console.Out.WriteLine("This node is called {0} and is using cookie='{1}'.",
  53. node.node(), node.cookie());
  54. OtpCookedConnection.ConnectTimeout = 2000;
  55. OtpCookedConnection conn = node.connection(remote);
  56. conn.OnReadWrite += OnReadWrite;
  57. if (conn == null)
  58. {
  59. Console.WriteLine("Can't connect to node " + remote);
  60. return;
  61. }
  62. // If using short names or IP address as the host part of the node name,
  63. // get the short name of the peer.
  64. remote = conn.peer.node();
  65. System.Console.Out.WriteLine(" successfully connected to node " + remote + "\n");
  66. OtpMbox mbox = null;
  67. try
  68. {
  69. mbox = node.createMbox();
  70. {
  71. Otp.Erlang.Object reply = mbox.rpcCall(
  72. remote, "lists", "reverse", new Otp.Erlang.List("Abcdef!"));
  73. System.Console.Out.WriteLine("<= [REPLY1]:" + (reply == null ? "null" : reply.ToString()));
  74. }
  75. {
  76. Otp.Erlang.Object reply = mbox.rpcCall(
  77. remote, "global", "register_name",
  78. new Otp.Erlang.List(new Otp.Erlang.Atom("me"), mbox.self()));
  79. System.Console.Out.WriteLine("<= [REPLY2]:" + (reply == null ? "null" : reply.ToString()));
  80. }
  81. {
  82. Otp.Erlang.Object reply = mbox.rpcCall(remote, "global", "register_name", new Otp.Erlang.List(new Otp.Erlang.Atom("me"), mbox.self()), 5000);
  83. System.Console.Out.WriteLine("<= [REPLY3]:" + (reply == null ? "null" : reply.ToString()));
  84. }
  85. {
  86. Otp.Erlang.Object reply = mbox.rpcCall(
  87. remote, "io", "format",
  88. new Otp.Erlang.List(
  89. "Test: ~w -> ~w\n",
  90. new Otp.Erlang.List(mbox.self(), new Otp.Erlang.Atom("ok"))
  91. ));
  92. System.Console.Out.WriteLine("<= [REPLY4]:" + (reply == null ? "null" : reply.ToString()));
  93. }
  94. while (true)
  95. {
  96. Otp.Erlang.Object msg = mbox.receive();
  97. if (msg is Otp.Erlang.Tuple)
  98. {
  99. Otp.Erlang.Tuple m = msg as Otp.Erlang.Tuple;
  100. if (m.arity() == 2 && m.elementAt(0) is Otp.Erlang.Pid)
  101. {
  102. mbox.send(m.elementAt(0) as Otp.Erlang.Pid, m.elementAt(1));
  103. }
  104. }
  105. System.Console.Out.WriteLine("IN msg: " + msg.ToString() + "\n");
  106. }
  107. }
  108. catch (System.Exception e)
  109. {
  110. System.Console.Out.WriteLine("Error: " + e.ToString());
  111. }
  112. finally
  113. {
  114. node.closeMbox(mbox);
  115. }
  116. node.close();
  117. }
  118. }
  119. }