Example #1
0
 private void ctor(ErlLocalNode home)
 {
     m_TcpKeepAlive = home.TcpKeepAlive;
     m_TcpLinger = home.TcpLinger;
     m_TcpNoDelay = home.TcpNoDelay;
     m_TcpRcvBufSize = home.TcpRcvBufSize;
     m_TcpSndBufSize = home.TcpSndBufSize;
 }
Example #2
0
 internal ErlMbox(ErlLocalNode home, ErlPid self, ErlAtom name)
 {
     m_Self     = self;
     m_Node     = home;
     m_RegName  = name;
     m_Queue    = new ErlBlockingQueue <IQueable>();
     m_Links    = new ErlLinks();
     m_Monitors = new ErlMonitors(this);
 }
Example #3
0
 private ErlAbstractConnection(ErlLocalNode home, ErlRemoteNode peer, TcpClient s, ErlAtom?cookie = null)
 {
     m_Peer      = peer;
     m_Home      = home;
     m_TcpClient = s;
     m_Cookie    = !cookie.HasValue || cookie.Value == ErlAtom.Null
   ? (peer.Cookie == ErlAtom.Null ? home.Cookie : peer.Cookie) : cookie.Value;
     m_SentBytes        = 0;
     m_ReceivedBytes    = 0;
     m_MaxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
 }
Example #4
0
 private ErlAbstractConnection(ErlLocalNode home, ErlRemoteNode peer, TcpClient s, ErlAtom? cookie = null)
 {
   m_Peer = peer;
   m_Home = home;
   m_TcpClient = s;
   m_Cookie = !cookie.HasValue || cookie.Value == ErlAtom.Null
       ? (peer.Cookie == ErlAtom.Null ? home.Cookie : peer.Cookie) : cookie.Value;
   m_SentBytes = 0;
   m_ReceivedBytes = 0;
   m_MaxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
 }
Example #5
0
        public void Configure(IConfigSectionNode node)
        {
            var appRoot = node.NavigateSection("/" + ErlConsts.ERLANG_CONFIG_SECTION);

            if (appRoot == null)
            {
                throw new ErlException(
                          StringConsts.CONFIGURATION_NAVIGATION_SECTION_REQUIRED_ERROR,
                          ErlConsts.ERLANG_CONFIG_SECTION);
            }

            // Configure global node variables

            ErlAbstractNode.s_DefaultCookie = new ErlAtom(
                appRoot.AttrByName(ErlConsts.ERLANG_COOKIE_ATTR)
                .ValueAsString(ErlAbstractNode.s_DefaultCookie.Value));

            ErlAbstractNode.s_UseShortNames =
                appRoot.AttrByName(ErlConsts.ERLANG_SHORT_NAME_ATTR)
                .ValueAsBool(ErlAbstractNode.s_UseShortNames);

            ErlAbstractConnection.ConnectTimeout =
                appRoot.AttrByName(ErlConsts.ERLANG_CONN_TIMEOUT_ATTR)
                .ValueAsInt(ErlAbstractConnection.ConnectTimeout);

            // Configure local node and remote connections

            var cfg = new MemoryConfiguration();

            cfg.CreateFromNode(appRoot);

            var root  = cfg.Root;
            var nodes = root.Children
                        .Where(n => n.Name.EqualsIgnoreCase(ErlConsts.ERLANG_NODE_SECTION));

            var localNodes = nodes.Where(n => n.AttrByName(ErlConsts.CONFIG_IS_LOCAL_ATTR).ValueAsBool()).ToArray();

            if (localNodes.Length != 1)
            {
                throw new ErlException(StringConsts.ERL_CONFIG_SINGLE_NODE_ERROR, localNodes.Length);
            }

            var localNode = localNodes[0];

            // Create and configure local node

            s_Node = new ErlLocalNode(localNode.Value, localNode);

            // Configure connections to all remote nodes

            //m_AllNodes = nodes.Where(n => !n.AttrByName(ErlConsts.CONFIG_IS_LOCAL_ATTR).ValueAsBool());
            m_AllNodes = root;
        }
 private ErlAbstractConnection(ErlLocalNode home, ErlRemoteNode peer, IErlTransport s, ErlAtom? cookie = null)
 {
   m_Peer = peer;
   m_Home = home;
   m_Transport = s;
   m_Cookie = !cookie.HasValue || cookie.Value == ErlAtom.Null
       ? (peer.Cookie == ErlAtom.Null ? home.Cookie : peer.Cookie) : cookie.Value;
   m_SentBytes = 0;
   m_ReceivedBytes = 0;
   m_MaxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
   if (m_Transport != null)
     m_Transport.Trace += (o, t, d, msg) => home.OnTrace(t, d, msg);
 }
