コード例 #1
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);
            }
        }
コード例 #2
0
ファイル: OtpEpmd.cs プロジェクト: bmizerany/jungerl
        /*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;

            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)
                    System.Console.Out.WriteLine("-> 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)
                            System.Console.Out.WriteLine("<- OK");
                        return s; // success
                    }
                }
            }
            catch (System.IO.IOException)
            {
                // epmd closed the connection = fail
                if (s != null)
                    s.Close();
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (no response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }
            catch (Erlang.DecodeException)
            {
                if (s != null)
                    s.Close();
                if (traceLevel >= traceThreshold)
                    System.Console.Out.WriteLine("<- (invalid response)");
                throw new System.IO.IOException("Nameserver not responding on " + node.host() + " when publishing " + node.getAlive());
            }

            if (s != null)
                s.Close();
            return null;
        }