/** * 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 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()); } }
/** * 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()); } }
/** * @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(); }