1/*
2 * Copyright (c) 2011 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32package jme3test.network;
33
34import com.jme3.network.*;
35import com.jme3.network.serializing.Serializable;
36import com.jme3.network.serializing.Serializer;
37import java.io.IOException;
38
39public class TestThroughput implements MessageListener<MessageConnection> { //extends MessageAdapter {
40
41    private static long lastTime = -1;
42    private static long counter = 0;
43    private static long total = 0;
44    // Change this flag to test UDP instead of TCP
45    private static boolean testReliable = false;
46    private boolean isOnServer;
47
48    public TestThroughput(boolean isOnServer) {
49        this.isOnServer = isOnServer;
50    }
51
52    @Serializable
53    public static class TestMessage extends AbstractMessage {
54
55        public TestMessage() {
56            setReliable(testReliable);
57        }
58    }
59
60    @Override
61    public void messageReceived(MessageConnection source, Message msg) {
62
63        if (!isOnServer) {
64            // It's local to the client so we got it back
65            counter++;
66            total++;
67            long time = System.currentTimeMillis();
68//System.out.println( "total:" + total + "  counter:" + counter + "  lastTime:" + lastTime + "  time:" + time );
69            if (lastTime < 0) {
70                lastTime = time;
71            } else if (time - lastTime > 1000) {
72                long delta = time - lastTime;
73                double scale = delta / 1000.0;
74                double pps = counter / scale;
75                System.out.println("messages per second:" + pps + "  total messages:" + total);
76                counter = 0;
77                lastTime = time;
78            }
79        } else {
80            if (source == null) {
81                System.out.println("Received a message from a not fully connected source, msg:" + msg);
82            } else {
83//System.out.println( "sending:" + msg + " back to client:" + source );
84                // The 'reliable' flag is transient and the server doesn't
85                // (yet) reset this value for us.
86                ((com.jme3.network.Message) msg).setReliable(testReliable);
87                source.send(msg);
88            }
89        }
90    }
91
92    public static void main(String[] args) throws IOException, InterruptedException {
93
94        Serializer.registerClass(TestMessage.class);
95
96        // Use this to test the client/server name version check
97        //Server server = Network.createServer( "bad name", 42, 5110, 5110 );
98        Server server = Network.createServer(5110, 5110);
99        server.start();
100
101        Client client = Network.connectToServer("localhost", 5110);
102        client.start();
103
104        client.addMessageListener(new TestThroughput(false), TestMessage.class);
105        server.addMessageListener(new TestThroughput(true), TestMessage.class);
106
107        Thread.sleep(1);
108
109        TestMessage test = new TestMessage();
110//        for( int i = 0; i < 10; i++ ) {
111        while (true) {
112//System.out.println( "sending." );
113            client.send(test);
114        }
115
116        //Thread.sleep(5000);
117    }
118}
119