Example #7
0
 private ErlAbstractConnection(ErlLocalNode home, ErlRemoteNode peer, IErlTransport s, ErlAtom?cookie = null)
 {
     m_Peer      = peer;
     m_Home      = home;
     m_Transport = s;
     m_Cookie    = !cookie.HasValue || cookie.Value == ErlAtom.Null
   ? (peer.Cookie == ErlAtom.Null ? home.Cookie : peer.Cookie) : cookie.Value;
     m_SentBytes        = 0;
     m_ReceivedBytes    = 0;
     m_MaxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH;
     if (m_Transport != null)
     {
         m_Transport.Trace += (o, t, d, msg) => home.OnTrace(t, d, msg);
     }
 }
Example #8
0
    public void BeforeTest()
    {
      var node = new ErlLocalNode("test@localhost", true, false);
      node.Start();
      ErlApp.Node = node;

      store = new ErlDataStore();

      store.RemoteName = REMOTE_NAME;
      store.RemoteCookie = REMOTE_COOKIE;
      store.QueryResolver.ScriptAssembly = SCRIPT_ASM;
      //store.QueryResolver.RegisterHandlerLocation("NFX.NUnit.Integration.CRUD.ErlSpecific, NFX.NUnit.Integration");
      store.Start();

      clearAll();
    }
Example #9
0
        /// <summary>
        /// Intiate and open a connection to a remote node
        /// </summary>
        protected ErlAbstractConnection(ErlLocalNode self, ErlRemoteNode other, ErlAtom?cookie = null, bool connect = true)
            : this(self, other, null, cookie)
        {
            // now find highest common dist value
            if ((m_Peer.Proto != self.Proto) || (self.DistHigh < m_Peer.DistLow) || (self.DistLow > m_Peer.DistHigh))
            {
                throw new ErlException(StringConsts.ERL_CONN_NO_COMMON_PROTO_ERROR);
            }

            // highest common version: min(m_Peer.distHigh, self.distHigh)
            m_Peer.DistChoose = Math.Min(m_Peer.DistHigh, self.DistHigh);

            m_Connected = false;

            if (connect)
            {
                Connect();
            }
        }
Example #10
0
        /// <summary>
        /// Send an auth error to peer because he sent a bad cookie
        /// The auth error uses his cookie (not revealing ours).
        /// This is just like send_reg otherwise
        /// </summary>
        private void cookieError(ErlLocalNode local, ErlAtom cookie)
        {
            var header = new ErlOutputStream(writeVersion: false, capacity: HEADER_LEN);

            // preamble: 4 byte length + "PASS_THROUGH" tag + version
            header.Write4BE(0); // reserve space for length
            header.Write1(PASS_THROUGH);
            header.Write1((byte)ErlExternalTag.Version);

            header.WriteTupleHead(4);
            header.WriteLong((long)ErlMsg.Tag.RegSend);
            header.WritePid(local.CreatePid()); // disposable pid
            header.WriteAtom(cookie);           // important: his cookie, not mine...
            header.WriteAtom("auth");

            // version for payload written later by the payload stream
            //header.Write1((byte)ErlExternalTag.Version);

            // the payload

            // the no_auth message (copied from Erlang) Don't change this (Erlang will crash)
            // {$gen_cast, {print, "~n** Unauthorized cookie ~w **~n", [foo@aule]}}

            var msg = ErlObject.Parse(
                "{'$gen_cast', {print, \"\n** Unauthorized cookie ~w **\n\", [" + local.NodeName.Value + "]}}");

            var payload = new ErlOutputStream(msg, writeVersion: true);

            // fix up length in preamble
            header.Poke4BE(0, (int)(header.Length + payload.Length - 4));

            try
            {
                DoSend(header, payload);
            }
            catch (Exception e)
            {
                Close();
                throw new ErlException(StringConsts.ERL_CONN_UNAUTH_COOKIE_ERROR.Args(cookie.Value), e);
            }
        }
