Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
Last active August 29, 2015 14:01
Show Gist options
  • Save kevinlynx/02b82f9347c848369ff6 to your computer and use it in GitHub Desktop.
Save kevinlynx/02b82f9347c848369ff6 to your computer and use it in GitHub Desktop.
actor model
class Printer : public Theron::Actor
{
public:
// Constructor, passes the framework to the baseclass.
Printer(Theron::Framework &framework) : Theron::Actor(framework)
{
// Register the message handler.
RegisterHandler(this, &Printer::Print);
}
private:
// Handler for messages of type std::string.
void Print(const std::string &message, const Theron::Address from)
{
// Print the string.
printf("%s\n", message.c_str());
// Send a dummy message back for synchronization.
Send(0, from);
}
};
int main()
{
// Construct a framework and instantiate a Printer within it.
Theron::Framework framework;
Printer printer(framework);
// Construct a receiver to receive the reply message.
Theron::Receiver receiver;
// Send a string message to the Printer.
// We pass the address of the receiver as the 'from' address.
if (!framework.Send(
std::string("Hello world!"),
receiver.GetAddress(),
printer.GetAddress()))
{
printf("ERROR: Failed to send message\n");
}
// Synchronize with the dummy message sent in reply to make sure we're done.
receiver.Wait();
}
% module interfaces
start_link() ->
gen_server:start_link(?MODULE, [], []).
stats(Pid) ->
gen_server:call(Pid, get_stats, infinity).
download(Pid, MagHash) ->
gen_server:cast(Pid, {download, MagHash, self()}).
% framework callbacks
handle_cast({download, MagHash, From}, State) ->
#state{reqs = Reqs, hashSum = H, reqSum = R} = State,
UpdateReqs = Reqs,
NewReqs = create_download(UpdateReqs, MagHash, From),
NewSum = R + 1 - (gb_trees:size(Reqs) - gb_trees:size(UpdateReqs)),
{noreply, State#state{reqs = NewReqs, hashSum = H + 1, reqSum = NewSum}};
handle_call(get_stats, _From, State) ->
#state{hashSum = H, reqSum = R, totalTime = T, reqs = Reqs} = State,
{reply, {H, R, T, gb_trees:size(Reqs)}, State};
% pid ! message
object Test {
val system = ActorSystem("AwaitProducts")
var reqKeeper: ActorRef = null
def initialize(pCnt: Int, imgCnt: Int, l: ProgressListener) = {
assert(pCnt > 0)
listener = l
reqKeeper = system.actorOf(Props[ReqKeeper])
}
def process(id: String, filterExpr: String, name: String, findPatent: Boolean,
pl: ProcessListener) = {
reqKeeper ! new ProductReq(id, filterExpr, findPatent, pl)
}
}
class ReqKeeper extends Actor {
val logger = LoggerFactory.getLogger(this.getClass())
val awaits = new ListBuffer[ProductReq]
def receive = {
case req: ProductReq => append(req)
case 'get => response
case ('done, q: ProductReq, pat: Option[Patent]) => productDone(q, pat)
case m => logger.error("unknown message: " + m.toString)
}
private def response = {
val req = if (awaits.size == 0) new ProductReq
else {
val req = awaits.remove(0)
logger.debug(s"response req ${req.id}, remain ${awaits.size}")
req
}
sender ! req
}
}
-module(echo).
-export([go/0, loop/0]).
go() ->
Pid2 = spawn(echo, loop, []),
Pid2 ! {self(), hello},
receive
{Pid2, Msg} ->
io:format("P1 ~w~n",[Msg])
end,
Pid2 ! stop.
loop() ->
receive
{From, Msg} ->
From ! {self(), Msg},
loop();
stop ->
true
end.
template <typename T>
class Future {
public:
T get(int timeout = INFINITE);
T get(std::function<void (T &)> cb, int timeout = INFINITE);
void cancel();
bool isDone();
};
template <typename T>
class Promise {
public:
Future<T> &future();
void success(T ret);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment