예제 #1
0
        public static void Main(string[] args)
        {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
             TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

             bool portAvailable;
             bool portFound = false;
             Random rdm = new Random();

             int port = rdm.Next(2001, 2100);

             while (!portFound)
             {

            portAvailable = true;
            foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
            {
               if (tcpi.LocalEndPoint.Port == port)
               {
                  portAvailable = false;
                  break;
               }
            }

            if (portAvailable)
            {
               portFound = true;
            }
            else
            {
               port = rdm.Next(2001, 2100);
            }
             }

             TcpChannel serverChannel = new TcpChannel(port);
             ChannelServices.RegisterChannel(serverChannel, false);

             string serverURL = "tcp://localhost:" + port + "/" + Constants.SERVER_NAME;
             System.Console.Title = serverURL;
             Server server = new Server(serverURL);
             RemotingServices.Marshal(server, Constants.SERVER_NAME, typeof(iServer));
             server.registerInMaster();

             if (server.getStatus().Equals(Constants.STATUS_PRIMARY))
             {
            server.launchNewServer();
             }

             new Thread(delegate()
             {
            while (true)
            {
               Thread t = new Thread(server.sendData);
               t.Start();

               if (!t.Join(TimeSpan.FromSeconds(Constants.TIMEOUT)))
               {
                  t.Abort();
                  if (!server.getStatus().Equals(Constants.STATUS_FAILED))
                  {
                     server.onSecundaryServerFailure();
                  }
               }
               Thread.Sleep(TimeSpan.FromSeconds(Constants.PING_PERIOD));
            }
             }).Start();

             System.Console.WriteLine("enter commands to control server:");
             System.Console.WriteLine("    f to fail");
             System.Console.WriteLine("    r to recover");
             System.Console.WriteLine("    s to freeze");
             while (true)
             {
            string command = System.Console.ReadLine();
            if (command.Equals("f"))
            {
               server.fail();
            }
            else if (command.Equals("r"))
            {
               server.recover();
            }
            else if (command.Equals("s"))
            {
               server.freeze();
            }
             }
        }