Exemplo n.º 1
0
        /**
         * Converts the string form of a NetChannelLocation back into its object form
         *
         * @param str
         *            The string representation of a NetChannelLocation
         * @return A new NetChannelLocation created from the String representation
         */
        public static NetChannelLocation parse(String str)
        {
            NodeID nodeID = null;
            int    vcn    = 0;

            try
            {
                if (str.Equals("null", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                if (str.StartsWith("ncl://"))
                {
                    String toParse = str.Substring(6);
                    int    index   = toParse.IndexOf("/");
                    nodeID = NodeID.parse(toParse.Substring(0, index));
                    vcn    = Int32.Parse(toParse.Substring(index + 1));
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e + "String is not a string form of a NetChannelLocation");
            }

            return(new NetChannelLocation(nodeID, vcn));
        }
Exemplo n.º 2
0
        /**
         * Takes the string representation of a NetBarrierLocation and converts it back into an object for usage by JCSP.
         *
         * @param str
         *            The string representation of the NetBarrierLocation
         * @return A NetBarrierLocation produced from a String representation
         * @//throws ArgumentException
         *             Thrown if a non NetBarrierLocation is attempted to be parsed
         */
        public static NetBarrierLocation parse(String str)
        ////throws ArgumentException
        {
            if (str.Equals("null", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }
            // Check that the string starts with nbl://
            if (str.StartsWith("nbl://"))
            {
                // Take the off the starting part of the string
                String toParse = str.Substring(6);
                // Split the string in two
                int index = toParse.IndexOf("/");
                // Parse the NodeID portion
                NodeID nodeID = NodeID.parse(toParse.Substring(0, index));
                // Parse the VBN portion
                int vcn = Int32.Parse(toParse.Substring(index + 1));
                // Return a new NetBarrierLocation created from the two parts
                return(new NetBarrierLocation(nodeID, vcn));
            }

            // We don't have a NetBarrierLocation
            throw new ArgumentException("String is not a string form of a NetBarrierLocation");
        }
Exemplo n.º 3
0
        /**
         * Connects the Link to the remote Node. Exchanges the NodeIDs
         *
         * @return True if the Link successfully connected to the remote Link
         * @//throws JCSPNetworkException
         *             Thrown if something goes wrong during the connection
         */
        public override Boolean connect()
        //throws JCSPNetworkException
        {
            // First check if we are connected.
            if (this.connected)
            {
                return(true);
            }

            // Flag to determine if we are connected at the end of the process.
            Boolean toReturn = false;

            try
            {
                // Write the string representation of our NodeID to the remote Node
                this.txStream.Write(Node.getInstance().getNodeID().toString());
                this.txStream.Flush();

                // Read in the response from the opposite Node
                String response = this.rxStream.ReadString();

                // Either the connection has been accepted (no connection to this Node exists on the opposite Node) or
                // it has not. The opposite Node sends OK in the first instance.
                if (response.Equals("OK", StringComparison.OrdinalIgnoreCase))
                {
                    // The connection is to be kept. Log, and set toReturn to true
                    Node.log.log(this.GetType(), "Link to " + this.remoteAddress.toString() + " connected");
                    toReturn = true;
                }

                // Read in Remote NodeID as string
                NodeID otherID = NodeID.parse(this.rxStream.ReadString());

                // First check we have a tcpip Node connection. This should always be the case
                if (otherID.getNodeAddress() is TCPIPNodeAddress)
                {
                    // Set address and NodeID. If we are not connected then this NodeID can be used to
                    // get the actual Link from the LinkManager
                    this.remoteAddress = (TCPIPNodeAddress)otherID.getNodeAddress();
                    this.remoteID      = otherID;

                    // Set connected to toReturn
                    this.connected = toReturn;
                    return(toReturn);
                }

                // We do not have a tcpip? Should never really happen however. Log and throw Exception
                Node.err.log(this.GetType(), "Tried to connect a TCPIPLink to a non TCPIP connection");
                throw new JCSPNetworkException("Tried to connect a TCPIPLink to a non TCPIP connection");
            }
            catch (IOException ioe)
            {
                // Something went wrong during the connection process. Log and throw exception.
                Node.err.log(this.GetType(), "Failed to connect TCPIPLink to: " + this.remoteAddress.getAddress());
                throw new JCSPNetworkException("Failed to connect TCPIPLink to: " + this.remoteAddress.getAddress());
            }
        }
Exemplo n.º 4
0
 /**
  * Converts the string form of a NetConnectionLocation back into its object form
  *
  * @param str
  *            The string representation of a NetConnectionLocation
  * @return A new NetConnectionLocation created from the String representation
  */
 public static NetConnectionLocation parse(String str)
 {
     if (str.Equals("null", StringComparison.OrdinalIgnoreCase))
     {
         return(null);
     }
     if (str.StartsWith("nconnl://"))
     {
         String   toParse     = str.Substring(6);
         String[] addressBits = toParse.Split("/");
         NodeID   nodeID      = NodeID.parse(addressBits[0]);
         int      vcn         = Int32.Parse(addressBits[1]);
         return(new NetConnectionLocation(nodeID, vcn));
     }
     throw new ArgumentException("String is not a string form of a NetConnectionLocation");
 }
Exemplo n.º 5
0
        /**
         * The run method for the TCPIPLinkServer process
         */
        public override void run()
        {
            // Log start of Link Server
            Node.log.log(this.GetType(), "TCPIP Link Server started on " + this.listeningAddress.getAddress());
            try
            {
                // Now we loop until something goes wrong
                while (true)
                {
                    // Receive incoming connection
                    Socket incoming = this.serv.Accept();

                    //IPEndPoint remoteEndPoint = (IPEndPoint) incoming.RemoteEndPoint;
                    //Debug.WriteLine("Accepted connection from {0}:{1}.", remoteEndPoint.Address, remoteEndPoint.Port);

                    // Log
                    Node.log.log(this.GetType(), "Received new incoming connection");
                    // Set TcpNoDelay
                    incoming.NoDelay = true;

                    // Now we want to receive the connecting Node's NodeID
                    NetworkStream networkStream = new NetworkStream(incoming);

                    // Receive remote NodeID and parse
                    String otherID  = new BinaryReader(networkStream).ReadString();
                    NodeID remoteID = NodeID.parse(otherID);

                    // First check we have a tcpip Node connection
                    if (remoteID.getNodeAddress() is TCPIPNodeAddress)
                    {
                        // Create an output stream from the Socket
                        BinaryWriter outStream = new BinaryWriter(networkStream);

                        // Now Log that we have received a connection
                        Node.log.log(this.GetType(), "Received connection from: " + remoteID.toString());

                        // Check if already connected
                        if (requestLink(remoteID) == null)
                        {
                            // No existing connection to incoming Node exists. Keep connection

                            // Write OK to the connecting Node
                            outStream.Write("OK");
                            outStream.Flush();

                            // Send out our NodeID
                            outStream.Write(Node.getInstance().getNodeID().toString());
                            outStream.Flush();

                            // Create Link, register, and start.
                            TCPIPLink link = new TCPIPLink(incoming, remoteID);
                            registerLink(link);
                            new ProcessManager(link).start();
                        }
                        else
                        {
                            // We already have a connection to the incoming Node

                            // Log failed connection
                            Node.log.log(this.GetType(), "Connection to " + remoteID
                                         + " already exists.  Informing remote Node.");

                            // Write EXISTS to the remote Node
                            outStream.Write("EXISTS");
                            outStream.Flush();

                            // Send out NodeID. We do this so the opposite Node can find its own connection
                            outStream.Write(Node.getInstance().getNodeID().toString());
                            outStream.Flush();

                            // Close socket
                            incoming.Close();
                        }
                    }

                    // Address is not a TCPIP address. Close socket. This will cause an exception on the opposite Node
                    else
                    {
                        incoming.Close();
                    }
                }
            }
            catch (IOException ioe)
            {
                // We can't really recover from this. This may happen if the network connection was lost.
                // Log and fail
                Node.err.log(this.GetType(), "TCPIPLinkServer failed.  " + ioe.Message);
            }
        }