Home | History | Annotate | Download | only in network
      1 /*
      2  * Copyright (c) 2009-2010 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  */
     32 
     33 package jme3test.network;
     34 
     35 import com.jme3.network.*;
     36 import com.jme3.network.serializing.Serializable;
     37 import com.jme3.network.serializing.Serializer;
     38 import java.io.IOException;
     39 
     40 public class TestLatency {
     41 
     42     private static long startTime;
     43     private static Client client;
     44     private static MovingAverage average = new MovingAverage(100);
     45 
     46     static {
     47         startTime = System.currentTimeMillis();
     48     }
     49 
     50     private static long getTime(){
     51         return System.currentTimeMillis() - startTime;
     52     }
     53 
     54     @Serializable
     55     public static class TimestampMessage extends AbstractMessage {
     56 
     57         long timeSent     = 0;
     58         long timeReceived = 0;
     59 
     60         public TimestampMessage(){
     61             setReliable(false);
     62         }
     63 
     64         public TimestampMessage(long timeSent, long timeReceived){
     65             setReliable(false);
     66             this.timeSent = timeSent;
     67             this.timeReceived = timeReceived;
     68         }
     69 
     70     }
     71 
     72     public static void main(String[] args) throws IOException, InterruptedException{
     73         Serializer.registerClass(TimestampMessage.class);
     74 
     75         Server server = Network.createServer(5110);
     76         server.start();
     77 
     78         client = Network.connectToServer("localhost", 5110);
     79         client.start();
     80 
     81         client.addMessageListener(new MessageListener<Client>(){
     82             public void messageReceived(Client source, Message m) {
     83                 TimestampMessage timeMsg = (TimestampMessage) m;
     84 
     85                 long curTime = getTime();
     86                 //System.out.println("Time sent: " + timeMsg.timeSent);
     87                 //System.out.println("Time received by server: " + timeMsg.timeReceived);
     88                 //System.out.println("Time recieved by client: " + curTime);
     89 
     90                 long latency = (curTime - timeMsg.timeSent);
     91                 System.out.println("Latency: " + (latency) + " ms");
     92                 //long timeOffset = ((timeMsg.timeSent + curTime) / 2) - timeMsg.timeReceived;
     93                 //System.out.println("Approximate timeoffset: "+ (timeOffset) + " ms");
     94 
     95                 average.add(latency);
     96                 System.out.println("Average latency: " + average.getAverage());
     97 
     98                 long latencyOffset = latency - average.getAverage();
     99                 System.out.println("Latency offset: " + latencyOffset);
    100 
    101                 client.send(new TimestampMessage(getTime(), 0));
    102             }
    103         }, TimestampMessage.class);
    104 
    105         server.addMessageListener(new MessageListener<HostedConnection>(){
    106             public void messageReceived(HostedConnection source, Message m) {
    107                 TimestampMessage timeMsg = (TimestampMessage) m;
    108                 TimestampMessage outMsg = new TimestampMessage(timeMsg.timeSent, getTime());
    109                 source.send(outMsg);
    110             }
    111         }, TimestampMessage.class);
    112 
    113         Thread.sleep(1);
    114 
    115         client.send(new TimestampMessage(getTime(), 0));
    116 
    117         Object obj = new Object();
    118         synchronized(obj){
    119             obj.wait();
    120         }
    121     }
    122 
    123 }
    124