Skip to content

Instantly share code, notes, and snippets.

public class ReadyReminderClientMokapotWithReminders {
public static void main(String[] args) throws Exception {
DistributedCommunicator communicator
= new DistributedCommunicator("test2.p12",
"testpassword1".toCharArray());
communicator.startCommunication();
CommunicationAddress serverAddress
= communicator.lookupAddress(InetAddress.getLoopbackAddress(), 15238);
public class ReadyReminderClientLocalWithReminders {
public static void main(String[] args) throws Exception {
ReadyReminderServer<String> server = new ReadyReminderServer<>();
final PrintStream out = System.out;
server.submitEventWithReminder("two seconds", Instant.now().plusSeconds(2),
() -> out.println("reminder for two-second event"));
server.submitEvent("one second", Instant.now().plusSeconds(1));
server.submitEventWithReminder("four seconds", Instant.now().plusSeconds(4),
@FunctionalInterface
public interface XWWWFormUrlencodedHTTPHandler extends HttpHandler {
/**
* Handles an HTTP request encoded as <code>x-www-form-urlencoded</code>.
* This format takes a set of key-value pairs as its input. This method does
* not place requirements on what format is used for the output.
*
* @param params The parameters given in the request, a set of key/value
* pairs.
public class ReadyReminderServerHTTP {
public static void main(String[] args) throws Exception {
ReadyReminderServer<String> list = new ReadyReminderServer<>();
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 15230), 4);
server.createContext("/ReadyReminder", (XWWWFormUrlencodedHTTPHandler) (params, method) -> {
if (!method.equals("POST")) {
return stringResponse("request method " + method + " is inapplicable\n", 405);
}
public final class XWWWFormUrlencodedHTTPRequest extends HashMap<String, String> {
private static final long serialVersionUID = 0xe673f874cf17d539L;
public XWWWFormUrlencodedHTTPRequest(URL url, Object... params)
throws IOException, IllegalArgumentException {
this(url, evenLengthListToMap(params));
}
@SuppressWarnings("LeakingThisInConstructor")
public class ReadyReminderClientHTTP {
private static void submitEvent(URL url, String event, Instant readyAt)
throws IOException {
new XWWWFormUrlencodedHTTPRequest(url, "command", "submitEvent",
"event", event, "readyAt", readyAt);
}
private static String extractReadyEvent(URL url) throws IOException {
return new XWWWFormUrlencodedHTTPRequest(url, "command", "extractReadyEvent")
public class ReadyReminderClientMokapot {
public static void main(String[] args) throws Exception {
// create and start the communicator
DistributedCommunicator communicator =
new DistributedCommunicator("test2.p12",
"testpassword1".toCharArray());
communicator.startCommunication();
// retrieve communicator address
CommunicationAddress serverAddress =
ReadyReminderServer<String> server =
communicator.runRemotely
(ReadyReminderServer<String>::new, serverAddress);
public class ReadyReminderClientLocal
{
public static void main(String[] args) throws Exception {
ReadyReminderServer<String> server = new ReadyReminderServer<>();
server.submitEvent("two seconds", Instant.now().plusSeconds(2));
server.submitEvent("one second", Instant.now().plusSeconds(1));
server.submitEvent("three seconds", Instant.now().plusSeconds(3));
System.out.println(Objects.toString(server.extractReadyEvent()));
public class ReadyReminderServer<T> {
private final PriorityQueue<Entry> queue = new PriorityQueue<>();
public void submitEvent(T event, Instant readyAt) {
queue.add(new Entry(event, readyAt));
System.err.println("# submitEvent(" + event + ", " + readyAt + ")");
}
public T extractReadyEvent() {