/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xunome.game.multiplayerg;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.ServerSocketConnection;
import xunome.xUnoME;
/**
*
* @author Sem iNick
*/
public class SocketConnectionServer extends ConnectionServer implements Runnable {
private String sessionName;
public static final String SERVER_SESSION =
//#ifdef DEBUG
"http://127.0.0.1/unome/";
//#else
//# "http://xunome.awardspace.info/";
//#endif
//private String ip;
public SocketConnectionServer(String sessionName, String playName, xUnoME mid) {
super(mid, playName);
this.sessionName = sessionName;
new Thread(this).start();
}
public void run() {
HttpConnection setHostIpCon = null;
int resultType = 0;
try {
ServerSocketConnection server = (ServerSocketConnection) Connector.open("socket://");
//ip = server.getLocalAddress();
String port = String.valueOf(server.getLocalPort());
setHostIpCon = (HttpConnection) Connector.open(SERVER_SESSION
+ "/multiplayer/manageSession.php"
+ "?session=" + sessionName
+ /*"&ip=" + ip + */"&port="
+ port + "&type=a");
InputStream result = setHostIpCon.openInputStream();
resultType = result.read();
if (resultType == 0) {
setLocalServer(server);
setManager(new MultiPlayerManager(this, getMidlet(), getName()));
startListening();
} else {
throw new Exception("Result error.");
}
} catch (Exception io) {
//#ifdef DEBUG
io.printStackTrace();
//#endif
switch (resultType) {
case 2:
showError("Session exists.");
break;
case 3:
showError("There was a connection error with server.");
break;
default:
showError("Server couldn't be initialized.");
}
} finally {
if (setHostIpCon != null) {
try {
setHostIpCon.close();
} catch (Exception e) {}
}
}
}
}