public void add(HostUrl hostUrl, bool isFirstHost) { if (hostsAddresses.Count == 0) { hostsAddresses.Add(hostUrl); me = hostUrl; //it must recognize itself in the list } else { lock (hostsAddresses) { isCoordinator = hostsAddresses.Count == 1 ? true : false; int index = 0; for (index = 0; index < hostsAddresses.Count; index++) { if (hostsAddresses[index].compare(hostUrl) >= 0) { break; } } hostsAddresses.Insert(index, hostUrl); if (isCoordinator && isFirstHost) { startTokenRingsRotate(); } else { initTokenRings(); } } } }
public RequestObject(long id, int logicalClock, HostUrl hostUrl) //for external requests that must push in queue { this.isInternalRequester = false; this.eLCO = new ExtendedLamportClockObject(id, logicalClock); this.hostsAddresses = new List <HostUrl>(); this.hostsAddresses.Add(hostUrl); //the address of external requester }
public bool remove(HostUrl hostUrl) { bool result = hostsAddresses.Remove(hostUrl); if (hostsAddresses.Count <= 1) { stopTokenRingsRotate(); } return(result); }
private MachinIdentification(int _port, String ipv4Address) { if (ipv4 == null) { setIpv4(ipv4Address); if (_port > 1024 & _port < 65535) { port = _port; } hostUrl = new HostUrl(_port, ipv4Address); } }
public void sendToken() { try { int result = -1; Random rnd = new Random(); int rand = rnd.Next(214748300); HostUrl hostUrl = TokenRingQueue.nextHostOnRing(); if (hostUrl != null) { if (setDestinationHost(hostUrl)) { if (tokenRing.getTokenRingName().Equals("AddTokenRing")) { host.MethodName = "AddTokenRing.receiveToken"; host.Params.Clear(); host.Params.Add(rand); } else if (tokenRing.getTokenRingName().Equals("ModifyTokenRing")) { host.MethodName = "ModifyTokenRing.receiveToken"; host.Params.Clear(); host.Params.Add(rand); } int attempts = 1; do { try { response = host.Send(hostUrlAddress); result = (int)response.Value; break; } catch (Exception) { attempts++; } }while(attempts < 4); if (result != rand + 1) { Console.WriteLine("* The token has not been received by the host : [" + hostUrl.getFullUrl() + "]."); } } } } catch (Exception) {} }
//@Override public bool addPemissionAccepted(String replierId, int replierLogicalClock, String replierHostUrl, int replierHostPort, String requesterId, int requesterLogicalClock) { //the clock will manage here too //The first 2 objects used for correcting the clock //The third one used for deleting the host from the tail list ! and not from the queue //The last 2 objects are used for checking true answering //this will call by other clients to send [rep<id,clock> --to--> req<id',clock'>] actually OK message! //when they want to send a OK reply to our previous request they must call this function //this function will remove RequestObjects from the tail list try{ if (!ServerStatus.getServerStatus()) { return(false); } if (currentAddRequest != null) { long requesterIdL = 0; HostUrl hostUrl = null; try { requesterIdL = Convert.ToInt64(requesterId); hostUrl = new HostUrl(replierHostUrl, replierHostPort); } catch (Exception) { return(false); } //correcting Clock ExtendedLamportClockObject ELC = new ExtendedLamportClockObject(requesterIdL, requesterLogicalClock); //for correcting logical clock after receive if (currentAddRequest.getELCO().compare(ELC) == 0) //if the replier send the reply for the current request { lock (this) { currentAddRequest.removeNode(hostUrl); } hostUrl = null; ELC = null; } return(true); } return(false); }catch (Exception) { return(false); } }
/// <summary> /// Downloads remote file from ftp /// </summary> /// <param name="path">Path to file on server (example: "/path/to/file.txt")</param> /// <param name="localPath">Path on local machine, where file should be saved (if not provided, Temp folder will be used)</param> /// <returns>Path to saved on disk file (Temp folder)</returns> private string DownloadRemoteFile(string path, string localPath = null) { try { string pathToSave = localPath ?? Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); // Create an FTP Request var ftpRequest = (FtpWebRequest)FtpWebRequest.Create( $@"{HostUrl}{(HostUrl.EndsWith("/") ? string.Empty : "/")}{path}"); ftpRequest.Credentials = credentials; // Set options ftpRequest.UseBinary = true; ftpRequest.UsePassive = true; ftpRequest.KeepAlive = true; // Specify the Type of FTP Request ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; // Establish Return Communication with the FTP Server using (var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) { // Get the FTP Server's Response Stream using (var ftpStream = ftpResponse.GetResponseStream()) { // Save data on disk using (var localFileStream = new FileStream(pathToSave, FileMode.Create)) { byte[] byteBuffer = new byte[bufferSize]; int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); while (bytesRead > 0) { localFileStream.Write(byteBuffer, 0, bytesRead); bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); } } } } return(pathToSave); } catch (Exception ex) { throw new WebException( $"An error occurred when trying to download file {Path.GetFileName(path)}: {ex.Message}"); } }
public bool removeNode(HostUrl hostUrl) //this function will remove a host as a node that has sent the new reply { try { //check to find the host in the list for (int index = 0; index < this.hostsAddresses.Count; index++) { if (this.hostsAddresses[index].getHostUrl().Equals(hostUrl.getHostUrl()) && this.hostsAddresses[index].getPort() == hostUrl.getPort()) { this.hostsAddresses.RemoveAt(index); break; } } return(true); //whether to find or not it will send true! } catch (Exception) { return(false);//if the removing process fail in any cases, this false value will show this failure } }
//this will call by others //@Override public String requestModifyPermission(String requesterId, int requesterLogicalClock, int requestedAppointmentSequentialNumber, String requesterHostUrl, int requesterHostPort) { //the clock will manage here too //this will call by others to send [req<id,clock>] to this host //By receiving a req message we will send a true that mean we receive your message //if we want to accept the request at time we will send a OK instead //we must implement this pseudocode // ********************************************* // if (not accessing resource and do not want to access it) // send OK // else if (currently using resource) // queue request // else if (want to access resource too) // if (timestamp of request is smaller) // send OK // else //own timestamp is smaller // queue request // ********************************************* try{ if (!ServerStatus.getServerStatus()) { return(null); } long id = Convert.ToInt64(requesterId); HostUrl hostUrl = new HostUrl(requesterHostUrl, requesterHostPort); RequestObject requestObject = new RequestObject(id, requesterLogicalClock, hostUrl); //the clock for receiving message will correct here //the clock will correct in the constructor of ExternalLampartClock class based on following rule //LC = Max(LC, LCsender) + 1 String response = null; //if not accessing resource and do not want to access it --> send OK if (currentModifyRequest == null || (currentModifyRequest != null && requestedAppointmentSeqNum != 0 && requestedAppointmentSeqNum != requestedAppointmentSequentialNumber) ) { ExtendedLamportClockObject ELC = new ExtendedLamportClockObject(); //for increasing clock before send response = "true" + "\n"; //Send 'OK' response += " ID:&@[" + ELC.getIdString() + "]#! "; response += " LC:&@[" + ELC.getLogicalClock() + "]#! "; return(response); } //if currently using resource else if (currentModifyRequest != null && !currentModifyRequest.isWaiting() && requestedAppointmentSeqNum != 0 && requestedAppointmentSeqNum == requestedAppointmentSequentialNumber ) { modifyRequestsQueue.add(requestObject); response = "false" + "\n"; //Send 'NOKEY'//means 'you have to wait for me' return(response); } //if want to access resource too else if (currentModifyRequest != null && currentModifyRequest.isWaiting() && requestedAppointmentSeqNum != 0 && requestedAppointmentSeqNum == requestedAppointmentSequentialNumber ) { if (requestObject.getELCO().compare(currentModifyRequest.getELCO()) < 0) //if timestamp of requester is smaller { ExtendedLamportClockObject ELC = new ExtendedLamportClockObject(); //for increasing clock before send response = "true" + "\n"; //Send 'OK' response += " ID:&@[" + ELC.getIdString() + "]#! "; response += " LC:&@[" + ELC.getLogicalClock() + "]#! "; return(response); } else //if own timestamp is smaller { modifyRequestsQueue.add(requestObject); response = "false" + "\n"; //Send 'OK'//means 'you have to wait for me' return(response); } } else { return(null); } }catch (Exception) { return(null); } }
public bool removeNode(String ipv4Address, int port) //this function will remove a host as a node that has sent the new reply { HostUrl hostUrl = new HostUrl(port, ipv4Address); return(this.removeNode(hostUrl)); }
//setters public bool addNewNode(HostUrl hostUrl) //this function will add a host as a node that has received the new request { this.hostsAddresses.Add(hostUrl); return(this.hostsAddresses.Contains(hostUrl)); }
protected abstract bool synchronizeDataBase(HostUrl hostUrl);
private bool setDestinationHost(HostUrl hostUrl) { this.hostUrlAddress = hostUrl.getFullUrl(); return(true); //it comes from java implementation }