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); } }