package juglr.net;

import juglr.*;

import static juglr.net.HTTP.*;
import static juglr.StructuredMessage.Type;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.channels.SocketChannel;

/**
 * A simple example that uses a HTTPMessageBus to expose a service that
 * calculates whether or not a given number is a prime.
 * <p/>
 * JSON messages should be <code>POST</code>ed to
 * <a href="http://localhost:4567/actor/calc">http://localhost:4567/calc</a>
 * looking like:
 * <pre>
 * {
 *    "isPrime" : 123
 * }
 * </pre>
 *
 * To invoke the 'list' or 'ping' handlers you can point your browser at
 * <a href="http://localhost:4567/list">http://localhost:4567/list</a> or
 * <a href="http://localhost:4567/ping/calc">http://localhost:4567/ping/calc</a>.
 * Or simply use a command line tool like <code>wget</code> to send
 * <code>GET</code> requests to those URLs.
 */
public class HTTPServerExample {

    static class CalcActor extends Actor {

        public void start() {
            try {
                getBus().allocateNamedAddress(this, "calc");
            } catch (AddressAlreadyOwnedException e) {
                e.printStackTrace();
            }
        }

        public void react(Message msg) {
            if (!(msg instanceof StructuredMessage)) {
                throw new MessageFormatException("Expected StructuredMessage");
            }

            StructuredMessage resp = new StructuredMessage(Type.MAP);
            StructuredMessage json = (StructuredMessage)msg;
            if (!json.has("isPrime")) {
                resp.put("error", "No 'isPrime' key in request");
                send(resp, msg.getSender());
                return;
            }
            String test = json.get("isPrime").toString();

            try {
                BigInteger bigInt = new BigInteger(test);

                // We guess right with 0.9990234375 probab
                if (bigInt.isProbablePrime(10)) {
                    resp.put("response", "true");
                } else {
                    resp.put("response", "false");
                }
                send(resp, msg.getSender());
            } catch (NumberFormatException e) {
                resp.put("error", "Not a valid integer");
                send(resp, msg.getSender());
                return;
            }
        }
    }

    public static void main (String[] args) throws Exception {
        System.setProperty("juglr.busclass", "juglr.net.HTTPMessageBus");
        Actor actor = new CalcActor();
        MessageBus.getDefault().start(actor.getAddress());

        synchronized (actor) {
            actor.wait(); // Indefinte non-busy block
        }
    }

}

@@ -336,16 +336,311 @@
 a prime.
+ Workload is spead%0A * over three parallel calculators.%0A * %3Cp/%3E%0A * Compile with:%0A *     javac -Xbootclasspath/p:../lib/jsr166.jar -classpath ../juglr-0.0.1.jar HTTPServerExample.java%0A *%0A * Run with:%0A *     java -Xbootclasspath/p:../lib/jsr166.jar -classpath ../juglr-0.0.1.jar:. HTTPServerExample
 %0A * %3Cp/%3E
@@ -1130,16 +1130,40 @@
 ode%3E
+ or %3Ccode%3Ecurl%3C/code%3E%0A *
  to send
 %0A * 
@@ -1158,19 +1158,16 @@
  to send
-%0A *
  %3Ccode%3EG
@@ -1206,312 +1206,296 @@
 .%0A *
-/%0Apublic class HTTPServerExample %7B%0A%0A    static class CalcActor extends Actor %7B%0A%0A        public void start() %7B%0A            try %7B%0A                getBus().allocateNamedAddress(this, %22calc%22);%0A            %7D catch (AddressAlreadyOwnedException e)
+ %3Cp/%3E%0A * To POST an isPrime request using %3Ccode%3Ecurl%3C/code%3E use the following command%0A * in a Unix terminal:%0A * %3Cpre%3E%0A *     curl http://localhost:4567/actor/calc --data '%7B %22isPrime%22 : 982451653 %7D'%0A * %3C/pre%3E%0A */%0Apublic class HTTPServerExample
  %7B%0A
+%0A
     
-            e.printStackTrace();%0A            %7D%0A        %7D
+static class CalcActor extends Actor %7B
 %0A%0A  
@@ -1995,32 +1995,33 @@
 ;%0A            %7D%0A
+%0A
             Stri
@@ -2062,17 +2062,16 @@
 ring();%0A
-%0A
         
@@ -2197,16 +2197,21 @@
 5 probab
+ility
 %0A       
@@ -2597,32 +2597,8 @@
 ));%0A
-                return;%0A
     
@@ -2687,16 +2687,79 @@
 ption %7B%0A
+        // Make sure the default message bus is HTTPMessageBus%0A
         
@@ -2828,44 +2828,250 @@
 %22);%0A
-        Actor actor = new CalcActor(
+%0A        // Delegate work to three CalcActors in a round-robin manner%0A        Actor actor = new SwarmActor(%0A                new CalcActor(), new CalcActor(), new CalcActor());%0A        MessageBus.getDefault().allocateNamedAddress(actor, %22calc%22
 );%0A 
@@ -3125,24 +3125,61 @@
 ddress());%0A%0A
+        // Indefinite non-busy block%0A
         sync
@@ -3226,36 +3226,8 @@
 t();
- // Indefinte non-busy block
 %0A   

Document HTTPServerExample