Captive Imagination
September 06, 2010, 09:55:27 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: jSeamless 1.0 Beta 7 is Available http://www.jseamless.org
 
   Home   Help Search Calendar Login Register  
Pages: [1] 2  All   Go Down
  Print  
Author Topic: Determining the latency (using Ping)  (Read 3511 times)
0 Members and 1 Guest are viewing this topic.
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« on: April 12, 2008, 09:48:56 AM »

Greetings everyone.

I'm trying to implement an ingame display of player latencies.

Now, there is a class Ping and a PingTest example that uses MessageClient. I'm having trouble looking through the JGN-implementation here, so I hope you can help me out. I hope I haven't overlooked similar threads in this forums...

But back to the topic: I built my connection between server and clinet according to the FlagRushTest-example in the jme-networking project. On the client side I habe a JGNClient thread, on the server side a JGNServer thread. How do I get the MessageClients that are needed to create remote Ping objects? Can I somehow use the JGNServer and JGNClient connection to get one or do I have to create more threads?

And some other questions directly concerning ping calculations:
Is it appropriate for the server to ping the client and for the client to ping the server? Or does the server ping everybody and propagate the values to all clients?
Does the method in TestPing determine the real ping or rather a round-trip-time?
« Last Edit: April 12, 2008, 10:04:56 AM by not!an!exit » Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #1 on: April 12, 2008, 10:26:30 AM »

JGNServer and JGNClient contain MessageClients and MessageServers.  However, it would be trivial to write a ping test another way as well.  The current ping test just uses shared objects.  Although really easy and simple, it probably isn't practical if dealing with several servers (like a server list).  If you created a Ping message and Pong message that simply have an associated "long" for the time it was sent.  You could add a listener on all systems for a Ping message and immediately respond with a Pong message and apply the "long" from Ping to it.  Upon receipt of a Pong message you could determine the Ping time by System.currentTimeInMillis() (or you could use System.nanoTime() for better accuracy) - pong.getTimeSent() and it would give you the delay.  Does that make sense?

Either way is perfectly acceptable when you're already connected to a server though.
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #2 on: April 12, 2008, 02:14:46 PM »

Does that make sense?
Yeah, it does.
Just one thing about the MessageClient and MessageServer thing.

Suppose I am the client. JGNClient and JGNServer both return MessageServers. What about the MessageClients?
I have a getReliableServer().gerMessageClient(SocketAddress) method. Is this the way to go? Do I just insert the SocketAddress I use for the regular connection?
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #3 on: April 15, 2008, 01:46:12 PM »

On the client you can call client.getServerConnection().getFastClient() and/or client.getServerConnection().getReliableClient()
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #4 on: April 17, 2008, 09:03:59 AM »

Okay, I've been fooling around with messages and pings a bit and after reading this thread about TimeSynchronizationMessage I've tried to do the  follwing:

We have a two player game where one player is the server at the same time. Another player connects and synchronizes his time:
Code:
client.connectAndWait(serverReliable, serverFast, 5000);
client.getServerConnection().getFastClient().synchronizeTimeAndWait(5000);

I have implemented a simle ping system where a timestamped ping (with System.nanoTime()) is sent to another peer, who basically sends it back. The ping im milliseconds is then determined by
Code:
(System.nanoTime() - pong.getTime()) / MILLI2NANO / 2;
 \_____________________________/       \_________/ \_/
  round trip time (rtt) in nanos        to millis   to ping

Now, what I do is (after adding "implements TimestampedMessage" to Pong):
Code:
public void messageReceived(Pong message) {
LOG.debug("PONG: " + (System.currentTimeMillis() - message.getTimestamp()));
}
That should - according to my understanding of the use of time synchronization - give me approximately the same result: the ping in milliseconds.
But the difference between my ping calculations and the TimestampedMessage-method ranges between 30 and 100 milliseconds. Unfortunately I am testing running server and client on one machine. My local ping is usually about 3 ms (which appears to be realistic). The TimestampedMessage-method results in local pings of 35 to about 100 ms.