Example #11
0
        /// <summary>
        /// Publish node's port at local EPMD, so that other nodes can connect to it.
        ///
        /// On failure to connect to EPMD the function may throw if the value of
        /// ErlApp.IgnoreLocalEpmdConnectErrors variable is true.
        ///
        /// On failed connection attempt the function calls
        /// node.OnEpmdFailedConnectAttempt delegate
        /// </summary>
        /// <remarks>
        /// This function will get an exception if it tries to talk to an r3
        /// epmd, or if something else happens that it cannot forsee. In both
        /// cases we return an exception (and the caller should try again, using
        /// the r3 protocol).
        ///
        /// If we manage to successfully communicate with an r4 epmd, we return
        /// either the socket, or null, depending on the result
        /// </remarks>
        public static bool PublishPort(ErlLocalNode node)
        {
            Exception error = null;
              IErlTransport s = null;

              try
              {
            ErlOutputStream obuf = new ErlOutputStream(writeVersion: false, capacity: node.AliveName.Length + 20);
            s = node.Epmd ?? new ErlTcpTransport(ErlLocalNode.LocalHost, EPMD_PORT) { NodeName = node.NodeName.Value };

            obuf.Write2BE(node.AliveName.Length + 13);

            obuf.Write1((byte)Indicator.Publish4req);
            obuf.Write2BE(node.Port);

            obuf.Write1((byte)node.Ntype);

            obuf.Write1((byte)node.Proto);
            obuf.Write2BE(node.DistHigh);
            obuf.Write2BE(node.DistLow);

            obuf.Write2BE(node.AliveName.Length);
            var buf = Encoding.ASCII.GetBytes(node.AliveName);
            //UPGRADE_NOTE: This code will be optimized in the future;
            obuf.Write(buf);
            obuf.Write2BE(0); // No extra

            // send request
            obuf.WriteTo(s.GetStream());

            node.OnTrace(ErlTraceLevel.Epmd, Direction.Outbound, () =>
                StringConsts.ERL_EPMD_PUBLISH.Args(node.AliveName, node.Port, "r4"));

            // get reply
            buf = new byte[100];
            int n = s.GetStream().Read(buf, 0, buf.Length);

            if (n < 0)
              // this was an r3 node => not a failure (yet)
              throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));

            ErlInputStream ibuf = new ErlInputStream(buf, 0, n, checkVersion: false);

            if (Indicator.Publish4resp == (Indicator)ibuf.Read1())
            {
              int result = ibuf.Read1();
              if (result == 0)
              {
            node.Creation = (byte)ibuf.Read2BE();
            node.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_OK);

            node.Epmd = s;
            return true; // success
              }
            }
            error = new ErlException("Cannot register node '{0}' (not unique?)".Args(node.AliveName));
              }
              catch (Exception e)
              {
            error = e;
              }

              // epmd closed the connection = fail
              if (s != null)
            try { s.Close(); } catch { }

              node.Epmd = null;

              node.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_NO_RESPONSE);

              node.OnEpmdFailedConnectAttempt(node.NodeName, error.Message);

              node.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_FAILED_TO_CONNECT_ERROR);

              if (!ErlApp.IgnoreLocalEpmdConnectErrors)
            throw new ErlException(StringConsts.ERL_EPMD_FAILED_TO_CONNECT_ERROR + ": " + error.Message);

              node.Creation = 0;
              return false;
        }
Example #12
0
        /// <summary>
        /// Unregister from Epmd.
        /// Other nodes wishing to connect will no longer be able to
        /// </summary>
        public static void UnPublishPort(ErlLocalNode node)
        {
            IErlTransport s = null;

              try
              {
            s = node.Epmd ?? new ErlTcpTransport(ErlLocalNode.LocalHost, EPMD_PORT) { NodeName = node.NodeName.Value };
            ErlOutputStream obuf = new ErlOutputStream(
            writeVersion: false, capacity: node.AliveName.Length + 8);
            obuf.Write2BE(node.AliveName.Length + 1);
            obuf.Write1((byte)Indicator.StopReq);
            var buf = Encoding.ASCII.GetBytes(node.AliveName);
            obuf.Write(buf);
            obuf.WriteTo(s.GetStream());

            node.OnTrace(ErlTraceLevel.Epmd, Direction.Outbound, () =>
                StringConsts.ERL_EPMD_UNPUBLISH.Args(node.NodeName.Value, node.Port));
              }
              catch
              {
            s = null;
              }
              finally
              {
            if (s != null)
              try { s.Close(); } catch { }
            node.Epmd = null;
              }
        }
