Package

Source Code of UdpSocketManagerTest

import java.net.Inet4Address;
import junit.framework.TestCase;
import dijjer.io.comm.DMT;
import dijjer.io.comm.Message;
import dijjer.io.comm.MessageFilter;
import dijjer.io.comm.MessageType;
import dijjer.io.comm.Peer;
import dijjer.io.comm.UdpSocketManager;
import dijjer.util.Misc;

public class UdpSocketManagerTest extends TestCase {

    public static final String VERSION = "$Id: UdpSocketManagerTest.java,v 1.18 2005/04/12 13:04:15 sanity Exp $";

    private int _pingcount = 0;
    private int _pongcount = 0;
    private Object lock = new Object();

    public void testManager() throws Exception {

        final Peer source = new Peer(Inet4Address.getLocalHost(), (short) 5555);
        final Peer dest = new Peer(Inet4Address.getLocalHost(), (short) 5556);
        final MessageType ping = DMT.ping;
        final MessageType pong = DMT.pong;
        final UdpSocketManager sourceUSM = new UdpSocketManager((short) 5555);
        final UdpSocketManager destUSM = new UdpSocketManager((short) 5556);
        ping.addField("id", Integer.class);
        pong.addField("id", Integer.class);
        // Sender
        Misc.startThread(new Runnable() {
            public void run() {
                int id = 0;
                for(int i=0;i<15;i++) {
                    Message p = new Message(ping);
                    p.set("id", id++);
                    p.set(DMT.EXTERNAL_ADDRESS, dest);
                    System.out.print("Sender: Ping sent... ");
                    sourceUSM.send(dest, p);
                    Message r = sourceUSM.waitFor(MessageFilter.create(10000, pong).setField("id",
                        new Integer(id - 1)));
                    if (r != null) {
                        _pongcount++;
                        synchronized (lock) {
                            lock.notifyAll();
                        }
                        System.out.println("Sender: Pong received.");
                    } else {
                        System.out.println("Timed out");
                    }
                }
            }
        });
        // Receiver
        for (int i = 0; i < 15; i++) {
            Message p = destUSM.waitFor(MessageFilter.create(10000, ping));
            if (p != null) {
                _pingcount++;
                System.err.println("Receiver: Ping " + p.getInt("id") + " received.  Sending pong");
                Message po = new Message(pong);
                po.set("id", p.getInt("id"));
                po.set(DMT.EXTERNAL_ADDRESS, source);
                destUSM.send(source, po);
            }
        }
        assertEquals(15, _pingcount);
        synchronized(lock) {
            lock.wait(2000);
        }
        assertEquals(15, _pongcount);
    }

}
TOP

Related Classes of UdpSocketManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.