32 lines
909 B
Java
32 lines
909 B
Java
package org.syndicate_lang.actors.example.example2;
|
|
|
|
import org.syndicate_lang.actors.Entity;
|
|
import org.syndicate_lang.actors.Ref;
|
|
import org.syndicate_lang.actors.Turn;
|
|
|
|
public class Forwarder extends Entity {
|
|
private final Ref _main;
|
|
private final int _nRounds;
|
|
private Ref _peer = null;
|
|
|
|
public Forwarder(Ref main, int nRounds) {
|
|
this._main = main;
|
|
this._nRounds = nRounds;
|
|
}
|
|
|
|
@Override
|
|
public void retract_(Turn turn, Long handle) {
|
|
turn.quit();
|
|
}
|
|
|
|
@Override
|
|
public void message_(Turn turn, Object body) {
|
|
if (body instanceof IForwarder.HandleMessage op) {
|
|
Ref target = op.hopCount() >= this._nRounds - 1 ? _main : _peer;
|
|
turn.message_(target, new IForwarder.HandleMessage(op.hopCount() + 1));
|
|
} else if (body instanceof IForwarder.SetPeer op) {
|
|
this._peer = op.peer();
|
|
}
|
|
}
|
|
}
|