Exemple #1
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());
            }
        }
        /**
         * Creates LinkServer by wrapping round an existing ServerSocket. Used internally by JCSP
         *
         * @param serverSocket
         *            The ServerSocket to create the LinkServer with
         */
        //internal TCPIPLinkServer(ServerSocket serverSocket)
        internal TCPIPLinkServer(Socket serverSocket)
        {
            // We need to set the NodeAddress. Create from ServerSocket address and port
            //TODO double check if thee code below provides the proper values - Karol Pasierb
            //this.listeningAddress = new TCPIPNodeAddress(serverSocket.getInetAddress().getHostAddress(), serverSocket.getLocalPort());
            IPEndPoint a = (IPEndPoint)serverSocket.LocalEndPoint;

            this.listeningAddress = new TCPIPNodeAddress(a.Address.ToString(), a.Port);
            this.serv             = serverSocket;
        }
Exemple #3
0
        /**
         * Creates new TCPIPLink from a Socket. This is used internally by JCSP
         *
         * @param socket
         *            The socket to create the TCPIPLink with
         * @param nodeID
         *            The NodeID of the remote Node
         * @//throws JCSPNetworkException
         *             Thrown if there is a problem during the connection
         */
        internal TCPIPLink(Socket socket, NodeID nodeID)
        //throws JCSPNetworkException
        {
            try
            {
                // Set the socket property
                this.socket = socket;
                // Set TcpNoDelay
                //socket.setTcpNoDelay(!TCPIPLink.NAGLE);
                socket.NoDelay = !TCPIPLink.NAGLE;
                // Set the input and output streams for the Link
                //this.rxStream = new BinaryReader(new BufferedInputStream(socket.getInputStream(), TCPIPLink.BUFFER_SIZE));
                //this.txStream = new BinaryWriter(new BufferedOutputStream(socket.getOutputStream(), TCPIPLink.BUFFER_SIZE));


                //this.rxStream = new BinaryReader(new BufferedInputStream(this.socket.getInputStream(), TCPIPLink.BUFFER_SIZE));
                this.rxStream = new BinaryReader(new NetworkStream(this.socket));
                //this.txStream = new BinaryWriter(new BufferedOutputStream(this.socket.getOutputStream(), TCPIPLink.BUFFER_SIZE));
                this.txStream = new BinaryWriter(new NetworkStream(this.socket));


                // Set the NodeID
                this.remoteID = nodeID;
                // Set the remote address
                this.remoteAddress = (TCPIPNodeAddress)this.remoteID.getNodeAddress();
                // Set connected to true
                this.connected = true;
                // Log Link creation and Link connection
                Node.log.log(this.GetType(), "Link created to " + nodeID.toString());
                Node.log.log(this.GetType(), "Link to " + nodeID.toString() + " connected");
            }
            catch (IOException ioe)
            {
                // Something went wrong during the creation. Log and throw exception
                Node.err.log(this.GetType(), "Failed to create Link to " + nodeID.toString());
                throw new JCSPNetworkException("Failed to create TCPIPLink to: " +
                                               nodeID.getNodeAddress().getAddress());
            }
        }
        /**
         * Initialises the Node, connecting to the CNS / BNS
         *
         * @param node
         *            The Node to initialise
         * @return A new NodeAddress which the Node is registered at
         * @//throws JCSPNetworkException
         *             Thrown if something goes wrong during the Node initialisation process
         */
        internal override NodeAddress initNode(Node node)
        //throws JCSPNetworkException
        {
            // First install TCPIPProtocolID
            NodeAddress.installProtocol("tcpip", TCPIPProtocolID.getInstance());
            try
            {
                // Get the local IP addresses
                //InetAddress[] local = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
                IPAddress[] localIPAddresses = GetLocalIPAddress.GetAllAddresses();
                //InetAddress toUse = InetAddress.getLocalHost();
                IPAddress ipAddresstoUse = GetLocalIPAddress.GetOnlyLocalIPAddress();


                // We basically have four types of addresses to worry about. Loopback (127), link local (169),
                // local (192) and (possibly) global. Grade each 1, 2, 3, 4 and use highest scoring address. In all
                // cases use first address of that score.
                int current = 0;

                // Loop until we have checked all the addresses
                for (int i = 0; i < localIPAddresses.Length; i++)
                {
                    // Ensure we have an IPv4 address
                    //if (localIPAddresses[i] is Inet4Address)
                    if (localIPAddresses[i] is IPAddress)
                    {
                        // Get the first byte of the address
                        //byte first = localIPAddresses[i].getAddress()[0];
                        byte first = localIPAddresses[i].GetAddressBytes()[0];


                        // Now check the value
                        if (first == (byte)127 && current < 1)
                        {
                            // We have a Loopback address
                            current = 1;
                            // Set the address to use
                            ipAddresstoUse = localIPAddresses[i];
                        }
                        else if (first == (byte)169 && current < 2)
                        {
                            // We have a link local address
                            current = 2;
                            // Set the address to use
                            ipAddresstoUse = localIPAddresses[i];
                        }
                        else if (first == (byte)192 && current < 3)
                        {
                            // We have a local address
                            current = 3;
                            // Set the address to use
                            ipAddresstoUse = localIPAddresses[i];
                        }
                        else
                        {
                            // Assume the address is globally accessible and use by default.
                            ipAddresstoUse = localIPAddresses[i];
                            // Break from the loop
                            break;
                        }
                    }
                }

                // Create a new ServerSocket listening on this address
                //TcpClient serv = new TcpClient(0, 10, ipAddresstoUse);
                //TcpListener serv = new TcpListener(0, 10, toUse);
                Socket serv = new Socket(ipAddresstoUse.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                //ServerSocket serv = new ServerSocket(0, 10, toUse);
                IPEndPoint inetAddress = new IPEndPoint(ipAddresstoUse, 10);

                // Create the local address
                //TCPIPNodeAddress localAddr = new TCPIPNodeAddress(toUse.getHostAddress(), serv.getLocalPort());
                TCPIPNodeAddress localAddr = new TCPIPNodeAddress(ipAddresstoUse.ToString(), inetAddress.Port);

                // Create and start the LinkServer
                TCPIPLinkServer server = new TCPIPLinkServer(serv);
                new ProcessManager(server).start();

                // Return the NodeAddress
                return(localAddr);
            }

            /*catch (UnknownHostException uhe)
             * {
             *  throw new JCSPNetworkException("Failed to start TCPIPLinkServer.  Could not get local IP address.\n"
             + uhe.getMessage());
             + }*/
            catch (IOException ioe)
            {
                throw new JCSPNetworkException("Failed to open new Server Socket.\n" + ioe.Message);
            }
        }
 /**
  * Creates a new TCPIPNodeFactory
  *
  * @param addr
  *            The address of the CNS / BNS
  */
 public TCPIPNodeFactory(TCPIPNodeAddress addr)
 {
     this.cnsAddress = addr;
 }
        /**
         * Creates a new TCPIPLinkServer listening on the given address
         *
         * @param address
         *            The address to listen on for new connections
         * @//throws JCSPNetworkException
         *             Thrown if something goes wrong during the creation of the ServerSocket
         */
        public TCPIPLinkServer(TCPIPNodeAddress address)
        //throws JCSPNetworkException
        {
            try
            {
                // First check if we have an ip address in the string
                if (String.IsNullOrEmpty(address.GetIpAddressAsString()))
                {
                    IPAddress   localIPAddresstoUse = GetLocalIPAddress.GetOnlyLocalIPAddress();
                    IPAddress[] localIPAddresses    = GetLocalIPAddress.GetAllAddresses();
                    address.setIpAddress(GetLocalIPAddress.ConvertIPAddressToString(localIPAddresstoUse));
                    // Get the local IP addresses
                    //InetAddress[] local = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
                    //InetAddress toUse = InetAddress.getLocalHost();


                    //TODO the code below is not updated and not working correctly.
                    // We basically have four types of addresses to worry about. Loopback (127), link local (169),
                    // local (192) and (possibly) global. Grade each 1, 2, 3, 4 and use highest scoring address. In all
                    // cases use first address of that score.
                    int current = 0;

                    // Loop until we have checked all the addresses
                    for (int i = 0; i < localIPAddresses.Length; i++)
                    {
                        // Ensure we have an IPv4 address
                        //if (local[i] is Inet4Address)
                        if (localIPAddresses[i] is IPAddress)
                        {
                            // Get the first byte of the address
                            //byte first = local[i].getAddress()[0];
                            byte first = localIPAddresses[i].GetAddressBytes()[0];

                            // Now check the value
                            if (first == (byte)127 && current < 1)
                            {
                                // We have a Loopback address
                                current = 1;
                                // Set the address to use
                                localIPAddresstoUse = localIPAddresses[i];
                            }
                            else if (first == (byte)169 && current < 2)
                            {
                                // We have a link local address
                                current = 2;
                                // Set the address to use
                                localIPAddresstoUse = localIPAddresses[i];
                            }
                            else if (first == (byte)192 && current < 3)
                            {
                                // We have a local address
                                current = 3;
                                // Set the address to use
                                localIPAddresstoUse = localIPAddresses[i];
                            }
                            else
                            {
                                // Assume the address is globally accessible and use by default.
                                localIPAddresstoUse = localIPAddresses[i];
                                // Break from the loop
                                break;
                            }
                        }
                    }

                    // Now set the IP address of the address
                    //address.setIpAddress(toUse.getHostAddress());
                    //address.setIpAddress(localIPAddresstoUse.ToString());

                    // Set the address part now, but it may change if we have to get a port number
                    //address.setAddress(address.GetIpAddressAsString() + ":" + address.getPort());
                }

                // Now check if the address has a port number
                if (address.getPort() == 0)
                {
                    // No port number supplied. Get one as we create the ServerSocket
                    //InetAddress socketAddress = InetAddress.getByName(address.GetIpAddressAsString());
                    //SocketAddress socketAddress = new SocketAddress(AddressFamily.InterNetwork);
                    IPEndPoint socketEndPoint =
                        new IPEndPoint(IPAddress.Parse(address.GetIpAddressAsString()),
                                       0); //port 0 as in Java's implementation

                    // Create the server socket with a random port
                    //this.serv = new ServerSocket(0, 0, socketAddress);
                    //this.serv = new TcpListener(localIPAddresstoUse.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    this.serv = new Socket(socketEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    this.serv.Bind(socketEndPoint);
                    this.serv.Listen(0);


                    // Assign the port to the address
                    //address.setPort(this.serv.getLocalPort());
                    address.setPort(socketEndPoint.Port);

                    // And set the address
                    address.setAddress(address.GetIpAddressAsString() + ":" + address.getPort());

                    // Set the listening address
                    this.listeningAddress = address;
                }
                else
                {
                    // Create an IP address from the NodeAddress
                    IPEndPoint inetAddress = new IPEndPoint(IPAddress.Parse(address.GetIpAddressAsString()),
                                                            address.getPort());

                    // Now create the ServerSocket
                    this.serv = new Socket(inetAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    //Bind server to the ip address - Karol Pasierb
                    this.serv.Bind(inetAddress);
                    this.serv.Listen(10); //backlog 10 for the queue  - Karol Pasierb

                    // Set listeningAddress
                    this.listeningAddress = address;
                }
            }
            catch (IOException ioe)
            {
                throw new JCSPNetworkException("Failed to create TCPIPLinkServer on: " + address.getAddress());
            }
        }
Exemple #7
0
        /**
         * Creates a new TCPIPLink
         *
         * @param address
         *            The address of the remote Node to connect to
         * @//throws JCSPNetworkException
         *             Thrown if something goes wrong during the creation process
         */
        public TCPIPLink(TCPIPNodeAddress address)
        //throws JCSPNetworkException
        {
            try
            {
                // First check if we have an ip address in the string. If not, we assume that this is to be connected
                // to the local machine but to a different JVM
                if (String.IsNullOrEmpty(address.GetIpAddressAsString()))
                {
                    IPAddress[] localIPAddresses = GetLocalIPAddress.GetAllAddresses();
                    IPAddress   ipAddresstoUse   = GetLocalIPAddress.GetOnlyLocalIPAddress();
                    address.setIpAddress(GetLocalIPAddress.ConvertIPAddressToString(ipAddresstoUse));

                    //TODO the code below is not updated and not working correctly.
                    // We basically have four types of addresses to worry about. Loopback (127), link local (169),
                    // local (192) and (possibly) global. Grade each 1, 2, 3, 4 and use highest scoring address. In all
                    // cases use first address of that score.
                    int current = 0;

                    // Loop until we have checked all the addresses
                    for (int i = 0; i < localIPAddresses.Length; i++)
                    {
                        // Ensure we have an IPv4 address
                        if (localIPAddresses[i] is IPAddress)
                        {
                            // Get the first byte of the address
                            //byte first = localIPAddresses[i].getAddress()[0];
                            byte first = localIPAddresses[i].GetAddressBytes()[0];

                            // Now check the value
                            if (first == (byte)127 && current < 1)
                            {
                                // We have a Loopback address
                                current = 1;
                                // Set the address to use
                                ipAddresstoUse = localIPAddresses[i];
                            }
                            else if (first == (byte)169 && current < 2)
                            {
                                // We have a link local address
                                current = 2;
                                // Set the address to use
                                ipAddresstoUse = localIPAddresses[i];
                            }
                            else if (first == (byte)192 && current < 3)
                            {
                                // We have a local address
                                current = 3;
                                // Set the address to use
                                ipAddresstoUse = localIPAddresses[i];
                            }
                            else
                            {
                                // Assume the address is globally accessible and use by default.
                                ipAddresstoUse = localIPAddresses[i];
                                // Break from the loop
                                break;
                            }
                        }
                    }

                    // Now set the IP address of the address
                    //address.setIpAddress(ipAddresstoUse.ToString());

                    // Set the address part.
                    //address.setAddress(address.GetIpAddressAsString() + ":" + address.getPort());
                }

                // Connect the socket to the server socket on the remote Node
                IPAddress remoteAddress = IPAddress.Parse(address.GetIpAddressAsString());
                this.socket = new Socket(remoteAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                EndPoint remoteEndPoint = new IPEndPoint(remoteAddress, address.getPort());

                // Set TcpNoDelay. Off should improve performance for smaller packet sizes, which JCSP should have in general
                this.socket.NoDelay = !TCPIPLink.NAGLE;
                this.socket.Connect(remoteEndPoint);
                // Create the input and output streams for the Link
                this.rxStream = new BinaryReader(new NetworkStream(this.socket));
                this.txStream = new BinaryWriter(new NetworkStream(this.socket));

                // Set the remote address
                this.remoteAddress = address;

                // We are not connected, so set connected to false.
                this.connected = false;

                // Log Node connection
                Node.log.log(this.GetType(), "Link created to " + address.toString());
            }
            catch (IOException ioe)
            {
                // Something went wrong during connection. Log and throw exception
                Node.err.log(this.GetType(), "Failed to create Link to " + address.toString());
                throw new JCSPNetworkException("Failed to create TCPIPLink to: " + address.getAddress());
            }
        }
        /**
         * @param args
         * @//throws Exception
         */
        public static void main(String[] args)
        //throws Exception
        {
            Node.getInstance().setLog(new StreamWriter(Console.OpenStandardOutput()));
            Node.getInstance().setErr(new StreamWriter(Console.OpenStandardError()));

            // Get the local IP addresses
            IPAddress[] localIPAddresses = GetLocalIPAddress.GetAllAddresses();
            IPAddress   ipAddresstoUse   = GetLocalIPAddress.GetOnlyLocalIPAddress();


            // We basically have four types of addresses to worry about. Loopback (127), link local (169),
            // local (192) and (possibly) global. Grade each 1, 2, 3, 4 and use highest scoring address. In all
            // cases use first address of that score.
            int current = 0;

            // Loop until we have checked all the addresses
            for (int i = 0; i < localIPAddresses.Length; i++)
            {
                // Ensure we have an IPv4 address
                if (localIPAddresses[i] is IPAddress)
                {
                    // Get the first byte of the address
                    //byte first = localIPAddresses[i].getAddress()[0];
                    byte first = localIPAddresses[i].GetAddressBytes()[0];
                    // Now check the value
                    if (first == (byte)127 && current < 1)
                    {
                        // We have a Loopback address
                        current = 1;
                        // Set the address to use
                        ipAddresstoUse = localIPAddresses[i];
                    }
                    else if (first == (byte)169 && current < 2)
                    {
                        // We have a link local address
                        current = 2;
                        // Set the address to use
                        ipAddresstoUse = localIPAddresses[i];
                    }
                    else if (first == (byte)192 && current < 3)
                    {
                        // We have a local address
                        current = 3;
                        // Set the address to use
                        ipAddresstoUse = localIPAddresses[i];
                    }
                    else
                    {
                        // Assume the address is globally accessible and use by default.
                        ipAddresstoUse = localIPAddresses[i];
                        // Break from the loop
                        break;
                    }
                }
            }

            // Create a local address object
            TCPIPNodeAddress localAddr = new TCPIPNodeAddress(ipAddresstoUse.ToString(), 7890);

            // Initialise the Node
            Node.getInstance().init(localAddr);
            // Start CNS and BNS
            IamCSProcess[] processes = { CNS.CNS.getInstance(), BNS.BNS.getInstance() };
            new CSPParallel(processes).run();
        }