(三)
Fruite-machine严格上来说,只是一个排列高低分的游戏,并不是真正意义上的网络游戏,但是它也实现了网络游戏的一些简单的功能。
Fruite-machine虽然用了HTTP协议,却并不轮循服务器,而是通过callback的方式,向服务器端发送一个报文后,再接受一个报文处理。这样在真正的网络游戏中就容易产生问题,因为在真正的网络游戏中,服务器可能会主动的给一个玩家发送报文。而在fruite-machine中,服务器是无法给玩家主动的发送报文的,它只是在用户发送报文给服务器端时,再回调函数处理response,这样一应一答的报文传输方式。
严格意义上来说,在真正的网络游戏中采用HTTP协议只能采用轮循服务器的方式来解决服务器主动发送报文给某个客户端的问题。就是所谓的心跳报文。
下面我们来看看实现callback功能的程序:
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
/**
* This class accepts and queues POST requests for a particular URL, and
* services them in first-in-first-out order. Using the queue allows it
* to be thread-safe without forcing its clients ever to block.
*/
public class HttpPoster
implements Runnable
{
private String url;
private volatile boolean aborting = false;
private Vector requestQueue = new Vector();
private Vector listenerQueue = new Vector();
public HttpPoster(String url)
{
this.url = url;
Thread thread = new Thread(this);
thread.start();
}
public synchronized void sendRequest(String request,
HttpPosterListener listener)
throws IOException
{
requestQueue.addElement(request);
listenerQueue.addElement(listener);
notify(); // wake up sending thread
}
public void run()
{
running:
while (!aborting)
{
String request;
HttpPosterListener listener;
synchronized (this)
{
while (requestQueue.size() == 0)
{
try
{
wait(); // releases lock
}
catch (InterruptedException e)
{
}
if (aborting)
break running;
}
request = (String)(requestQueue.elementAt(0));
listener = (HttpPosterListener)(listenerQueue.elementAt(0));
requestQueue.removeElementAt(0);
listenerQueue.removeElementAt(0);
}
// sendRequest must have notified us
doSend(request, listener);
}
}
private void doSend(String request,
HttpPosterListener listener)
{
HttpConnection conn = null;
InputStream in = null;
OutputStream out = null;
String responseStr = null;
String errorStr = null;
boolean wasError = false;
try
{
conn = (HttpConnection)Connector.open(url);
// Set the request method and headers
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Length",Integer.toString(request.length()));
// Getting the output stream may flush the headers
out = conn.openOutputStream();
int requestLength = request.length();
for (int i = 0; i < requestLength; ++i)
{
out.write(request.charAt(i));
}
// Opening the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
in = conn.openInputStream();
// Get the length and process the data
StringBuffer responseBuf;
long length = conn.getLength();
if (length > 0)
{
responseBuf = new StringBuffer((int)length);
}
else
{
responseBuf = new StringBuffer(); // default length
}
int ch;
while ((ch = in.read()) != -1)
{
responseBuf.append((char)ch);
}
responseStr = responseBuf.toString();
// support URL rewriting for session handling
String rewrittenUrl = conn.getHeaderField("X-RewrittenURL");
if (rewrittenUrl != null)
{
url = rewrittenUrl; // use this new one in future
}
}
catch (IOException e)
{
wasError = true;
errorStr = e.getMessage();
}
catch (SecurityException e)
{
wasError = true;
errorStr = e.getMessage();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
}
}
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (IOException e)
listener.receiveHttpResponse(responseStr);
{
}
}
}
if (wasError)
{
listener.handleHttpError(errorStr);
}
else
{
listener.receiveHttpResponse(responseStr);
}
}
// This is just for tidying up - the instance is useless after it has
// been called
public void abort()
{
aborting = true;
synchronized (this)
{
notify(); // wake up our posting thread and kill it
}
}
}
从HttpPoster类中我们可以看出来:HttpPoster类采用单独的一个线程,只要调用HttpPoster类的sendRequest(String request,HttpPosterListener listener)方法,线程就会运行,然后调用doSend(String request, HttpPosterListener listener)方法来把request传送到服务器端。
注意listener.receiveHttpResponse(responseStr);这句程序回调HttpPosterListener的receiveHttpResponse(String response)方法,这里也运用了多态性,listener会根据相应实现的类调用相应类的receiveHttpResponse(String reponse)方法来处理服务器返回的报文。