コード例 #1
0
        public static void Add(int ClientNumber, string MachineName, string fullIP)
        {
            string IP   = fullIP.Split(':')[0];
            string Port = "00000";

            if (fullIP.Split(':').Length >= 2)
            {
                Port = fullIP.Split(':')[1];
            }

            if (HasClient(ClientNumber))
            {
                GetClient(MachineName).fullIP = fullIP;
                GetClient(MachineName).IP     = IP;
                GetClient(MachineName).Port   = Port;
            }
            else
            {
                new ClientViewObject(ClientNumber, MachineName, fullIP, IP, Port);

                reAssignRowAndColumn();

                if (NetworkManager.isServer)
                {
                    NetworkManager.server.TCPBroadcastMessage("addAllClientInfo", ClientViewObject.allClientToXML().ToString());
                }
            }
        }
コード例 #2
0
 public static void ClientDisconnected(object sender, ClientDisconnectedEventArgs args)
 {
     window.Dispatcher.Invoke(() =>
     {
         ClientViewObject.Delete(args.IpPort);
     });
 }
コード例 #3
0
        private static async void StartServer(object sender, RoutedEventArgs e)
        {
            if (System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners().Any(p => p.Port == 15000))
            {
                if (!simultaneousErrorDialog.IsVisible)
                {
                    await window.ShowMetroDialogAsync(simultaneousErrorDialog);
                }
                simultaneousErrorDialog.MessageText.Content = "Error when starting server- UDP port already occupied. Check whether there is another instance running";
                Console.Out.WriteLine("[NetworkManager] error when starting server- UDP port already occupied. Check whether there is another instance running");
                return;
            }

            server = new Server();

            connected    = true;
            clientNumber = 0;
            clientCount  = 0;

            //client view object for displaying in the network page
            ClientViewObject.Add(clientNumber, NetworkUtil.GetMachineName(), NetworkUtil.GetLocalIPAddress());

            //TODO: add -= when server shut down?
            server.MessageReceivedFromClientEvent += MessageReceivedFromClient;
            server.ClientConnectedEvent           += ClientConnected;
            server.ClientDisconnectedEvent        += ClientDisconnected;


            //load all patient in registered patient after starting server, the client will load the patient via requesting
            PatientManager.LoadAllPatientFromSchedular();
            PatientManager.LoadAllPatient();

            //load all the injection after starting server, the client will load the injection via contacting with server
            InjectionsManager.loadAllInjections();

            if (twoChoiceDialog.IsVisible)
            {
                await window.HideMetroDialogAsync(twoChoiceDialog);
            }
            await window.ShowMessageAsync("No server found. \nStarted as server instead!", "Server Name: " + NetworkUtil.GetMachineName() + "\nServer IP:" + NetworkUtil.GetLocalIPAddress());
        }
コード例 #4
0
        public static void Delete(string fullIP)
        {
            foreach (ClientViewObject clientViewObject in clientViewObjects)
            {
                if (clientViewObject.fullIP == fullIP)
                {
                    clientViewObjects.Remove(clientViewObject);

                    reAssignRowAndColumn();

                    if (NetworkManager.isServer)
                    {
                        NetworkManager.server.TCPBroadcastMessage("addAllClientInfo", ClientViewObject.allClientToXML().ToString());
                    }

                    return;
                }
            }
            Console.Error.WriteLine("[ClientViewObject] Could not found clientviewobject with IP: {0}", fullIP);
        }
