/**************************************************************** * CALLBACK FUNCTIONS ****************************************************************/ private void PropesedSeqCallback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); if (!ValidView(response)) { return; } if (response.Code.Equals("proposedSeq")) { //Console.WriteLine("Proposed Seq"); lock (ProposedSeq) { // Store porposed sequence number ProposedSeq.Add(response.SequenceNumber); Interlocked.Increment(ref AcksCounter); } } }
/// <summary> /// Callback function for the phase 1 of the take operation. /// </summary> /// <param name="result">Async calback result.</param> public void Take1Callback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); //Console.WriteLine("take1" + response); if (!ValidView(response)) { return; } // Stores the list of matching tuples // and the RequestID of the server that answered if (response.Code.Equals("OK")) { // Tuples have to be added before the acks are incremented lock (MatchingTuples) { MatchingTuples.Add(new List <ITuple>(response.Tuples)); Interlocked.Increment(ref AcksCounter); } if (verbose) { Console.WriteLine("Response:"); Console.WriteLine(response); } } }
// This is the call that the AsyncCallBack delegate will reference. public void OurRemoteAsyncCallBack(IAsyncResult ar) { Monitor.Enter(this); try { try { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)ar).AsyncDelegate; _status = del.EndInvoke(ar); Console.WriteLine("\nSIGNALLED STATUS" + _status); waiter.Set(); Thread.Sleep(WAIT_TIME); } catch (Exception e) { Console.WriteLine("Exception" + e.Message); _status = false; Console.WriteLine("\nFAILURE SIGNALLED STATUS" + _status); waiter.Set(); Thread.Sleep(WAIT_TIME); } return; } finally { Monitor.Exit(this); } }
/// <summary> /// Callback function for the read operation. /// </summary> /// <param name="result">Async callback result.</param> public void ReadCallback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); if (!ValidView(response)) { return; } // Stores the tuple returned // and the RequestID of the server that answered if (response.Code.Equals("OK")) { lock (LockRef) { if (response.Tuple != null) { Tuple = response.Tuple; } } IncrementAcksCounter(response.RequestID); } }
private void ReadCallback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); if (!ValidView(response)) { return; } // Stores the tuple returned // and the OperationID of the server that answered if (response.Code.Equals("OK")) { if (response.Tuple != null) { lock (LockRef) { Tuple = response.Tuple; } if (verbose) { Console.WriteLine("Response:"); Console.WriteLine(response); } } Interlocked.Increment(ref AcksCounter); } }
private void WaitAnyCallback(IAsyncResult asyncResult) { AsyncResult ar = (AsyncResult)asyncResult; RemoteAsyncDelegate remoteDel = (RemoteAsyncDelegate)ar.AsyncDelegate; Message responseMessage = remoteDel.EndInvoke(asyncResult); CallbackState state = (CallbackState)ar.AsyncState; Message originalMessage = state.OriginalMessage; string remoteUrl = state.RemoteUrl; lock (ReplyResultQueue) { ReplyResultQueue.TryGetValue(originalMessage, out var replyResult); if (replyResult == null) { return; } // save the result replyResult.AddResult(remoteUrl, responseMessage); // if ANY result, notify the caller (the one who initiated the multi-cast) if (replyResult.IsDone()) { PulseMessage(originalMessage); } } }
public void OurRemoteAsyncCallBack(IAsyncResult ar) { lock (_sync) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)ar).AsyncDelegate; del.EndInvoke(ar); //e.Set(); } }
public static void OurRemoteAsyncCallBack(IAsyncResult ar) { // Alternative 2: Use the callback to get the return value RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)ar).AsyncDelegate; del.EndInvoke(ar); Console.WriteLine("\r\n**SUCCESS**: Result of the remote AsyncCallBack: "); return; }
public void OurRemoteAsyncCallBack(IAsyncResult ar) { // ASyncResult encapsule le résultat issu d'un appel asynchrone ; // AsyncDelegate permet de récupérer l'objet delegate sur lequel l'appel asynchrone a été invoqué. // ligne suivante, on ne fait donc que récupérer dans del le delegate sur lequel l'appel asynchrone a été effectué RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)ar).AsyncDelegate; Console.WriteLine("\r\n**SUCCESS**: Result of the remote AsyncCallBack: " + del.EndInvoke(ar)); // Signal the thread. Set : événement signalé, les threads en attente peuvent poursuivre e.Set(); return; }
static void Main() { // allocate and register channel TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel, true); // get reference to remote service MyRemoteInterface obj = (MyRemoteInterface)Activator.GetObject( typeof(MyRemoteInterface), "tcp://localhost:8086/MyRemoteObjectName"); try { // first a simple synchronous call Console.WriteLine(obj.MetodoOla()); // change this to true to use the callback (alt.2) bool useCallback = false; if (!useCallback) { // Alternative 1: asynchronous call without callback // Create delegate to remote method RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.MetodoOla); // Call delegate to remote method IAsyncResult RemAr = RemoteDel.BeginInvoke(null, null); // Wait for the end of the call and then explictly call EndInvoke RemAr.AsyncWaitHandle.WaitOne(); Console.WriteLine(RemoteDel.EndInvoke(RemAr)); } else { // Alternative 2: asynchronous call with callback // Create delegate to remote method RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.MetodoOla); // Create delegate to local callback AsyncCallback RemoteCallback = new AsyncCallback(Client.OurRemoteAsyncCallBack); // Call remote method IAsyncResult RemAr = RemoteDel.BeginInvoke(RemoteCallback, null); } } catch (SocketException) { System.Console.WriteLine("Could not locate server"); } catch (MyException e) { Console.WriteLine("I caught an exception: " + e.campo); Console.WriteLine("I caught an exception: " + e.mo.OlaSemExcepcao()); } Console.ReadLine(); }
public Message SendMessageToRemote(RemotingEndpoint remotingEndpoint, Message message) { try { RemoteAsyncDelegate remoteDel = new RemoteAsyncDelegate(remotingEndpoint.OnReceiveMessage); IAsyncResult ar = remoteDel.BeginInvoke(message, null, null); //ar.AsyncWaitHandle.WaitOne(); // This should not be necessary, since EndInvoke already waits for completion // unless we want to provide a timeout a do something with it return(remoteDel.EndInvoke(ar)); } catch (Exception e) { Log("Server at " + remotingEndpoint.EndpointURL + " is unreachable. (For more detail: " + e.Message + ")"); throw new SocketException(); } }
/*********************************************************** * CALLBACK FUNCTIONS ***********************************************************/ /// <summary> /// Callback function for the acknowledgements. /// </summary> /// <param name="result">Async callback result.</param> public void AddCallback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); if (!ValidView(response)) { return; } if (response.Code.Equals("ACK")) { IncrementAcksCounter(response.RequestID); } }
/// <summary> /// Callback function for the acknowledgements. /// </summary> /// <param name="result">Async call result.</param> private void AcksCallback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); if (!ValidView(response)) { return; } if (response.Code.Equals("ACK")) { Interlocked.Increment(ref AcksCounter); } }
private void CallNextReplica(int index, List <string> urls, TuplePack inputPack) { RemoteAsyncDelegate remoteDel = new RemoteAsyncDelegate(GetDownstreamReplicas(urls)[index].Dispatch); if (_semantic.Equals("at-most-once")) { remoteDel.BeginInvoke(inputPack, null, null); } else if (_semantic.Equals("exactly-once") || _semantic.Equals("at-least-once")) { remoteDel.BeginInvoke( inputPack, (IAsyncResult ar) => { try { remoteDel.EndInvoke(ar); } // it crashed catch (SocketException e) { TryAgain(inputPack, index, urls); } // it may be slowed catch (Exception e) { TryAgain(inputPack, index, urls); } }, null); } else { Console.WriteLine("I don't support such semantic!"); } }
private void PropesedSeqCallback(IAsyncResult result) { RemoteAsyncDelegate del = (RemoteAsyncDelegate)((AsyncResult)result).AsyncDelegate; // Retrieve results. TSpaceMsg response = del.EndInvoke(result); if (response == null || (response.Code.Equals("badView") && response.MsgView.ID > TSMan.ServerView.ID)) { return; } if (response.Code.Equals("proposedSeq")) { lock (ProposedSeq) { // Store porposed sequence number ProposedSeq.Add(response.SequenceNumber); Interlocked.Increment(ref AcksCounter); } } }
/// <summary> /// Method the will be called by the threads the DisseminateReservationw will create. /// This method will make a remote call to a specific user (proxy) /// </summary> /// <param name="proxy"></param> /// <param name="reservation"></param> /// private void CallSendReservation(object context) { Console.WriteLine("[Calling] CallSendReservation"); lock (User.userLoger) { User.userLoger.IncrementNumberOfExchangedMessages("CallSendReservation"); } ThreadInfo threadInfo = context as ThreadInfo; UserInterface proxy = threadInfo.proxy; Reservation reservation = threadInfo.reservationMessage.reservation; Console.WriteLine("Thread Number: " + threadInfo.threadNumber); ReservationMessage response = null; RemoteAsyncDelegate remoteDelegate = new RemoteAsyncDelegate(proxy.SendReservation); String userName = threadInfo.userName; int originalMessageCounter = threadInfo.reservationMessage.messageCounter; String threadName = Thread.CurrentThread.Name; JacobsonKarels timeoutAlgoritm = User.KnownUsersTimeout[userName]; timeoutAlgoritm.ModifiedUpdateTimeout(); int timeout = timeoutAlgoritm.GetTimeout(); //dissemination if (threadName.Length == 3) { timeout = timeout / 2; } IAsyncResult result = remoteDelegate.BeginInvoke(threadInfo.reservationMessage, timeout, null, null); //colocar timeout dinamico! bool responseInTime = result.AsyncWaitHandle.WaitOne(timeout, true); Console.WriteLine("Work time is over! Response in time? " + responseInTime); //didn't get a response till timeout if (responseInTime == false) { Console.WriteLine("Timeout! Did not receive any answer!"); result.AsyncWaitHandle.Close(); response = new ReservationMessage(); //add an empty dicionary indicating that the user did not answered response.AddSlotResponse(userName, new Dictionary<int, String>()); result.AsyncWaitHandle.Close(); } else { IncrementUserTrust(threadInfo.userName); response = remoteDelegate.EndInvoke(result); Console.WriteLine("Received: " + response); result.AsyncWaitHandle.Close(); } String currentThreadName = Thread.CurrentThread.Name; char[] separator = { ' ' }; String[] splitedThreadName = currentThreadName.Split(separator); String currentThreadNumber = splitedThreadName[0]; int currentTNumber = int.Parse(currentThreadNumber); if (threadResponses.ContainsKey(currentTNumber)) { threadResponses.Remove(currentTNumber); } //adds the response the response dicionary //if is the retry if (originalMessageCounter < 0) { if (threadResponses2.ContainsKey(threadInfo.userName)) threadResponses2.Remove(threadInfo.userName); threadResponses2.Add(threadInfo.userName, response); } else threadResponses.Add(threadInfo.threadNumber, response); }
private void CallCommitReservation(object context) { ThreadInfo threadInfo = context as ThreadInfo; UserInterface proxy = threadInfo.proxy; Console.WriteLine("Thread Number: " + threadInfo.threadNumber); lock (User.userLoger) { User.userLoger.IncrementNumberOfExchangedMessages("CallCommitReservation"); } ReservationMessage response = null; RemoteAsyncDelegate remoteDelegate = new RemoteAsyncDelegate(proxy.CommitReservation); String userName = threadInfo.userName; JacobsonKarels timeoutAlgoritm = User.KnownUsersTimeout[userName]; timeoutAlgoritm.ModifiedUpdateTimeout(); int timeout = timeoutAlgoritm.GetTimeout(); IAsyncResult result = remoteDelegate.BeginInvoke(threadInfo.reservationMessage, timeout, null, null); bool responseInTime = result.AsyncWaitHandle.WaitOne(timeout, true); Console.WriteLine("Work time is over! Response in time? " + responseInTime); //didn't get a response till timeout if (responseInTime == false) { Console.WriteLine("Timeout! Did not receive any answer!"); result.AsyncWaitHandle.Close(); return; } IncrementUserTrust(threadInfo.userName); response = remoteDelegate.EndInvoke(result); Console.WriteLine("Commit!"); result.AsyncWaitHandle.Close(); return; }
private void CallTwoPhaseCommit(object context) { ThreadInfo threadInfo = context as ThreadInfo; UserInterface proxy = threadInfo.proxy; Console.WriteLine("Thread Number: " + threadInfo.threadNumber); lock (User.userLoger) { User.userLoger.IncrementNumberOfExchangedMessages("CallTwoPhaseCommit"); } ReservationMessage response = new ReservationMessage(); RemoteAsyncDelegate remoteDelegate = new RemoteAsyncDelegate(proxy.TwoPhaseCommit); String userName = threadInfo.userName; JacobsonKarels timeoutAlgoritm = User.KnownUsersTimeout[userName]; timeoutAlgoritm.ModifiedUpdateTimeout(); int timeout = timeoutAlgoritm.GetTimeout(); IAsyncResult result = remoteDelegate.BeginInvoke(threadInfo.reservationMessage, timeout, null, null); bool responseInTime = result.AsyncWaitHandle.WaitOne(timeout, true); Console.WriteLine("Work time is over! Response in time? " + responseInTime); //didn't get a response till timeout if (responseInTime == false) { Console.WriteLine("Timeout! Did not receive any answer!"); if(!response.response2PC.ContainsKey(userName)) response.response2PC.Add(userName,"Timeout");; result.AsyncWaitHandle.Close(); return; } if (!response.response2PC.ContainsKey(userName)) response.response2PC.Add(userName, "YES"); IncrementUserTrust(threadInfo.userName); response = remoteDelegate.EndInvoke(result); Console.WriteLine("Can Commit!"); result.AsyncWaitHandle.Close(); String currentThreadName = Thread.CurrentThread.Name; char[] separator = { ' ' }; String[] splitedThreadName = currentThreadName.Split(separator); String currentThreadNumber = splitedThreadName[0]; int currentTNumber = int.Parse(currentThreadNumber); if (threadResponses.ContainsKey(currentTNumber)) { threadResponses.Remove(currentTNumber); } int originalMessageCounter = threadInfo.reservationMessage.messageCounter; //if is the retry if (originalMessageCounter < 0) { if (threadResponses2.ContainsKey(threadInfo.userName)) threadResponses2.Remove(threadInfo.userName); threadResponses2.Add(threadInfo.userName, response); } else threadResponses.Add(threadInfo.threadNumber, response); }