Example #13
0
 /// <summary>
 /// Create a peer node
 /// </summary>
 public ErlRemoteNode(ErlLocalNode home, ErlAtom toNode, ErlAtom? cookie = null)
     : base(toNode, cookie.HasValue ? cookie.Value : home.Cookie, home.UseShortName)
 {
     ctor(home);
 }
Example #14
0
 /// <summary>
 /// Create a connection to a remote node
 /// </summary>
 /// <param name="self">the local node from which you wish to connect</param>
 /// <returns>A connection to the remote node</returns>
 public ErlConnection Connect(ErlLocalNode self)
 {
     return(new ErlConnection(self, this));
 }
Example #15
0
 /// <summary>
 /// Intiate and open a connection to a remote node
 /// </summary>
 internal ErlConnection(ErlLocalNode node, ErlRemoteNode peer, bool connect = true)
     : base(node, peer, connect: connect)
 {
   _ctor(StringConsts.ERL_CONNECTION.Args(m_Home.NodeName.Value, "->", peer.NodeName.Value));
 }
Example #16
0
 internal ErlMbox(ErlLocalNode home, ErlPid self, ErlAtom name)
 {
   m_Self = self;
   m_Node = home;
   m_RegName = name;
   m_Queue = new ErlBlockingQueue<IQueable>();
   m_Links = new ErlLinks();
   m_Monitors = new ErlMonitors(this);
 }
Example #17
0
        private long m_SentMsgs; // Total number of messages sent

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Accept an incoming connection from a remote node. Used by ErlLocalNode.Accept
        /// to create a connection
        /// based on data received when handshaking with the peer node, when
        /// the remote node is the connection intitiator.
        /// </summary>
        protected ErlAbstractConnection(ErlLocalNode home, IErlTransport s)
            : this(home, new ErlRemoteNode(home), s, home.Cookie)
        {
            setSockOpts();
              //this.socket.ReceiveTimeout = 5000;

              home.OnTrace(ErlTraceLevel.Handshake, Direction.Inbound, () =>
              StringConsts.ERL_CONN_ACCEPT_FROM.Args(
                  IPAddress.Parse(s.RemoteEndPoint.ToString()).ToString(),
                  (s.RemoteEndPoint as IPEndPoint).Port.ToString()));

              // get his info
              recvName(m_Peer);

              // now find highest common dist value
              if ((m_Peer.Proto != home.Proto) || (home.DistHigh < m_Peer.DistLow) || (home.DistLow > m_Peer.DistHigh))
              {
            Close();
            throw new ErlException(StringConsts.ERL_CONN_NO_COMMON_PROTO_ERROR);
              }

              // highest common protocol version
              m_Peer.DistChoose = Math.Min(m_Peer.DistHigh, home.DistHigh);

              doAccept();
        }
Example #18
0
    /// <summary>
    /// Send an auth error to peer because he sent a bad cookie
    /// The auth error uses his cookie (not revealing ours).
    /// This is just like send_reg otherwise
    /// </summary>
    private void cookieError(ErlLocalNode local, ErlAtom cookie)
    {
      var header = new ErlOutputStream(writeVersion: false, capacity: HEADER_LEN);

      // preamble: 4 byte length + "PASS_THROUGH" tag + version
      header.Write4BE(0); // reserve space for length
      header.Write1(PASS_THROUGH);
      header.Write1((byte)ErlExternalTag.Version);

      header.WriteTupleHead(4);
      header.WriteLong((long)ErlMsg.Tag.RegSend);
      header.WritePid(local.CreatePid()); // disposable pid
      header.WriteAtom(cookie); // important: his cookie, not mine...
      header.WriteAtom("auth");

      // version for payload written later by the payload stream
      //header.Write1((byte)ErlExternalTag.Version);

      // the payload

      // the no_auth message (copied from Erlang) Don't change this (Erlang will crash)
      // {$gen_cast, {print, "~n** Unauthorized cookie ~w **~n", [foo@aule]}}

      var msg = ErlObject.Parse(
          "{'$gen_cast', {print, \"\n** Unauthorized cookie ~w **\n\", [" + local.NodeName.Value + "]}}");

      var payload = new ErlOutputStream(msg, writeVersion: true);

      // fix up length in preamble
      header.Poke4BE(0, (int)(header.Length + payload.Length - 4));

      try
      {
        DoSend(header, payload);
      }
      catch (Exception e)
      {
        Close();
        throw new ErlException(StringConsts.ERL_CONN_UNAUTH_COOKIE_ERROR.Args(cookie.Value), e);
      }
    }
