Adding networking to the engine that when complet will be integrated whit the game

This commit is contained in:
Ziver Koc 2007-06-02 22:19:26 +00:00
parent 8f6dac310a
commit 84383f666c
17 changed files with 767 additions and 0 deletions

View file

@ -0,0 +1,10 @@
package ei.engine.network.response;
public class PrintRsp extends ResponseEvent{
@Override
protected void responseEvent(byte[] rsp) {
System.out.println(new String(rsp));
}
}

View file

@ -0,0 +1,35 @@
package ei.engine.network.response;
public abstract class ResponseEvent {
private byte[] rsp = null;
public synchronized boolean handleResponse(byte[] rsp) {
this.rsp = rsp;
notify();
return true;
}
public synchronized void waitForResponse() {
while(!gotResponse()) {
try {
this.wait();
} catch (InterruptedException e) {
}
}
responseEvent(rsp);
}
public void handleResponse(){
if(gotResponse()){
responseEvent(rsp);
}
}
public boolean gotResponse(){
return (rsp != null);
}
protected abstract void responseEvent(byte[] rsp);
}

View file

@ -0,0 +1,41 @@
package ei.engine.network.response;
import java.util.LinkedList;
import java.util.List;
public abstract class ResponseHandler implements Runnable{
private List<ResponseEvent> queue = new LinkedList<ResponseEvent>();
public ResponseHandler(){
}
public synchronized void addResponseEvent(ResponseEvent re){
queue.add(re);
notify();
}
public synchronized void removeResponseEvent(ResponseEvent re){
queue.remove(re);
}
public void run() {
while(true) {
try {
this.wait();
} catch (InterruptedException e) {}
update();
}
}
public synchronized void update(){
while(!queue.isEmpty()){
queue.get(0).handleResponse();
if(queue.get(0).gotResponse()){
queue.remove(0);
}
}
}
}