The online racing simulator
#1 - amp88
InSim noob requesting advice on Java/JInSim
Hi, all. As the topic says, I'm very much an InSim noob. I've tried looking at the guides/examples/readme files for a few things, but haven't found many on Java/JInSim.

I've had an idea for writing my own race tracking/timing tool and I've been putting off writing it for a long time. So, I decided to get into it properly yesterday and I've made some progress. I've attached how it's looking in its current state (don't laugh! :P )

The biggest problem I have for the moment is that the player order isn't correct. So, I'm trying to use the Node and Lap information to order the players in correct race order. However, when I try to do this I get a BufferUnderflowException:

"02-Mar-2008 21:18:34 net.sf.jinsim.response.ResponseFactory getPacketData
SEVERE: java.nio.BufferUnderflowException"

I'm working from a modified version of one of the JInSim examples, the code for the insim part of the app is below:


try {
client = new SimpleClient();

client.addListener(this);

client.connect(new UDPChannel(hostname, port), adminPassword, "console");

client.send(new SmallRequest(Small.START_SENDING_POSITION));

while(!stopped) {
// Retrieve node packets (for positioning)
client.send(new TinyRequest(Tiny.NODE_LAP));

Thread.sleep(Constants.WAITTIMEINSIM);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}

I've put the request for the information in a loop that is delayed 5 seconds, so I'm trying to retrieve player's position information periodically then re-order the player list.

I'd appreciate any help/advice or pointing to relevant Java examples on this, thanks.

I'm using JInSim 0.5rc2.
Attached images
lfstimes.PNG
Quote from amp88 :
The biggest problem I have for the moment is that the player order isn't correct. So, I'm trying to use the Node and Lap information to order the players in correct race order. However, when I try to do this I get a BufferUnderflowException:

"02-Mar-2008 21:18:34 net.sf.jinsim.response.ResponseFactory getPacketData
SEVERE: java.nio.BufferUnderflowException"

Hmm, could be a bug in JInSim. Do you have a stack trace for the BufferUnderflowException?
I'll try to write an example program to verify if this is a JInSim bug (hopefully this week).

I have fixed one issue in rc3 or rc4 that could also fix this BufferUnderflowException, so you could also try the latest version:
http://www.lfsforum.net/showthread.php?p=708608#post708608
#3 - amp88
Ah, I hadn't updated JInSim to the new version. Just tried it now but no joy, still getting the same problem.

I don't have a stack trace, and I can't see how to print one, it appears to be being thrown from the getPacketData method in ResponseFactory but I don't know for certain.

Thanks for the input and I should also thank you for your continuing development of JInSim! You've helped to make it relatively easy for a newcomer to InSim to come in and feel comfortable.
I did an example and the NodeLap works fine for me:


package net.sf.jinsim.examples.nodelap;

import java.io.IOException;

import net.sf.jinsim.Channel;
import net.sf.jinsim.SimpleClient;
import net.sf.jinsim.TCPChannel;
import net.sf.jinsim.Tiny;
import net.sf.jinsim.request.MessageExtendedRequest;
import net.sf.jinsim.request.TinyRequest;
import net.sf.jinsim.response.InSimListener;
import net.sf.jinsim.response.InSimResponse;
import net.sf.jinsim.response.NodeLapInfoResponse;

public class NodeLapExample implements InSimListener {
private SimpleClient client;
private String hostname;
private int port;
private String adminPassword;

public NodeLapExample(String[] args) {
hostname = args[0];
port = Integer.parseInt(args[1]);

if (args.length > 2) {
adminPassword = args[2];
}
}

public void run() {

try {
client = new SimpleClient();

Channel channel = new TCPChannel(hostname, port);
client.addListener(this);
client.connect(channel, adminPassword, "NodeLapExample");

MessageExtendedRequest msgRequest = new MessageExtendedRequest();
msgRequest.setMessage("NodeLapExample");
client.send(msgRequest);

int i = 0;
while(i<10) {
client.send(new TinyRequest(Tiny.NODE_LAP));
Thread.sleep(500);
i++;
}

} catch (Exception e) {
e.printStackTrace();

} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void packetReceived(InSimResponse response) {
if (response instanceof NodeLapInfoResponse) {
NodeLapInfoResponse nodeLap = (NodeLapInfoResponse)response;
System.out.println(nodeLap);
}
}

static public void main(String[] args) {
if (args.length >= 2) {
NodeLapExample testConnect = new NodeLapExample(args);
testConnect.run();
} else {
System.out.println("usage: net.sf.insim.nodelap.NodeLapExample <hostname> <port> <admin password>");
}
}

}

I took a look at NodeLapInfoResponse, and it looks like we were trying to read in a NodeLaps for the max number of players every time. I changed it so we are now just reading in a NodeLap for each of the existing players. I think this may fix the problem. If you don't want to check out the latest source on SourceForge, just go to NodeLapInfoResponse, and in the construct() method, change "MAX_PLAYERS" to "numberPlayers".

Let us know if this works! Thanks.
#6 - amp88
Quote from rheiser :I took a look at NodeLapInfoResponse, and it looks like we were trying to read in a NodeLaps for the max number of players every time. I changed it so we are now just reading in a NodeLap for each of the existing players. I think this may fix the problem. If you don't want to check out the latest source on SourceForge, just go to NodeLapInfoResponse, and in the construct() method, change "MAX_PLAYERS" to "numberPlayers".

Let us know if this works! Thanks.

This seems to have fixed it

Thanks very much for your help chaps and keep up the good work!

FGED GREDG RDFGDR GSFDG