예제 #1
0
        // Ask epmd to close his end of the connection.
        // Caller should close his epmd socket as well.
        // This method is pretty forgiving...

        /*
         * Unregister from Epmd. Other nodes wishing to connect will no
         * longer be able to.
         *
         * <p> This method does not report any failures.
         **/
        public static void  unPublishPort(OtpLocalNode node)
        {
            TcpClient s = null;

            try
            {
                s = new TcpClient(System.Net.Dns.GetHostName(), epmdPort);
                OtpOutputStream obuf = new OtpOutputStream();
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(stopReq);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);
                obuf.writeTo((System.IO.Stream)s.GetStream());
                // don't even wait for a response (is there one?)
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> UNPUBLISH " + node + " port=" + node.port());
                    OtpTrace.TraceEvent("<- OK (assumed)");
                }
            }
            catch (System.Exception)
            {
                /*ignore all failures */
            }
            finally
            {
                try
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                catch (System.IO.IOException)
                {
                    /*ignore close failure */
                }
                s = null;
            }
        }
예제 #2
0
        /*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.
         */
        private static System.Net.Sockets.TcpClient r4_publish(OtpLocalNode node)
        {
            System.Net.Sockets.TcpClient s     = null;
            System.Exception             error = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new TcpClient(System.Net.Dns.GetHostName(), epmdPort);

                obuf.write2BE(node.getAlive().Length + 13);

                obuf.write1(publish4req);
                obuf.write2BE(node.port());

                obuf.write1(node.type());

                obuf.write1(node.proto());
                obuf.write2BE(node.distHigh());
                obuf.write2BE(node.distLow());

                obuf.write2BE(node.getAlive().Length);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);
                obuf.write2BE(0);                 // No extra

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());

                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> PUBLISH (r4) " + node + " port=" + node.port());
                }

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

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    if (s != null)
                    {
                        s.Close();
                    }
                    throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                int response = ibuf.read1();
                if (response == publish4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        node._creation = ibuf.read2BE();
                        if (traceLevel >= traceThreshold)
                        {
                            OtpTrace.TraceEvent("<- OK");
                        }
                        return(s);                        // success
                    }
                }
            }
            catch (System.IO.IOException e)
            {
                error = e;
            }
            catch (Erlang.Exception e)
            {
                error = e;
            }
            catch (System.Net.Sockets.SocketException e)
            {
                error = e;
            }

            if (error == null)
            {
                return(s);
            }
            else
            {
                // epmd closed the connection = fail
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    System.Console.Out.WriteLine("<- (no response)");
                }

                string err = "Nameserver not responding on " + node.host() + " when publishing " + node.getAlive();
                node.epmdFailedConnAttempt(node.node(), err);

                if (traceLevel >= traceThreshold)
                {
                    System.Console.Out.WriteLine("Failed to connect to empd daemon!");
                }

                if (!OtpLocalNode.ignoreLocalEpmdConnectErrors)
                {
                    throw new System.Exception(err);
                }

                node._creation = 0;
                return(null);
            }
        }
예제 #3
0
        private static System.Net.Sockets.TcpClient r3_publish(OtpLocalNode node)
        {
            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(System.Net.Dns.GetHostName(), epmdPort);

                obuf.write2BE(node.getAlive().Length + 3);

                obuf.write1(publish3req);
                obuf.write2BE(node.port());
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> PUBLISH (r3) " + node + " port=" + node.port());
                }

                byte[] tmpbuf = new byte[100];

                int n = s.GetStream().Read(tmpbuf, 0, 100);

                if (n < 0)
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                    if (traceLevel >= traceThreshold)
                    {
                        OtpTrace.TraceEvent("<- (no response)");
                    }
                    return(null);
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                if (ibuf.read1() == publish3ok)
                {
                    node._creation = ibuf.read2BE();
                    if (traceLevel >= traceThreshold)
                    {
                        OtpTrace.TraceEvent("<- OK");
                    }
                    return(s);                    // success - don't close socket
                }
            }
            catch (System.IO.IOException)
            {
                // epmd closed the connection = fail
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }
            catch (Erlang.Exception)
            {
                if (s != null)
                {
                    s.Close();
                }
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (invalid response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }

            if (s != null)
            {
                s.Close();
            }
            return(null);            // failure
        }
