package galileo.samples.net;

import java.io.IOException;

import java.util.Random;

import galileo.event.ConcurrentEventReactor;
import galileo.event.EventContext;
import galileo.event.EventHandler;
import galileo.event.EventReactor;
import galileo.event.EventWithSynopsis;
import galileo.event.SynopsisWrapper;
import galileo.net.ClientMessageRouter;
import galileo.net.NetworkDestination;

public class SynopsisClient {

    private NetworkDestination server;

    private ClientMessageRouter messageClient;
    private ConcurrentEventReactor eventReactor;

    public SynopsisClient(NetworkDestination server)
    throws IOException {
        this.server = server;
        eventReactor = new ConcurrentEventReactor(
                this, new SynopsisWrapper(), 1);
        eventReactor.start();
        messageClient = new ClientMessageRouter();
        messageClient.addListener(eventReactor);
    }

    public void sendMessages()
    throws IOException {
        Random random = new Random();

        /* Send a simple, empty message */
        EventWithSynopsis event1 = new EventWithSynopsis(
                "SynopsisA", new byte[3]);
        messageClient.sendMessage(server, eventReactor.wrapEvent(event1));

        /* How about some random data? */
        byte[] data = new byte[8192];
        random.nextBytes(data);
        EventWithSynopsis event2 = new EventWithSynopsis("SynopsisA", data);
        messageClient.sendMessage(server, eventReactor.wrapEvent(event2));


        /* Send some random bytes on SynopsisB.  The server will print out the
         * hex representation of the data. */
        byte[] smallData = new byte[100];
        random.nextBytes(smallData);
        EventWithSynopsis event3
            = new EventWithSynopsis("SynopsisB", smallData);
        messageClient.sendMessage(server, eventReactor.wrapEvent(event3));

        /* Send something on SynopsisC.  This should make the server send us a
         * reply. */
        EventWithSynopsis event4
            = new EventWithSynopsis("SynopsisC", new byte[1]);
        messageClient.sendMessage(server, eventReactor.wrapEvent(event4));

        /* Send something on a special synopsis, 'ShutdownRequest'.  This will
         * make the server send us a 'Shutdown' reply that we will obey */
        EventWithSynopsis event5
            = new EventWithSynopsis("ShutdownRequest", new byte[1]);
        messageClient.sendMessage(server, eventReactor.wrapEvent(event5));
    }

    @EventHandler
    public void processEvent(EventWithSynopsis event, EventContext context)
    throws IOException {
        String synopsis = event.getSynopsis();

        System.out.println("We got a reply from the server!");
        System.out.println("Synopsis: " + synopsis);

        if (synopsis.equals("Shutdown")) {
            System.out.println("Goodbye!");
            messageClient.shutdown();
            System.exit(1);
        }
    }

    public static void main(String[] args)
    throws Exception {
        if (args.length < 1) {
            System.out.println("Usage: galileo.samples.net.SynopsisClient "
                    + "<server hostname>");
            System.exit(1);
        }

        String hostname = args[0];
        NetworkDestination server = new NetworkDestination(
                hostname, SynopsisServer.DEFAULT_PORT);

        SynopsisClient client = new SynopsisClient(server);
        client.sendMessages();
    }
}

@@ -2122,24 +2122,263 @@
 (event4));%0A%0A
+        /* How about a synopsis the server won't know about? */%0A        EventWithSynopsis event5%0A            = new EventWithSynopsis(%22BlahSynopsis%22, new byte%5B1%5D);%0A        messageClient.sendMessage(server, eventReactor.wrapEvent(event5));%0A%0A
         /* S
@@ -2444,16 +2444,16 @@
 is will%0A
-
         
@@ -2542,33 +2542,33 @@
 thSynopsis event
-5
+6
 %0A            = n
@@ -2687,25 +2687,25 @@
 pEvent(event
-5
+6
 ));%0A    %7D%0A%0A 

Send a 'bad' message synopsis as well