コード例 #5
0
        /// <summary>
        /// Handle message if client recieved anything from server
        ///
        /// The message contain two part,
        /// first part is a identifier (string) to distinguish what type of message is it
        /// second part is the message content
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void MessageReceivedFromServer(object sender, MessageReceivedFromServerEventArgs args)
        {
            Console.WriteLine("[NetworkManager-Client] Recieved message from server");
            string[] messages = Encoding.UTF8.GetString(args.Data).Split(new char[] { '_' }, 2);
            try
            {
                Console.Out.WriteLine("[NetworkManager-Client] Recieved message type: " + messages[0]);
                Console.Out.WriteLine("[NetworkManager-Client] The message is: " + messages[1]);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }

            switch (messages[0])
            {
            case "autoRestart":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving Auto Restart Request from server, proceed to restart client");

                //disable connection to prevent reconnection to server before restarting
                connectionEnabled = false;



                window.Dispatcher.Invoke(() =>
                {
                    Console.Out.WriteLine("[NetworkManager-Client] Start timer of 15 seconds");
                    DispatcherTimer timer = new DispatcherTimer();
                    timer.Interval        = TimeSpan.FromMinutes(1);
                    timer.Start();
                    timer.Tick += new EventHandler(delegate(object s, EventArgs a)
                    {
                        //only restart if it is a different date
                        if (WindowAutoRestart.isSameDate())
                        {
                            Console.Out.WriteLine("[NetworkManager-Client] It is still yesterday. Wait until auto restart.");
                        }
                        else
                        {
                            Console.Out.WriteLine("[NetworkManager-Client] AutoRestarting...");
                            WindowAutoRestart.AutoRestart();
                            timer.Stop();
                        }
                    });
                });
                break;

            case "setClientNumber":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving Set ClientNumber Request from server, proceed to set self ClientNumber");
                window.Dispatcher.Invoke(() =>
                {
                    clientNumber = int.Parse(messages[1]);
                    Console.Out.WriteLine("[NetworkManager-Client] My client number is:" + clientNumber);
                });
                break;

            case "addAllClientInfo":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving Add All ClientInfo Request from server, proceed to add client view objects for network page");
                window.Dispatcher.Invoke(() =>
                {
                    ClientViewObject.XMLtoClient(XElement.Parse(messages[1]));
                });
                break;

            case "modInjection":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving Mod Injection Request from server, proceed to modify injection");
                window.Dispatcher.Invoke(() =>
                {
                    InjectionsManager.modInjection(XElement.Parse(messages[1]));
                });
                break;

            case "changePatientStatus":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving change PatientStatus Request from server, proceed to change PatientStatus");
                window.Dispatcher.Invoke(() =>
                {
                    //try to splite the message back to (accessionNumber, patientStatus)
                    if (messages[1].Split('^').Length == 2)
                    {
                        InjectionsManager.changePatientStatus(messages[1].Split('^')[0], messages[1].Split('^')[1]);
                    }
                    else
                    {
                        Console.Out.WriteLine("[NetworkManager-Client_changePatientStatus()] Corrupted changePatientStatus message, require 2 input. Message: " + messages[1]);
                    }
                });
                break;

            //////////////////////////////////////////////////
            ////////    Obsolete    //////////////////////////
            case "dischargeInjection":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving Discharge Injection Request from server, proceed to discharge injection");
                window.Dispatcher.Invoke(() =>
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    InjectionsManager.dischargeInjection(messages[1]);
#pragma warning restore CS0618 // Type or member is obsolete
                });
                break;
            //////////////////////////////////////////////////

            case "removeInjection":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving remove Injection Request from server, proceed to remove injection");
                window.Dispatcher.Invoke(() =>
                {
                    InjectionsManager.removeInjection(messages[1]);
                });
                break;

            case "addPatient":
                Console.Out.WriteLine("[NetworkManager-Client] Receiving Add Patient Request from server, proceed to add Patient");
                window.Dispatcher.Invoke(() =>
                {
                    PatientManager.ModPatient(new Patient(XElement.Parse(messages[1])));
                });
                break;

            default:
                Console.Error.WriteLine("[NetworkManager-Client] Unhandled message type: " + messages[0]);
                break;
            }
        }