Example #19
0
        /// <summary>
        /// Determine what port a node listens for incoming connections on
        /// </summary>
        /// <param name="home">Local node</param>
        /// <param name="node">Remote lode for which to look up the port number from remote EPMD</param>
        /// <param name="closeSocket">If true, close the connection to remote EPMD at return</param>
        /// <returns>the listen port for the specified node, or 0 if the node
        /// was not registered with Epmd</returns>
        public static int LookupPort(ErlLocalNode home, ErlRemoteNode node,
                                     bool closeSocket = false)
        {
            TcpClient s = null;

            try
            {
                var obuf = new ErlOutputStream(
                    writeVersion: false, capacity: 4 + 3 + node.AliveName.Length + 1);
                s = node.Epmd ?? new TcpClient(node.Host, EPMD_PORT);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.Write2BE(node.AliveName.Length + 1);
                obuf.Write1((byte)Indicator.Port4req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] buf = Encoding.ASCII.GetBytes(node.AliveName);
                obuf.Write(buf);

                // send request
                obuf.WriteTo(s.GetStream());

                home.Trace(ErlTraceLevel.Epmd, Direction.Outbound,
                           StringConsts.ERL_EPMD_LOOKUP_R4.Args(node.NodeName.Value));

                // receive and decode reply
                // resptag[1], result[1], port[2], ntype[1], proto[1],
                // disthigh[2], distlow[2], nlen[2], alivename[n],
                // elen[2], edata[m]
                buf = new byte[100];

                int n = s.GetStream().Read(buf, 0, buf.Length);

                if (n < 0)
                {
                    throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));
                }

                var ibuf = new ErlInputStream(buf, 0, n, checkVersion: false);

                if (Indicator.Port4resp == (Indicator)ibuf.Read1())
                {
                    if (0 == ibuf.Read1())
                    {
                        node.Port = ibuf.Read2BE();

                        node.Ntype    = (ErlAbstractNode.NodeType)ibuf.Read1();
                        node.Proto    = ibuf.Read1();
                        node.DistHigh = ibuf.Read2BE();
                        node.DistLow  = ibuf.Read2BE();
                        // ignore rest of fields
                    }
                }

                if (!closeSocket)
                {
                    node.Epmd = s;
                    s         = null;
                }
            }
            catch (Exception)
            {
                home.Trace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_INVALID_RESPONSE_ERROR);
                throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));
            }
            finally
            {
                if (s != null)
                {
                    try { s.Close(); } catch {}
                }
                s = null;
            }

            home.Trace(ErlTraceLevel.Epmd, Direction.Inbound, () =>
                       node.Port == 0 ? StringConsts.ERL_EPMD_NOT_FOUND : StringConsts.ERL_EPMD_PORT.Args(node.Port));

            return(node.Port);
        }
Example #20
0
 /// <summary>
 /// Create a peer node
 /// </summary>
 public ErlRemoteNode(ErlLocalNode home, string toNode, ErlAtom? cookie = null)
     : base(toNode, cookie ?? home.Cookie, home.UseShortName)
 {
     ctor(home);
 }
