Пример #1
0
        static void Main(string[] args)
        {
            //application state trackers
            bool shutdown = false;
            bool serverstarted = false;
            bool clientconnected = false;

            os_util = new OSUtil();

            // This is a loop to get commands from the user and execute them
            while (!shutdown)
            {
                string userinput = Console.ReadLine();

                if (!string.IsNullOrEmpty(userinput))
                {
                    switch(os_util.ParseCommand(userinput))
                    {
                        case OSUtil.os_cmd.OS_EXIT:
                            {
                                if (serverstarted)
                                {
                                    os_server.Stop();
                                }
                                shutdown = true;
                                break;
                            }
                        case OSUtil.os_cmd.OS_STARTSERVER:
                            {
                                if (!serverstarted)
                                {
                                    os_server = new OSServer();
                                    bool started = os_server.Start(userinput);
                                    if (!started)
                                    {
                                        Console.WriteLine("Failed to Start Server.");
                                        Console.WriteLine(os_server.GetLastError());
                                    }
                                    else
                                    {
                                        Console.WriteLine("Server started successfully.");
                                        serverstarted = true;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Server is already running.");
                                }
                                break;
                            }
                        case OSUtil.os_cmd.OS_CONNECT:
                            {
                                if (!clientconnected)
                                {
                                    os_client = new OSClient();
                                    bool connected = os_client.Connect(userinput);
                                    if (!connected)
                                    {
                                        Console.WriteLine("Failed to connect Client.");
                                        Console.WriteLine(os_client.GetLastError());
                                    }
                                    else
                                    {
                                        Console.WriteLine("Client might be connected.  It's hard to say.");
                                        clientconnected = true;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Client is already connected");
                                }
                                break;
                            }
                        case OSUtil.os_cmd.OS_DISCONNECT:
                            if (clientconnected)
                            {
                                os_client.DisConnect();
                                clientconnected = false;
                                Console.WriteLine("Client dis-connected from server successfully.");
                            }
                            break;
                        case OSUtil.os_cmd.OS_SEND:
                            {
                                if (clientconnected)
                                {
                                    os_client.Send(userinput);
                                    Console.WriteLine("Message sent from client...");
                                }
                                else
                                {
                                    Console.WriteLine("Send Failed with message:");
                                    Console.WriteLine(os_client.GetLastError());
                                }
                                break;
                            }
                        case OSUtil.os_cmd.OS_HELP:
                            {
                                Console.WriteLine("Available Commands:");
                                Console.WriteLine("startserver <port> = Start the OS Server (Limit 1 per box)");
                                Console.WriteLine("connect <server> <port> = Connect the client to the OS Server");
                                Console.WriteLine("disconnect = Disconnect from the OS Server");
                                Console.WriteLine("send <message> = Send a message to the OS Server");
                                Console.WriteLine("exit = Stop the server and quit the program");
                                break;
                            }
                    }
                }
            }
        }
Пример #2
0
        // static OSClient os_client;


        static void Main(string[] args)
        {
            //application state trackers
            bool shutdown      = false;
            bool serverstarted = false;

            os_util = new OSUtil();

            //Intialize your settings and settings file
            Settings intSettings = new Settings();

            String path    = Directory.GetCurrentDirectory();
            String setFile = @"\settings.xml";
            String ComPath = path + setFile;

            //Check for config file and if it doesn't exist then create it

            if (File.Exists(ComPath))
            {
                Settings intSet = intSettings.Deserialize(ComPath);
                intSettings = intSet;
            }
            else
            {
                //Might want to end here or do something with no config
                intSettings.Serialize(ComPath, intSettings);
                // to shut down just call os_server.Stop();
            }
            if (intSettings.Type.ToString().ToUpper().Trim() == "SERVER")
            {
                os_server = new OSServer(intSettings);

                bool started = os_server.Start(intSettings);
                if (!started)
                {
                    Console.WriteLine("Failed to Start Server.");
                    Console.WriteLine(os_server.GetLastError());
                }
                else
                {
                    Console.WriteLine(string.Format("Server started successfully.\nRunning on Port:{0} and IP:{1}", intSettings.LocalPort, intSettings.LocalIPAddress));
                    serverstarted = true;
                }
            }

            if (intSettings.Type.ToString().ToUpper().Trim() == "CLIENT")
            {
                OSClient os_client = new OSClient(intSettings);

                bool connected = os_client.Connect(intSettings.RemoteIPAddress, Convert.ToInt32(intSettings.RemotePort));
                if (!connected)
                {
                    Console.WriteLine("Failed to Start Client.");
                    Console.WriteLine(os_client.GetLastError());
                }
                else
                {
                    Console.WriteLine(string.Format("Client started successfully.\nRunning on Port:{0} and IP:{1}", intSettings.RemoteIPAddress, intSettings.RemotePort));
                    connected = true;

                    // Watch only for changes to *.txt  or hl7 files this means multiple watchers.
                    String[] filters = { "*.txt", "*.hl7" };
                    List <FileSystemWatcher> watchers = new List <FileSystemWatcher>();
                    //     MyWatcher.Path = intSettings.OutFolderPath;

                    foreach (string f in filters)
                    {
                        FileSystemWatcher w = new FileSystemWatcher();
                        w.Filter = f;
                        w.Path   = intSettings.OutFolderPath;
                        w.IncludeSubdirectories = false;
                        // Enable the component to begin watching for changes.
                        w.EnableRaisingEvents = true;

                        w.Changed += new System.IO.FileSystemEventHandler(os_client.myFileWatcher_ChangeDetecter);
                        w.Created += new System.IO.FileSystemEventHandler(os_client.myFileWatcher_ChangeDetecter);
                        watchers.Add(w);
                    }
                }
            }


            while (!shutdown)
            {
                string userinput = Console.ReadLine();

                if (!string.IsNullOrEmpty(userinput))
                {
                    switch (os_util.ParseCommand(userinput))
                    {
                    case OSUtil.os_cmd.OS_EXIT:
                    {
                        if (serverstarted)
                        {
                            os_server.Stop();
                        }
                        shutdown = true;
                        break;
                    }

                    case OSUtil.os_cmd.OS_HELP:
                    {
                        Console.WriteLine("Available Commands:");
                        Console.WriteLine("exit = Stop the server and quit the program");
                        break;
                    }
                    }
                }
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            //application state trackers
            bool shutdown        = false;
            bool serverstarted   = false;
            bool clientconnected = false;

            os_util = new OSUtil();

            // This is a loop to get commands from the user and execute them
            while (!shutdown)
            {
                string userinput = Console.ReadLine();

                if (!string.IsNullOrEmpty(userinput))
                {
                    switch (os_util.ParseCommand(userinput))
                    {
                    case OSUtil.os_cmd.OS_EXIT:
                    {
                        if (serverstarted)
                        {
                            os_server.Stop();
                        }
                        shutdown = true;
                        break;
                    }

                    case OSUtil.os_cmd.OS_STARTSERVER:
                    {
                        if (!serverstarted)
                        {
                            os_server = new OSServer();
                            bool started = os_server.Start(userinput);
                            if (!started)
                            {
                                Console.WriteLine("Failed to Start Server.");
                                Console.WriteLine(os_server.GetLastError());
                            }
                            else
                            {
                                Console.WriteLine("Server started successfully.");
                                serverstarted = true;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Server is already running.");
                        }
                        break;
                    }

                    case OSUtil.os_cmd.OS_CONNECT:
                    {
                        if (!clientconnected)
                        {
                            os_client = new OSClient();
                            bool connected = os_client.Connect(userinput);
                            if (!connected)
                            {
                                Console.WriteLine("Failed to connect Client.");
                                Console.WriteLine(os_client.GetLastError());
                            }
                            else
                            {
                                Console.WriteLine("Client might be connected.  It's hard to say.");
                                clientconnected = true;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Client is already connected");
                        }
                        break;
                    }

                    case OSUtil.os_cmd.OS_DISCONNECT:
                        if (clientconnected)
                        {
                            os_client.DisConnect();
                            clientconnected = false;
                            Console.WriteLine("Client dis-connected from server successfully.");
                        }
                        break;

                    case OSUtil.os_cmd.OS_SEND:
                    {
                        if (clientconnected)
                        {
                            os_client.Send(userinput);
                            Console.WriteLine("Message sent from client...");
                        }
                        else
                        {
                            Console.WriteLine("Send Failed with message:");
                            Console.WriteLine(os_client.GetLastError());
                        }
                        break;
                    }

                    case OSUtil.os_cmd.OS_HELP:
                    {
                        Console.WriteLine("Available Commands:");
                        Console.WriteLine("startserver <port> = Start the OS Server (Limit 1 per box)");
                        Console.WriteLine("connect <server> <port> = Connect the client to the OS Server");
                        Console.WriteLine("disconnect = Disconnect from the OS Server");
                        Console.WriteLine("send <message> = Send a message to the OS Server");
                        Console.WriteLine("exit = Stop the server and quit the program");
                        break;
                    }
                    }
                }
            }
        }