private ClientMetadata lookUpOnOtherServers(string username) { IConsistencyService[] server = new IConsistencyService[_servers.Count]; for (int i = 0; i < _servers.Count; i++) { server[i] = getOtherServers(_servers[i]); } callback returnedValueOnLookup1 = new callback(); callback.RemoteLookupDelegate RemoteDelforLookup1 = new callback.RemoteLookupDelegate(() => server[0].ReadClientMetadata(username)); AsyncCallback RemoteCallbackOnLookup1 = new AsyncCallback(returnedValueOnLookup1.OurLookupAsyncCallBack); IAsyncResult RemArForLookup1 = RemoteDelforLookup1.BeginInvoke(RemoteCallbackOnLookup1, null); callback.RemoteLookupDelegate RemoteDelforLookup2 = new callback.RemoteLookupDelegate(() => server[1].ReadClientMetadata(username)); IAsyncResult RemArForLookup2 = RemoteDelforLookup2.BeginInvoke(RemoteCallbackOnLookup1, null); bool dataEqual; ClientMetadata myData = action.ReadClientMetadata(username); Log.Show(_username, "[READ METADATA] Waiting for one server to return"); returnedValueOnLookup1.waiter.WaitOne(); //returnedValueOnLookup1.waiter.Reset(); //Compare the received value ClientMetadata DataFromServer1 = returnedValueOnLookup1.data; dataEqual = CompareValues(myData, DataFromServer1); if (dataEqual) { Log.Show(_username, "[READ METADATA] First retreived value matches"); return myData; } else { Log.Show(_username, "[READ METADATA] Waiting for second server to return"); returnedValueOnLookup1.waiter.WaitOne(); //returnedValueOnLookup1.waiter.Reset(); Log.Show(_username, "[READ METADATA] Second server returned"); ClientMetadata DataFromServer2 = returnedValueOnLookup1.data; dataEqual = CompareValues(myData, DataFromServer2); if (dataEqual) { Log.Show(_username, "[READ METADATA] Second retreived value matches"); return myData; } else { dataEqual = CompareValues(DataFromServer1, DataFromServer1); if (dataEqual) { Log.Show(_username, "[READ METADATA] I have an outdated value. Other two fetched values match"); action.WriteClientMetadata(returnedValueOnLookup1.data); Log.Show(_username, "[READ METADATA] Updated my value to one of the received values"); return returnedValueOnLookup1.data; } else { Log.Show(_username, "[READ METADATA]ERROR - ERROR"); return null; } } } }
private ClientMetadata lookUpOnOtherServers(string username) { IConsistencyService[] server = new IConsistencyService[_servers.Count]; for (int i = 0; i < _servers.Count; i++) { server[i] = getOtherServers(_servers[i]); } callback returnedValueOnLookup1 = new callback(); callback.RemoteLookupDelegate RemoteDelforLookup1 = new callback.RemoteLookupDelegate(() => server[0].ReadClientMetadata(username)); AsyncCallback RemoteCallbackOnLookup1 = new AsyncCallback(returnedValueOnLookup1.OurLookupAsyncCallBack); IAsyncResult RemArForLookup1 = RemoteDelforLookup1.BeginInvoke(RemoteCallbackOnLookup1, null); callback.RemoteLookupDelegate RemoteDelforLookup2 = new callback.RemoteLookupDelegate(() => server[1].ReadClientMetadata(username)); IAsyncResult RemArForLookup2 = RemoteDelforLookup2.BeginInvoke(RemoteCallbackOnLookup1, null); bool dataEqual; ClientMetadata myData = action.ReadClientMetadata(username); Log.Show(_username, "[READ METADATA] Waiting for one server to return"); returnedValueOnLookup1.waiter.WaitOne(); //returnedValueOnLookup1.waiter.Reset(); //Compare the received value ClientMetadata DataFromServer1 = returnedValueOnLookup1.data; dataEqual = CompareValues(myData, DataFromServer1); if (dataEqual) { Log.Show(_username, "[READ METADATA] First retreived value matches"); return(myData); } else { Log.Show(_username, "[READ METADATA] Waiting for second server to return"); returnedValueOnLookup1.waiter.WaitOne(); //returnedValueOnLookup1.waiter.Reset(); Log.Show(_username, "[READ METADATA] Second server returned"); ClientMetadata DataFromServer2 = returnedValueOnLookup1.data; dataEqual = CompareValues(myData, DataFromServer2); if (dataEqual) { Log.Show(_username, "[READ METADATA] Second retreived value matches"); return(myData); } else { dataEqual = CompareValues(DataFromServer1, DataFromServer1); if (dataEqual) { Log.Show(_username, "[READ METADATA] I have an outdated value. Other two fetched values match"); action.WriteClientMetadata(returnedValueOnLookup1.data); Log.Show(_username, "[READ METADATA] Updated my value to one of the received values"); return(returnedValueOnLookup1.data); } else { Log.Show(_username, "[READ METADATA]ERROR - ERROR"); return(null); } } } }