package edu.ups.gamedev.net;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNClient;
import com.captiveimagination.jgn.event.MessageListener;
import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.synchronization.SynchronizationManager;
import com.captiveimagination.jmenet.JMEGraphicalController;
public class Client extends NetworkCommon {
JGNClient client; //this is our primary server and the addresses it should use
/**
* Constructor that takes a root node and uses default values for the server
* addresses. the server on
*
* @param root root <code>Node</code> of the scene graph
* @throws IOException
*/
public Client() throws UnknownHostException, IOException {
super();
try {
createClient(); //initialize everything
} catch (UnknownHostException e) {
throw new UnknownHostException("Could not create host addresses");
} catch (IOException e) {
throw new IOException("Could not create JGN client");
}
}
/**
* Constructor that takes a root node of and an <code>InetAddress</code>
* for the server it should connect to.
*
* @param root root <code>Node</code> of the scene graph
* @param address <code>InetAddress</code> this <code>Client</code>
* should connect to
* @throws IOException
*/
public Client(InetAddress address) throws IOException {
super(address);
System.out.println("Creating client for address " + serverFast);
try {
createClient(); //initialize everything
} catch (UnknownHostException e) {
throw new UnknownHostException("Could not create host addresses");
} catch (IOException e) {
throw new IOException("Could not create JGN client");
}
}
/**
* Creates and initializes all of the objects necessary for the client.
*
* @throws IOException
*/
private void createClient() throws UnknownHostException, IOException {
InetSocketAddress clientReliable, clientFast;
try {
clientReliable = new InetSocketAddress(InetAddress.getLocalHost(), 0); //let Java choose a port for us
clientFast = new InetSocketAddress(InetAddress.getLocalHost(), 0); //let Java choose a port for us
} catch (UnknownHostException e) {
System.err.println("Could not find host:");
e.printStackTrace();
return;
}
//create the client and start it in its own thread
try {
client = new JGNClient(clientReliable, clientFast); //create the networking client
} catch (IOException e) {
System.err.println("Could not create JGN client:");
e.printStackTrace();
return;
}
Thread tmp = JGN.createThread(client); //create a new thread for the client and start it
childThreads.add(tmp);
tmp.start();
controller = new JMEGraphicalController(); //in charge of generating and applying sync messages
//create the sync manager, register ourselves with it, and start its thread
syncManager = new SynchronizationManager(client, controller);
syncManager.addSyncObjectManager(this);
tmp = JGN.createThread(syncManager);
childThreads.add(tmp);
tmp.start();
}
/**
* Connects to the server. The server was previously specified when this <code>Client
* </code> was first created.
*
* @return <code>true</code> if successfully connected, <code>false</code> otherwise
*/
public boolean connectToServer() {
//connect to the server
System.out.println("Connecting...");
try {
client.connectAndWait(serverReliable, serverFast, 5000);
} catch (IOException e) {
//Show an error message that we couldn't connect and print detailed info to standard error
JOptionPane.showMessageDialog(null, "Could not connect to server.", "Connection Error", JOptionPane.ERROR_MESSAGE);
System.err.println("Error while connecting to server:");
e.printStackTrace();
return false;
} catch (InterruptedException e) {
//Show an error message that we couldn't connect and print detailed info to standard error
JOptionPane.showMessageDialog(null, "Could not connect to server.", "Connection Error", JOptionPane.ERROR_MESSAGE);
System.err.println("Error while connecting to server:");
e.printStackTrace();
return false;
}
System.out.println("Connected!");
//attach a MessageListener so that we can get more detailed information about what the server is doing
client.getServerConnection().getReliableClient().addMessageListener(new MessageListener() {
public void messageCertified(Message message) {
System.out.println("Message Certified: " + message);
}
public void messageFailed(Message message) {
System.out.println("Message Failed: " + message);
}
public void messageReceived(Message message) {
System.out.println("Message Received: " + message);
}
public void messageSent(Message message) {
System.out.println("Message Sent: " + message);
}
});
return true;
}
public void kill() {
super.kill();
try {
client.disconnect();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public JGNClient getJGNClient() {
return client;
}
}