Example #21
0
        /// <summary>
        /// Publish node's port at local EPMD, so that other nodes can connect to it.
        ///
        /// On failure to connect to EPMD the function may throw if the value of
        /// ErlApp.IgnoreLocalEpmdConnectErrors variable is true.
        ///
        /// On failed connection attempt the function calls
        /// node.OnEpmdFailedConnectAttempt delegate
        /// </summary>
        /// <remarks>
        /// This function will get an exception if it tries to talk to an r3
        /// epmd, or if something else happens that it cannot forsee. In both
        /// cases we return an exception (and the caller should try again, using
        /// the r3 protocol).
        ///
        /// If we manage to successfully communicate with an r4 epmd, we return
        /// either the socket, or null, depending on the result
        /// </remarks>
        public static bool PublishPort(ErlLocalNode node)
        {
            Exception error = null;
            TcpClient s     = null;

            try
            {
                ErlOutputStream obuf = new ErlOutputStream(
                    writeVersion: false, capacity: node.AliveName.Length + 20);
                s = node.Epmd ?? new TcpClient(ErlLocalNode.LocalHost, EPMD_PORT);

                obuf.Write2BE(node.AliveName.Length + 13);

                obuf.Write1((byte)Indicator.Publish4req);
                obuf.Write2BE(node.Port);

                obuf.Write1((byte)node.Ntype);

                obuf.Write1((byte)node.Proto);
                obuf.Write2BE(node.DistHigh);
                obuf.Write2BE(node.DistLow);

                obuf.Write2BE(node.AliveName.Length);
                var buf = Encoding.ASCII.GetBytes(node.AliveName);
                //UPGRADE_NOTE: This code will be optimized in the future;
                obuf.Write(buf);
                obuf.Write2BE(0); // No extra

                // send request
                obuf.WriteTo(s.GetStream());

                node.Trace(ErlTraceLevel.Epmd, Direction.Outbound, () =>
                           StringConsts.ERL_EPMD_PUBLISH.Args(node.AliveName, node.Port, "r4"));

                // get reply
                buf = new byte[100];
                int n = s.GetStream().Read(buf, 0, buf.Length);

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));
                }

                ErlInputStream ibuf = new ErlInputStream(buf, 0, n, checkVersion: false);

                if (Indicator.Publish4resp == (Indicator)ibuf.Read1())
                {
                    int result = ibuf.Read1();
                    if (result == 0)
                    {
                        node.Creation = (byte)ibuf.Read2BE();
                        node.Trace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_OK);

                        node.Epmd = s;
                        return(true); // success
                    }
                }
                error = new ErlException("Cannot register node '{0}' (not unique?)".Args(node.AliveName));
            }
            catch (Exception e)
            {
                error = e;
            }

            // epmd closed the connection = fail
            if (s != null)
            {
                try { s.Close(); } catch {}
            }

            node.Epmd = null;

            node.Trace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_NO_RESPONSE);

            node.OnEpmdFailedConnectAttempt(node.NodeName, error.Message);

            node.Trace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_FAILED_TO_CONNECT_ERROR);

            if (!ErlApp.IgnoreLocalEpmdConnectErrors)
            {
                throw new ErlException(error.Message);
            }

            node.Creation = 0;
            return(false);
        }
Example #22
0
 /// <summary>
 /// Create a peer node
 /// </summary>
 public ErlRemoteNode(ErlLocalNode home, ErlAtom toNode, IConfigSettings config, ErlAtom? cookie = null)
     : base(toNode, cookie ?? home.Cookie, home.UseShortName)
 {
     ctor(home);
 }
Example #23
0
 /// <summary>
 /// Constructor used for creating a remote node by the Acceptor of incoming connections
 /// </summary>
 /// <param name="home"></param>
 internal ErlRemoteNode(ErlLocalNode home) : this(home, ErlAtom.Null)
 {}
