/*
Copyright (c) 2014, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package galileo.test;

import galileo.net.ClientMessageRouter;
import galileo.net.NetworkDestination;

/**
 * Sends events to a HashTestServer instance that will verify the events
 * reached their destination without corruption.
 *
 * @author malensek
 */
public class HashTestClient {

    private ClientMessageRouter messageRouter;
    private NetworkDestination netDest;

    public HashTestClient(NetworkDestination netDest) throws Exception {
        this.netDest = netDest;
        messageRouter = new ClientMessageRouter();
    }

    public void disconnect() {
        messageRouter.shutdown();
    }

    public void test(int size, int messages)
    throws Exception {
        test(size, messages, false);
    }

    public void test(int size, int messages, boolean corrupt)
    throws Exception {
        for (int i = 0; i < messages; ++i) {
            HashTestEvent hte = new HashTestEvent(size);
            if (hte.verify() == false) {
                System.out.println("Local event corrupted!");
            }
            if (corrupt) {
                hte.corrupt();
            }
            messageRouter.sendMessage(netDest, message);
        }
    }

    public static void main(String[] args) throws Exception {
        if (args.length < 3) {
            System.out.println(
                    "Usage: HashTestClient <server> <msg_size> <num_msgs>");
            System.out.println(
                    "Give a fourth argument to send corrupted messages");
            return;
        }

        String hostname = args[0];
        int size = Integer.parseInt(args[1]);
        int messages = Integer.parseInt(args[2]);
        boolean corrupt = (args.length == 4);

        NetworkDestination netDest = new NetworkDestination(
                hostname, BufferTestServer.PORT);
        HashTestClient htc = new HashTestClient(netDest);

        if (corrupt == false) {
            htc.test(size, messages);
        } else {
            System.out.println("Corrupt message test");
            htc.test(size, messages, true);
        }

        System.out.println("Test complete");
        htc.disconnect();
    }
}

@@ -1346,39 +1346,136 @@
 ort 
-galileo.net.ClientMessageRouter
+java.security.MessageDigest;%0Aimport java.util.Random;%0A%0Aimport galileo.net.ClientMessageRouter;%0Aimport galileo.net.GalileoMessage
 ;%0Aim
@@ -1782,16 +1782,88 @@
 netDest;
+%0A    private Random random = new Random();%0A    private MessageDigest md;
 %0A%0A    pu
@@ -2011,16 +2011,70 @@
 outer();
+%0A%0A        this.md = MessageDigest.getInstance(%22SHA1%22);
 %0A    %7D%0A%0A
@@ -2403,170 +2403,345 @@
     
-HashTestEvent hte = new HashTestEvent(size);%0A            if (hte.verify() == false) %7B%0A                System.out.println(%22Local event corrupted!%22);%0A            %7D%0A
+byte%5B%5D data = new byte%5Bsize%5D;%0A            random.nextBytes(data);%0A            byte%5B%5D checksum = md.digest(data);%0A%0A            byte%5B%5D payload = new byte%5Bdata.length + checksum.length%5D;%0A            System.arraycopy(checksum, 0, payload, 0, checksum.length);%0A            System.arraycopy(data, 0, payload, checksum.length, data.length);%0A%0A//
     
@@ -2759,24 +2759,26 @@
 (corrupt) %7B%0A
+//
             
@@ -2792,24 +2792,26 @@
 .corrupt();%0A
+//
             
@@ -2808,24 +2808,90 @@
           %7D%0A
+            GalileoMessage message = new GalileoMessage(payload);%0A
             

Move message generation code to client