public bool DeregisterComputationalNode(Node node)
        {
            int i = 0;
            foreach (Node n in ComputationalNodes) {
                if (n.ID == node.ID) {
                    Clients.RemoveAt(i);
                    return true;
                }

                i++;
            }
            return false;
        }
        public bool DeregisterTaskManagers(Node node)
        {
            int i = 0;
            foreach (Node n in TaskManagers) {
                if (n.ID == node.ID) {
                    Clients.RemoveAt(i);
                    return true;
                }

                i++;
            }
            return false;
        }
        public static void Receive(Socket client, Node node)
        {
            try {
                // Create the state object.
                StateObject state = new StateObject();
                state.workSocket = client;
                state.node = node;

                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            } catch (Exception e) {
                node.FailedToSendToServer(null);
                Console.WriteLine("Receive Exception: " + e.Message + "\nAttempting to reconnect");
            }
        }
 public void RegisterTaskManager(Node node)
 {
     this.TaskManagers.Add(node);
 }
 public void RegisterComputationalNode(Node node)
 {
     this.ComputationalNodes.Add(node);
 }
 public void RegisterClient(Node node)
 {
     this.Clients.Add(node);
 }
        public void RegisterBackupServer(Node node)
        {
            foreach (Node n in this.BackupServers) { //do not duplicate same backup
                if (n.IP.Equals(node.IP) && n.Port == node.Port)
                    return;
            }

            this.BackupServers.Enqueue(node);
        }
        public static void StartClient(Int32 port, IPAddress ipAddress, String message, Node node)
        {
            // Connect to a remote device.
            Socket client = null;
            try {
                // Establish the remote endpoint for the socket.
                // The name of the
                // remote device is "host.contoso.com".
                //IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
                //IPAddress ipAddress = ipHostInfo.AddressList[0];
                //IPAddress ipAddress = getIPAddressOfTheLocalMachine();
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

                // Create a TCP/IP socket.
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                StateObject state = new StateObject();
                state.workSocket = client;
                state.node = node;
                // Connect to the remote endpoint.
                client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), state);
                connectDone.WaitOne();

                // Send test data to the remote device.
                Send(client, message, node); //wyjebane <EOF>
                sendDone.WaitOne();

                // Receive the response from the remote device.
                Receive(client, node);
                receiveDone.WaitOne();

                // Write the response to the console.
                //Console.WriteLine("Response received : {0}", response);

                Console.ReadLine();

                // Release the socket.
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            } catch (SocketException e) {
                Console.WriteLine("StartClient Exception: " + e.ToString() + "\nAttempting to reconnect");
                if (client != null) {
                    //Console.WriteLine("Weszłem");
                    //client.Shutdown(SocketShutdown.Both);
                    //client.Close();
                }
                node.FailedToSendToServer(message);
            } catch (Exception e) {
                Console.WriteLine("StartClient General Exception: " + e.ToString()+"\n");
            }
        }
        public static void Send(Socket client, String data, Node node)
        {
            // Convert the string data to byte data using ASCII encoding.
            byte[] byteData = Encoding.ASCII.GetBytes(data);

            StateObject state = new StateObject();
            state.node = node;
            state.workSocket = client;
            // Begin sending the data to the remote device.
            client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), state);
        }
 bool ensureNode(Node node)
 {
     if (node == null)
         return false;
     return true;
     //Console.WriteLine("Did not find node with id: "+ status.Id + "\tSending NoOp");
 }
 public void SendMessage(Int32 port, IPAddress server, String message, Node node)
 {
     AsynchronousClient.StartClient(port, server, message, node);
 }