protected override void sendRuptureRequest() { // sendRuptureRequest : this function must be call locally when the terminal want to SignOff, and then must clean all hosts from the system except local host because in the next time the host list must be empty // this function must be call locally when the terminal want to SignOff, and then must clean all hosts from the system except local host because in the next time the host list must be empty Console.WriteLine(""); Console.WriteLine("-------------------------------------------"); Console.WriteLine("| <<< Signing Off This Host >>> |"); Console.WriteLine("-------------------------------------------"); Console.WriteLine("Please wait to unjoin from all hosts on calendar network!"); String thisHostIPv4; String thisHostUrl; int thisHostPort; try { //Generate the ipv4 address of this machine thisHostIPv4 = LocalNet.IpAddress; thisHostUrl = "http://" + thisHostIPv4 + "/"; //Generate the Url of this machine based on its Ip thisHostPort = HostsList.getFirstHostUrl().getPort(); //find out the current machine port bool result = false; //Object[] params = new Object[]{thisHostUrl,thisHostPort}; host.MethodName = "CalendarNetwork.removeMe"; host.Params.Clear(); host.Params.Add(thisHostUrl); host.Params.Add(thisHostPort); HostsList iterator = new HostsList(); HostUrl hostUrl = iterator.nextHostUrl(); while (hostUrl != null) { if (setDestinationHost(hostUrl)) { try { HostsList.removeHost(hostUrl);//Add next host of the list to the hostList of this machine response = host.Send(hostUrlAddress); result = (bool)response.Value; if (result) { Console.WriteLine("The address of current machine has eliminated on the host : [" + hostUrl.getFullUrl() + "]."); } else { Console.WriteLine("Rupturation of the current machine has failed on the host : [" + hostUrl.getFullUrl() + "]"); } } catch (System.Exception e) { Console.WriteLine("Rupturation of the current machine has failed on the host : [" + hostUrl.getFullUrl() + "]"); Console.WriteLine(e.Message); } } hostUrl = iterator.nextHostUrl(); } Console.WriteLine("All the host was removed successfully!"); ServerStatus.signOffServer(); Console.WriteLine("Now this machine will work on offline mode!"); } catch (Exception e) { Console.WriteLine(e.Message); } }
protected override void sendJoinRequest() { Console.WriteLine(""); Console.WriteLine("-------------------------------------------"); Console.WriteLine("| <<< Join Calendar Network procedure >>> |"); Console.WriteLine("-------------------------------------------"); Console.Write("Please enter the IPv4 address of a know host that is a member of the calendar network currently : "); String ipAddress = Reader.nextIPv4(); Console.Write("Please enter the port number of the host that you have entered its IP address [" + ipAddress + "] : "); int port = Reader.nextInt(1025, 65535); try{ HostUrl hostUrl = new HostUrl(port, ipAddress); //Set the destination host based on what the user entered if (setDestinationHost(hostUrl)) { String thisHostIPv4; String thisHostUrl; int thisHostPort; try { thisHostIPv4 = LocalNet.IpAddress; //Generate the ipv4 address of this machine thisHostUrl = "http://" + thisHostIPv4 + "/"; //Generate the Url of this machine based on its Ip thisHostPort = HostsList.getFirstHostUrl().getPort(); //find out the current machine port if (thisHostIPv4.Equals(ipAddress) && thisHostPort == port) { Console.WriteLine("You can not connect to yourself.\nThe IP address and the port number that you have entered is blonged to this machine."); return; } host.MethodName = "CalendarNetwork.joinRequest"; host.Params.Clear(); host.Params.Add(thisHostUrl); host.Params.Add(thisHostPort); ServerStatus.signOnServer(); //if the joining fail it must change to signoff again //String hostsListString = (String) host.execute("CalendarNetwork.joinRequest", params); //joinRequest function is placed in HostList.java file response = host.Send(hostUrlAddress); String hostsListString = (String)response.Value; // if (hostsListString == null) { Console.WriteLine("The joining process was failed on the known host that you have introduced."); ServerStatus.signOffServer(); //because the joining fail it must change to signoff again return; } else { String[] lines = hostsListString.Split("\n".ToCharArray()); if (lines[0].Equals("true")) { HostsList.addHost(hostUrl);//At first Add the known host to the hostList of this machine after a successful connection } else { Console.WriteLine("The joining process was failed on the known host that you have introduced."); ServerStatus.signOffServer(); //because the joining fail it must change to signoff again return; } //sending new request to synchronize the appointments' list this.synchronizeDataBase(hostUrl); Console.WriteLine("The synchronizing stage has finished.\nNow we register this host on other hosts!"); String nextHostUrl, nextHostPort; bool result = false; //host.MethodName = "CalendarNetwork.addMe"; //Added in C# for (int index = 1; index < lines.Length; index++) { nextHostUrl = null; nextHostPort = null; //The response must be parse and other hosts come out from the String and propagate the current machine on all of them //Must parse each address and send requests separately //At first find the url of the next host Regex regex = null; Match matcher = null; regex = new Regex("URL:\\[(.*?)\\]"); matcher = regex.Match(lines[index]); if (matcher.Success) { nextHostUrl = matcher.Groups[1].Value; } //Then find the port number string regex = new Regex("Port:\\[(.*?)\\]"); matcher = regex.Match(lines[index]); if (matcher.Success) { nextHostPort = matcher.Groups[1].Value; } //Now connect to the next host and register if (nextHostUrl != null && nextHostPort != null) { hostUrl = new HostUrl(nextHostUrl, Integer.parseInt(nextHostPort)); //Set the destination host based on what the user entered if (setDestinationHost(hostUrl)) { host.MethodName = "CalendarNetwork.addMe"; host.Params.Clear(); host.Params.Add(thisHostUrl); host.Params.Add(thisHostPort); try { response = host.Send(hostUrlAddress); result = (bool)response.Value; if (result) { HostsList.addHost(hostUrl);//Add next host of the list to the hostList of this machine } else { Console.WriteLine("Registeration of the current machine has failed on the host : [" + hostUrl.getFullUrl() + "]"); } } catch (System.Exception e) { Console.WriteLine("Registeration of the current machine has failed on the host : [" + hostUrl.getFullUrl() + "]"); Console.WriteLine(e.Message); } } } } //End of for } //End of else } catch (Exception e) { Console.WriteLine(e.Message); } } } catch (Exception e) { Console.WriteLine(e.Message); } }