Am I doing something generally wrong? Or any ideas what may be causing this effect?
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #5 on: April 17, 2008, 10:14:16 AM »

I just added a new utility to JGN to help better handle this kind of thing.

Now, because of the asynchronous nature, the ping times may be further off than really is the case, but it at least gives a pretty fair and easy way to determine ping times.

Now you only have to call Ping.pingAndWait(messageClient, TimeUnit.MILLISECONDS) and you'll get a long back representing the amount of time in milliseconds it took to respond.  That should make pinging a little faster and easier.
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #6 on: April 18, 2008, 08:58:26 AM »

Does pingAndWait block the application while waiting for reply? I'm not sure that does make much sense.
I used to ping "in the background" to always have a current ping handy while the game is running.

And another thing is that I'd really love using synchronized time beween server and client to be able to calculate the real message delay from it's timestamp. Is this properly working? The thread about it that I linked earlier tells no results.

I was surprised to get that different results from my own ping calculation and the synchronized timestamps, and I did not quite got any wiser till now...
« Last Edit: April 18, 2008, 03:01:59 PM by not!an!exit » Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #7 on: April 18, 2008, 03:13:19 PM »

pingAndWait will block the current thread until it returns, yes.  However, there is also just a ping method that returns a Future<Long> as well.

TimeSynchronization should work just fine.  Let me know if you run into any problems with it.

The different results may be due to the asynchronous nature of JGN. Depending how your update threads work it can cause lag moving from queue to queue before it gets to the final resting place. This is also problematic in a game as CPU hungry threads can cause delays as well.
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #8 on: April 18, 2008, 04:56:21 PM »

Just for the record: If I synchronizeAndWait after I connected and then receive messages that implement the TimestampedMessage interface, (System.currentTimeInMillis() - message.getTimeStamp()) sould be the ping. Right?
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #9 on: April 18, 2008, 09:26:15 PM »

No, since that represents the time the message was enqueued from the other machine and a ping is a full cycle ping/pong.
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #10 on: April 19, 2008, 04:41:52 AM »

So, are you saying that it should give me half the time of a full-cycle ping/pong?

I'm sorry, I don't get it. If I send custom ping/pong messages, I calculate a (one way) delay of about 5 ms. Making my SynchronizeMessages implement TimestampedMessage and calculating the (one way) delay by System.currentTimeMillis() - message.getTimestamp() results in values beyond 25 ms.
« Last Edit: April 19, 2008, 04:52:28 AM by not!an!exit » Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #11 on: April 19, 2008, 08:22:19 AM »

Like I said previously, the way that JGN does things asynchronously can cause additional delays if other communication is happening.  In addition, you're using System.currentTimeInMillis() which does not provide good granularity on most operating systems.  This is why I typically use System.nanoTime() these days.

http://java.sun.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis()

In addition, the timestamp may be off by by several milliseconds.  It was not intended for such precision, it was more designed for scenarios when you want to see if a message is more than a few seconds old so it's not really surprising that it doesn't fit the bill for determining ping time.  I also fear it's something that cannot easily be resolved since you cannot use nanoTime to determine time offset.
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #12 on: April 19, 2008, 03:04:02 PM »

I still have the impression that something's fishy here... But may be it is my understanding. Help me out.

I have read through the synchronizeTimeAndWait() implementation and I think it does the following:


Now note that on both client and server timeConversion is set to the round trip time (rtt) of the message. It appears to me that it has nothing to do with time synchronization but rather a rtt calculation for both parties, a ping.
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #13 on: April 19, 2008, 09:45:44 PM »

I'll leave this open and come back to it later, I've been driving all day and my brain is too tired to look at diagrams.  Tongue
Logged
not!an!exit
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #14 on: April 20, 2008, 11:28:28 AM »

Okay, take your time. I'll check back later...
Logged
Pages: [1] 2  All   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.287 seconds with 22 queries.