Example #24
0
        /// <summary>
        /// Determine what port a node listens for incoming connections on
        /// </summary>
        /// <param name="home">Local node</param>
        /// <param name="node">Remote lode for which to look up the port number from remote EPMD</param>
        /// <param name="closeSocket">If true, close the connection to remote EPMD at return</param>
        /// <returns>the listen port for the specified node, or 0 if the node
        /// was not registered with Epmd</returns>
        public static int LookupPort(ErlLocalNode home, ErlRemoteNode node,
            bool closeSocket = false)
        {
            IErlTransport s = null;

              try
              {
            var obuf = new ErlOutputStream(writeVersion: false, capacity: 4 + 3 + node.AliveName.Length + 1);
            s = node.Epmd;

            if (s == null)
            {
              //create transport
              s = ErlTransportFactory.Create(node.TransportClassName, node.NodeName.Value);

              s.Trace += (o, t, d, msg) => home.OnTrace(t, d, msg);

              //append SSH params to transport
              node.AppendSSHParamsToTransport(s);

              //connect (I am not sure)
              s.Connect(node.Host, EPMD_PORT, node.ConnectTimeout);
            }

            // build and send epmd request
            // length[2], tag[1], alivename[n] (length = n+1)
            obuf.Write2BE(node.AliveName.Length + 1);
            obuf.Write1((byte)Indicator.Port4req);
            //UPGRADE_NOTE: This code will be optimized in the future;
            byte[] buf = Encoding.ASCII.GetBytes(node.AliveName);
            obuf.Write(buf);

            // send request
            obuf.WriteTo(s.GetStream());

            home.OnTrace(ErlTraceLevel.Epmd, Direction.Outbound,
                StringConsts.ERL_EPMD_LOOKUP_R4.Args(node.NodeName.Value));

            // receive and decode reply
            // resptag[1], result[1], port[2], ntype[1], proto[1],
            // disthigh[2], distlow[2], nlen[2], alivename[n],
            // elen[2], edata[m]
            buf = new byte[100];

            int n = s.GetStream().Read(buf, 0, buf.Length);

            if (n < 0)
              throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName));

            var ibuf = new ErlInputStream(buf, 0, n, checkVersion: false);

            if (Indicator.Port4resp == (Indicator)ibuf.Read1())
            {
              if (0 == ibuf.Read1())
              {
            node.Port = ibuf.Read2BE();

            node.Ntype = (ErlAbstractNode.NodeType)ibuf.Read1();
            node.Proto = ibuf.Read1();
            node.DistHigh = ibuf.Read2BE();
            node.DistLow = ibuf.Read2BE();
            // ignore rest of fields
              }
            }

            if (!closeSocket)
            {
              node.Epmd = s;
              s = null;
            }
              }
              catch (Exception e)
              {
            home.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, StringConsts.ERL_EPMD_INVALID_RESPONSE_ERROR.Args(node.Host, node.AliveName, e.ToString()));
            throw new ErlException(StringConsts.ERL_EPMD_NOT_RESPONDING.Args(node.Host, node.AliveName, e.ToString()));
              }
              finally
              {
            if (s != null)
              try { s.Close(); } catch { }
            s = null;
              }

              home.OnTrace(ErlTraceLevel.Epmd, Direction.Inbound, () =>
              node.Port == 0 ? StringConsts.ERL_EPMD_NOT_FOUND : StringConsts.ERL_EPMD_PORT.Args(node.Port));

              return node.Port;
        }
Example #25
0
 /// <summary>
 /// Create a peer node
 /// </summary>
 public ErlRemoteNode(ErlLocalNode home, ErlAtom toNode, ErlAtom?cookie = null)
     : base(toNode, cookie.HasValue ? cookie.Value : home.Cookie, home.UseShortName)
 {
     ctor(home);
 }
Example #26
0
        public void Configure(IConfigSectionNode node)
        {
            var appRoot = node.NavigateSection("/" + ErlConsts.ERLANG_CONFIG_SECTION);

              if (appRoot == null)
            throw new ErlException(
              StringConsts.CONFIGURATION_NAVIGATION_SECTION_REQUIRED_ERROR,
              ErlConsts.ERLANG_CONFIG_SECTION);

              // Configure global node variables

              ErlAbstractNode.s_DefaultCookie = new ErlAtom(
            appRoot.AttrByName(ErlConsts.ERLANG_COOKIE_ATTR)
            .ValueAsString(ErlAbstractNode.s_DefaultCookie.Value));

              ErlAbstractNode.s_UseShortNames =
            appRoot.AttrByName(ErlConsts.ERLANG_SHORT_NAME_ATTR)
            .ValueAsBool(ErlAbstractNode.s_UseShortNames);

              ErlAbstractConnection.ConnectTimeout =
            appRoot.AttrByName(ErlConsts.ERLANG_CONN_TIMEOUT_ATTR)
            .ValueAsInt(ErlAbstractConnection.ConnectTimeout);

              // Configure local node and remote connections

              var cfg = new MemoryConfiguration();
              cfg.CreateFromNode(appRoot);

              var root  = cfg.Root;
              var nodes = root.Children
                      .Where(n => n.Name.EqualsIgnoreCase(ErlConsts.ERLANG_NODE_SECTION));

              var localNodes = nodes.Where(n => n.AttrByName(ErlConsts.CONFIG_IS_LOCAL_ATTR).ValueAsBool()).ToArray();
              if (localNodes.Length != 1)
            throw new ErlException(StringConsts.ERL_CONFIG_SINGLE_NODE_ERROR, localNodes.Length);

              var localNode = localNodes[0];

              // Create and configure local node

              s_Node = new ErlLocalNode(localNode.Value, localNode);

              // Configure connections to all remote nodes

              //m_AllNodes = nodes.Where(n => !n.AttrByName(ErlConsts.CONFIG_IS_LOCAL_ATTR).ValueAsBool());
              m_AllNodes = root;
        }