예제 #4
0
        private static int r4_lookupPort(AbstractNode node)
        {
            int port = 0;

            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(node.host(), epmdPort);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(port4req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());

                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> LOOKUP (r4) " + node);
                }

                // 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]
                byte[] tmpbuf = new byte[100];

                int n = s.GetStream().Read(tmpbuf, 0, 100);

                if (n < 0)
                {
                    // this was an r3 node => not a failure (yet)
                    if (s != null)
                    {
                        s.Close();
                    }
                    throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
                }

                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                int response = ibuf.read1();
                if (response == port4resp)
                {
                    int result = ibuf.read1();
                    if (result == 0)
                    {
                        port = ibuf.read2BE();

                        node.ntype     = ibuf.read1();
                        node._proto    = ibuf.read1();
                        node._distHigh = ibuf.read2BE();
                        node._distLow  = ibuf.read2BE();
                        // ignore rest of fields
                    }
                }
            }
            catch (System.IO.IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (Erlang.Exception)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (invalid response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (System.Net.Sockets.SocketException)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no epmd response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            finally
            {
                try
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                catch (System.IO.IOException)
                {
                    /*ignore close errors */
                }
                s = null;
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                {
                    OtpTrace.TraceEvent("<- NOT FOUND");
                }
                else
                {
                    OtpTrace.TraceEvent("<- PORT " + port);
                }
            }
            return(port);
        }
예제 #5
0
        private static int r3_lookupPort(AbstractNode node)
        {
            int port = 0;

            System.Net.Sockets.TcpClient s = null;

            try
            {
                OtpOutputStream obuf = new OtpOutputStream();
                s = new System.Net.Sockets.TcpClient(node.host(), epmdPort);

                // build and send epmd request
                // length[2], tag[1], alivename[n] (length = n+1)
                obuf.write2BE(node.getAlive().Length + 1);
                obuf.write1(port3req);
                //UPGRADE_NOTE: This code will be optimized in the future;
                byte[] tmpBytes;
                int    i;
                string tmpStr;
                tmpStr   = node.getAlive();
                tmpBytes = new byte[tmpStr.Length];
                i        = 0;
                while (i < tmpStr.Length)
                {
                    tmpBytes[i] = (byte)tmpStr[i];
                    i++;
                }
                obuf.writeN(tmpBytes);

                // send request
                obuf.writeTo((System.IO.Stream)s.GetStream());

                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("-> LOOKUP (r3) " + node);
                }

                // receive and decode reply
                byte[] tmpbuf = new byte[100];

                s.GetStream().Read(tmpbuf, 0, 100);
                OtpInputStream ibuf = new OtpInputStream(tmpbuf);

                port = ibuf.read2BE();
            }
            catch (System.IO.IOException)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (no response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            catch (Erlang.Exception)
            {
                if (traceLevel >= traceThreshold)
                {
                    OtpTrace.TraceEvent("<- (invalid response)");
                }
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when looking up " + node.getAlive());
            }
            finally
            {
                try
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                catch (System.IO.IOException)
                {
                    /*ignore close errors */
                }
                s = null;
            }

            if (traceLevel >= traceThreshold)
            {
                if (port == 0)
                {
                    OtpTrace.TraceEvent("<- NOT FOUND");
                }
                else
                {
                    OtpTrace.TraceEvent("<- PORT " + port);
                }
            }
            return(port);
        }
예제 #6
0
파일: Test.cs 프로젝트: trompa/jungerl
        static public void Main(String[] args)
        {
            OtpTrace.TraceEvent("Otp test...");

            if (args.Length < 1)
            {
                OtpTrace.TraceEvent("Usage: Otp sname\n  where sname is" +
                                    "the short name of the Erlang node");
                return;
            }

            String host   = System.Net.Dns.GetHostName();
            String remote = args[0] + "@" + host;

            OtpNode node = new OtpNode("q@" + host);

            OtpTrace.TraceEvent("This node is called {0} and is using cookie='{1}'.",
                                node.node(), node.cookie());
            bool ok = false;

            ok = node.ping(remote, 1000);
            if (ok)
            {
                OtpTrace.TraceEvent("   successfully pinged node " + remote + "\n");
            }
            else
            {
                OtpTrace.TraceEvent("   could not ping node " + remote + "\n");
            }

            OtpMbox mbox = null;

            try
            {
                mbox = node.createMbox();

                Erlang.Object[] rpc  = new Erlang.Object[2];
                Erlang.Object[] call = new Erlang.Object[5];

                call[0] = new Erlang.Atom("call");
                call[1] = new Erlang.Atom("lists");
                call[2] = new Erlang.Atom("reverse");
                call[3] = new Erlang.List(new Erlang.List("Hello Erlang world!"));
                call[4] = mbox.self();

                rpc[0] = mbox.self();
                rpc[1] = new Erlang.Tuple(call);

                Erlang.Tuple rpcTuple = new Erlang.Tuple(rpc);
                OtpTrace.TraceEvent("=> " + rpcTuple.ToString());

                mbox.send("rex", remote, rpcTuple);
                Erlang.Object reply = mbox.receive(1000);

                OtpTrace.TraceEvent("<= " + reply.ToString());
            }
            catch (System.Exception)
            {
            }
            finally
            {
                node.closeMbox(mbox);
            }

            node.close();
        }