コード例 #6
0
        /// <summary>
        /// Handle message if client send anything to the server
        ///
        /// The message contain two part,
        /// first part is a identifier (string) to distinguish what type of message is it
        /// second part is the message content
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void MessageReceivedFromClient(object sender, MessageReceivedFromClientEventArgs args)
        {
            string[] messages = Encoding.UTF8.GetString(args.Data).Split(new char[] { '_' }, 2);
            try
            {
                Console.Out.WriteLine("[NetworkManager-Server] Recieved message type: " + messages[0]);
                Console.Out.WriteLine("[NetworkManager-Server] The message is: " + messages[1]);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }

            switch (messages[0])
            {
            case "ConnectionSucessful":
                Console.Out.WriteLine("[NetworkManager-Server] New connection established with client IP: {0}, Name: {1}", args.IpPort, messages[1]);
                window.Dispatcher.Invoke(() =>
                {
                    //Send the clientNumber to the client
                    clientCount += 1;
                    Console.Out.WriteLine("[NetworkManager-Server] Setting client with IPPort: {0} as client number {1}", args.IpPort, clientCount);
                    server.TCPSendMessage(args.IpPort, "setClientNumber", clientCount.ToString());

                    //Add the client view object for networking page
                    ClientViewObject.Add(clientCount, messages[1], args.IpPort);
                });
                break;

            case "modInjection":
                Console.Out.WriteLine("[NetworkManager-Server] Receiving Mod Injection Request from client, proceed to modify injection");
                window.Dispatcher.Invoke(() =>
                {
                    server.TCPBroadcastMessage("modInjection", messages[1]);
                    InjectionsManager.modInjection(XElement.Parse(messages[1]));
                });
                break;

            case "requestInitialInjection":
                Console.Out.WriteLine("[NetworkManager-Server] Receiving Initial Injection Request from client, proceed to send all injection to client with ip: {0}", args.IpPort);
                foreach (Injection injection in InjectionsManager.injections)
                {
                    server.TCPSendMessage(args.IpPort, "modInjection", injection.toXML().ToString());
                }
                break;

            case "requestInitialPatient":
                Console.Out.WriteLine("[NetworkManager-Server] Receiving Initial Patient Request from client, proceed to send all patient to client with ip: {0}", args.IpPort);
                foreach (Patient patient in PatientManager.Patients)
                {
                    server.TCPSendMessage(args.IpPort, "addPatient", patient.toXML().ToString());
                }
                break;

            //////////////////////////////////////////////////
            ////////    Obsolete    //////////////////////////
            case "dischargeInjection":
                Console.Out.WriteLine("[NetworkManager-Server] Receiving discharge Injection Request from client, proceed to discharge injection");
                window.Dispatcher.Invoke(() =>
                {
                    server.TCPBroadcastMessage("dischargeInjection", messages[1]);
#pragma warning disable CS0618 // Type or member is obsolete
                    InjectionsManager.dischargeInjection(messages[1]);
#pragma warning restore CS0618 // Type or member is obsolete
                });
                break;
            //////////////////////////////////////////////////

            case "changePatientStatus":
                Console.Out.WriteLine("[NetworkManager-Server] Receiving changePatientStatus Request from client, proceed to change PatientStatus");
                window.Dispatcher.Invoke(() =>
                {
                    server.TCPBroadcastMessage("changePatientStatus", messages[1]);
                    //try to splite the message back to (accessionNumber, patientStatus)
                    if (messages[1].Split('^').Length == 2)
                    {
                        InjectionsManager.changePatientStatus(messages[1].Split('^')[0], messages[1].Split('^')[1]);
                    }
                    else
                    {
                        Console.Out.WriteLine("[NetworkManager-Server_changePatientStatus()] Corrupted changePatientStatus message, require 2 input. Message: " + messages[1]);
                    }
                });

                break;

            case "removeInjection":
                Console.Out.WriteLine("[NetworkManager-Server] Receiving remove Injection Request from client, proceed to remove injection");
                window.Dispatcher.Invoke(() =>
                {
                    server.TCPBroadcastMessage("removeInjection", messages[1]);
                    InjectionsManager.removeInjection(messages[1]);
                });
                break;

            default:
                Console.Error.WriteLine("[NetworkManager-Server] Unhandled message type: " + messages[0]);
                break;
            }

            try
            {
                window.Dispatcher.Invoke(() =>
                {
                    ClientViewObject.GetClientViaIP(args.IpPort).UpdateMessage(messages[0], messages[1]);
                });
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
        }