From: Jeff Rose Date: 2006-02-07T22:32:54+09:00 Subject: Re: Seeking Continuations Links ------=_Part_609_12198989.1139319159543 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 2/1/06, James Edward Gray II wrote: > > Myself and a few others are trying to get together a "Playing Around > with Continuations" project. As a start, we are collecting any > resources we can find about them. > I don't have a link, but last night a buddy and I found a cool usage for continuations and AOP in implementing a network simulator. The general ide= a might work for testing drb code as well, although we haven't gone that far yet. Our simulator is a typical discrete event based simulator, and previously w= e had been using messages (packets) to communicate between virtual nodes in the network. For our current project we wanted to implement a p2p algorith= m in an rpc style though, which seemingly would require us to implement proxy objects for everything that would send messages so it can all go through th= e simulator. Then we realized that by just wrapping any methods which should be treated as "remote" with some message passing code we could possibly use the same code for simulation as for an implementation (yet to be verified o= n anything real...). Either way this saves a lot of time, and after some initial unit testing it seems to be working correctly. Here is the wrapper method, which hopefully shows how it is working: (It's shorter than it looks. Mostly just comments so a couple months down the line these continuations don't require another neural re-wiring :-) ---------------------------------- module GoSim module Net # Wrap the methods of a class so they are called through the # network simulator. def Net.remote_method(clazz, *methods) methods.each do |meth| # Store the original method so we can call it eventually. original =3D clazz.instance_method(meth) clazz.instance_eval do # Define a new method with the same name, which sends # a message on the call and then another on the return. define_method(meth) do |*a| # Save a continuation to return to this point when the # event fires at a later time. cont =3D nil from_sched =3D callcc {|cont|} # Post the message event and jump back into the # event scheduling loop. Note that from_sched will be # true when the continuation is called later. GoSim::Net::Topology.instance. transmit_continuation(cont) unless from_sched # Make the actual method call retval =3D original.bind(self).call(*a) # Now do it over again for the return value. cont =3D nil ret =3D callcc {|cont|} GoSim::Net::Topology.instance. transmit_continuation(cont) if ret.nil? retval end end end end # def remote_method end # Net end # GoSim ---------------------------------- Make sense? Think this could work to simulate a bunch of drb objects communicating with each other? Could be a handy way to unit test distributed applications etc... -Jeff ------=_Part_609_12198989.1139319159543--