Troubleshooting guide

219
15: Creating push applications
private boolean _stop = false;
private StreamConnectionNotifier _notify;
public synchronized void stop() {
_stop = true;
try {
_notify.close(); // Close the connection so thread returns.
} catch (IOException e) {
System.err.println(e.toString());
} catch (NullPointerException e) {
// The notify object likely failed to open, due to an IOException.
}
}
public void run()
{
StreamConnection stream = null;
InputStream input = null;
MDSPushInputStream pushInputStream=null;
while (!_stop)
{
try
{
//synchronize here so that we don’t end up creating a
//connection that is never closed
synchronized(this)
{
// Open the connection once (or re-open after an IOException),
// so we don’t end up in a race condition, where a push is lost if
// it comes in before the connection is open again.
// we open the url with a parameter that indicates that we should always
// use MDS when attempting to connect.
_notify = (StreamConnectionNotifier)Connector.open(URL + “;deviceside=false”);
}
while (!_stop)
{
//NOTE: the following will block until data is received
stream = _notify.acceptAndOpen();
try {
input = stream.openInputStream();
pushInputStream= new
MDSPushInputStream((HttpServerConnection)stream, input);
//Extract the data from the input stream
DataBuffer db = new DataBuffer();
byte[] data = new byte[CHUNK_SIZE];
int chunk = 0;
while ( -1 != (chunk = input.read(data)) )
{
db.write(data, 0, chunk);
}
updateMessage(data);
//This method is called to accept the push
pushInputStream.accept();