Example #27
0
 /// <summary>
 /// Constructor used for creating a remote node by the Acceptor of incoming connections
 /// </summary>
 /// <param name="home"></param>
 internal ErlRemoteNode(ErlLocalNode home) : this(home, ErlAtom.Null)
 {
 }
Example #28
0
 /// <summary>
 /// Accept an incoming connection from a remote node
 /// </summary>
 public ErlConnection(ErlLocalNode node, TcpClient peer)
     : base(node, new ErlTcpTransport(peer))
 {
   _ctor(StringConsts.ERL_CONNECTION.Args(m_Home.NodeName.Value, "<-", peer.ToString()));
 }
Example #29
0
 /// <summary>
 /// Create a peer node
 /// </summary>
 public ErlRemoteNode(ErlLocalNode home, string toNode, ErlAtom?cookie = null)
     : base(toNode, cookie ?? home.Cookie, home.UseShortName)
 {
     ctor(home);
 }
Example #30
0
 /// <summary>
 /// Create a mailbox with optional name
 /// </summary>
 internal ErlMbox(ErlLocalNode home, ErlPid self, string name = null)
     : this(home, self, name == null ? ErlAtom.Null : new ErlAtom(name))
 { }
Example #31
0
 /// <summary>
 /// Create a peer node
 /// </summary>
 public ErlRemoteNode(ErlLocalNode home, ErlAtom toNode, IConfigSettings config, ErlAtom?cookie = null)
     : base(toNode, cookie ?? home.Cookie, home.UseShortName)
 {
     ctor(home);
 }
Example #32
0
 /// <summary>
 /// Accept an incoming connection from a remote node
 /// </summary>
 public ErlConnection(ErlLocalNode node, TcpClient peer)
     : base(node, new ErlTcpTransport(peer))
 {
     _ctor(StringConsts.ERL_CONNECTION.Args(m_Home.NodeName.Value, "<-", peer.ToString()));
 }
Example #33
0
        /// <summary>
        /// Create a connection to a remote node
        /// </summary>
        /// <param name="self">the local node from which you wish to connect</param>
        /// <returns>A connection to the remote node</returns>
		public ErlConnection Connect(ErlLocalNode self)
		{
			return new ErlConnection(self, this);
		}
Example #34
0
        /// <summary>
        /// Intiate and open a connection to a remote node
        /// </summary>
        protected ErlAbstractConnection(ErlLocalNode self, ErlRemoteNode other, ErlAtom? cookie = null, bool connect = true)
            : this(self, other, null, cookie)
        {
            // now find highest common dist value
              if ((m_Peer.Proto != self.Proto) || (self.DistHigh < m_Peer.DistLow) || (self.DistLow > m_Peer.DistHigh))
            throw new ErlException(StringConsts.ERL_CONN_NO_COMMON_PROTO_ERROR);

              // highest common version: min(m_Peer.distHigh, self.distHigh)
              m_Peer.DistChoose = Math.Min(m_Peer.DistHigh, self.DistHigh);

              m_Connected = false;

              if (connect)
            Connect();
        }
Example #35
0
 /// <summary>
 /// Intiate and open a connection to a remote node
 /// </summary>
 internal ErlConnection(ErlLocalNode node, ErlRemoteNode peer, bool connect = true)
     : base(node, peer, connect: connect)
 {
     _ctor(StringConsts.ERL_CONNECTION.Args(m_Home.NodeName.Value, "->", peer.NodeName.Value));
 }
Example #36
0
 /// <summary>
 /// Create a mailbox with optional name
 /// </summary>
 internal ErlMbox(ErlLocalNode home, ErlPid self, string name = null)
     : this(home, self, name == null ? ErlAtom.Null : new ErlAtom(name))
 {
 }
Example #37
0
    private void btnErlang_Click(object sender, EventArgs e)
    {
        //connect to erlang
        var n = new ErlLocalNode("b", new ErlAtom("hahaha"));
        n.AcceptConnections = false;
        n.Start();

        var m = n.CreateMbox("test");

        var res = n.Send(m.Self, "[email protected]", "me", new ErlString("Hello! " + DateTime.Now));
        if (!res)
            Console.WriteLine("Can not send message");
        else
            Console.WriteLine("Message sent");
    }