public CSharpServerImplementation() { myRegisterHandler = new RegisterHandler(); myElectionHelper = new ElectionHelper(); myRequestHandler = new RequestHandler(); myRequestCentralHandler = new RequestHandlerCentralized(); myResourceHandler = new ResourceHandler(); }
/* ========= MAIN METHODS ====== */ public static void Main(string[] args) { IDictionary props = new Hashtable(); props["name"] = "MyHttpChannel"; props["port"] = 1090; HttpChannel channel = new HttpChannel( props, new XmlRpcClientFormatterSinkProvider(), new XmlRpcServerFormatterSinkProvider()); ChannelServices.RegisterChannel(channel, false); RemotingConfiguration.RegisterWellKnownServiceType( typeof(CSharpServerImplementation), "xml-rpc-example/xmlrpc", WellKnownObjectMode.Singleton); #region Section 1 // ===========================Section 1: Joining the network================================ try { electionHelper = new ElectionHelper(); //assign my IP myIp = Dns.GetHostEntry(Dns.GetHostName()); myIpAddress = NetworkHelper.GetMyIpAddress(); //join network String myNeighbourIp = ""; Console.WriteLine("=====Connecting to Network====="); Console.WriteLine("Please input neighbor IP address :"); myNeighbourIp = Console.ReadLine(); Console.WriteLine(""); RegisterHandler.joinNetwork(myIpAddress, myNeighbourIp); if (!machines.ContainsValue(myIpAddress)) { Console.WriteLine("Add myself to hashmap"); myPriority = addMachineToMap(myIpAddress); } else { //get priority myPriority = machines.First(x => x.Value == myIpAddress).Key; } //set myself as master if null if (ipMaster == "localhost" || ipMaster == null || ipMaster == string.Empty) { setMaster(myPriority); } Console.WriteLine(classNameLog + "Master IP now " + ipMaster); Console.WriteLine(classNameLog + "Master Key now {0}", machines.First(x => x.Value == ipMaster).Value); Console.WriteLine(classNameLog + "My IP now " + myIpAddress); Console.WriteLine(classNameLog + "My priority now " + myPriority); Console.WriteLine(classNameLog + "Machines now " + machines.Count()); } catch (Exception ex) { Console.WriteLine("{0}", ex.Message); } // ====================================End of Section 1======================================== #endregion #region Section 2 // =============================Section 2: Begin service=========================================== string input; do { input = Console.ReadLine().ToLower(); try { Console.WriteLine("Enter command, or 'exit' to quit: "); switch (input) { case "ip": Console.WriteLine("This machine ip :" + myIpAddress); break; default: Console.WriteLine("Command " + input + " not recognized"); break; } } catch (Exception e) { Console.WriteLine("{0}", e.Message); } } while (input != "exit"); // ================================End of Section 2============================================= #endregion Console.WriteLine("Shutting down server..."); serverShutDown(); }