|
Problem receiving data from BES -
05-08-2008, 12:32 PM
Hi!
I've tried many many many things to make this thing work and I'm out of ideas.
My application just freezes on the stream = mConnNotify.acceptAndOpen(); line.
It never ever receives data from the BES.
I first tried to run everything on my local computer.
The server send the data to the BES and receives code HTTP_OK...
In the MDS, I see the requests and it displays DELIVERED so I suppose the simulator did receive the data.
Plus, the data transfer icon appears at exactly the same time as the MDS sends data...
Then I tried to use a handheld with a real BES.
Nothing worked.
I just can't seem to be able to receive data from the BES.
The app just freezes on acceptAndOpen();
I know I'm not the only one to have had this issue and I tried almost every solution I could find and nothing works.
So I'm wondering if it could be environnement related?
Like, is there a certain number of ports from which a handheld can receive Data from the BES?
And I know this thing can work.
'Cause it once did (Wasn't me who first developped the app and the server. But he was able to make it work...)
so here is my code anyway....(of course, I had to remove parts of it because of privacy so there might be a couple of functions and variables missing but you get the idea anyway...)
The Application
MyApplication
class MyApplication extends net.rim.device.api.ui.UiApplication
{
public static void main(String[] args)
{
if(args != null && args.length > 0 && args[0].equals("gui"))
{
// Start the GUI
MyApplication instance = new MyApplication ();
instance.enterEventDispatcher();
}
else
{
mBackgroundThread = new BackgroundThread();
if(mBackgroundThread == null)
{
//impossible to create thread... so exit
System.exit(-1);
}
else
{
try
{
Thread.sleep(2000);
mBackgroundThread.start();
}
catch(Throwable ie)
{
}
}
//alternate entry point
Bitmap icon = Bitmap.getBitmapResource("icon1.png");
HomeScreen.setRolloverIcon(icon, 0);
}
}
MyApplication ()
{
//start main menu screen
mMainMenu = MainMenu.createMainMenuInstance();
mMainMenu.init();
pushScreen(mMainMenu);
}
public static MainMenu mMainMenu;
public static BackgroundThread mBackgroundThread;
BackgroundThread
class BackgroundThread extends Thread
{
BackgroundThread()
{
}
public void run()
{
StreamConnection stream = null;
InputStream input = null;
DataInputStream inputStream;
while(!mIsConnectionStopped)
{
try
{
synchronized(this)
{
try
{
mConnNotify = (StreamConnectionNotifier)Connector.open("http://:5700;deviceside=false",Connector.READ_WRITE);
}
catch(Throwable e)
{
e.printStackTrace();
Dialog.alert("Port problem : " + e.toString());
}
}
System.out.println("!!!!*** Just before the 2nd while");
System.out.println("mIsConnectionStopped = " + mIsConnectionStopped);
while(!mIsConnectionStopped)
{
// This method blocks until data is received
System.out.println("!!!!!!!!!!!!!!Will now try to acceptandconnect");
stream = mConnNotify.acceptAndOpen();
System.out.println("!!!!!!!!!!!!!!! RECEIVED SOMETHING!!!!!!");
try
{
input = stream.openInputStream();
// Extract the data from this input stream
DataBuffer db = new DataBuffer();
byte[] data = new byte[CHUNK_SIZE];
int chunk = 0;
while((chunk = input.read(data)) != -1)
{
db.write(data, 0, chunk);
}
input.close();
stream.close();
data = db.getArray();
}
catch (IOException e1)
{
// a problem occurred with the input stream
// however, the original StreamConnectionNotifier is still valid
System.err.println(e1.toString());
if ( input != null )
{
try
{
input.close();
}
catch (IOException e2)
{
System.out.println(e2.toString());
}
}
if ( stream != null )
{
try
{
stream.close();
}
catch (IOException e2)
{
}
}
}
}
mConnNotify.close();
mConnNotify = null;
}
catch(Throwable/*IOException*/ ioe)
{
// likely the stream was closed
System.err.println(ioe.toString());
if ( mConnNotify != null )
{
try
{
mConnNotify.close();
mConnNotify = null;
}
catch ( IOException e )
{
System.out.println(e.toString());
}
}
}
}
}
public synchronized void stop()
{
mIsConnectionStopped = true;
try
{
mConnNotify.close();
}
catch(IOException e)
{
System.err.println(e.toString());
}
catch(NullPointerException e)
{
System.err.println(e.toString());
}
}
public MainMenu mMainMenu;
boolean mIsConnectionStopped;
StreamConnectionNotifier mConnNotify;
final int CHUNK_SIZE = 256;
}
The Server
public class MyServer extends Thread
{
public MyServer ()
{
ShutdownHook shutdownHook = new ShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook) ;
mTimer = new Timer();
mTimerLong = new Timer();
mTimer.schedule(new PushTask(), 0, ALARM_TIME_INTERVAL*1000);
}
/**
* @param args
*/
public static void main(String[] args)
{
// Args passed to the application. PIN of all devices to call, BES host, BES port
args=new String[5];
args[0]="2100000a";
args[1]="127.0.0.1";
args[2]="8080";
args[3]="30";
args[4]="30";
int len = args.length;
if (len < 5)
{
System.out.println("Missing parameter");
System.exit(-1);
}
// create the array
HANDHELD_EMAIL = args[0].split(";");
for(int i=0;i<HANDHELD_EMAIL.length;i++)
{
System.out.println("PIN found : "+HANDHELD_EMAIL[i]);
}
/*HANDHELD_EMAIL = new String[len - NUMBER_OUTSIDE_OF_PARAMETER];
for(i = 0; i < len - NUMBER_OUTSIDE_OF_PARAMETER; i++)
{
HANDHELD_EMAIL[i] = args[i];
}*/
// BES_HOST
BES_HOST = args[1];
System.out.println("BES HOST="+BES_HOST);
// BES_PORT
try
{
BES_PORT = Integer.parseInt(args[2]);
System.out.println("BES PORT="+BES_PORT);
}
catch(NumberFormatException e)
{
System.out.print("Invalid BES_PORT (Not a number) : " + args[2]);
System.exit(-1);
}
// ALARM TIME
try
{
ALARM_TIME_INTERVAL = Integer.parseInt(args[3]);
System.out.println("Alarm Time Interval="+ALARM_TIME_INTERVAL);
}
catch(NumberFormatException e)
{
System.out.print("Invalid alarm time (Not a number) : " + args[3]);
System.exit(-1);
}
// DISPLAY TIME
try
{
DISPLAY_TIME_INTERVAL = Integer.parseInt(args[4]);
System.out.println("Display time interval="+DISPLAY_TIME_INTERVAL);
}
catch(NumberFormatException e)
{
System.out.print("Invalid display time (Not a number) : " + args[4]);
System.exit(-1);
}
MyServer TheThread= new MyServer ();
TheThread.start();
}
private static URL getPushURL(String aHandheldEmail)
{
URL pushURL = null;
try
{
if(aHandheldEmail == null || aHandheldEmail.length() == 0)
{
//aHandheldEmail = HANDHELD_EMAIL;
System.out.println("No handheld PIN");
return pushURL;
}
pushURL = new URL("http", BES_HOST, BES_PORT, "/push?DESTINATION=" + aHandheldEmail +
"&PORT=" + HANDHELD_PORT + "&REQUESTURI=/");
//System.out.println("url: " + pushURL);
}
catch(MalformedURLException e)
{
System.err.println(e);
}
return pushURL;
}
public static void postData(byte[] aData)
{
int i = 0;
try
{
for(i = 0; i < HANDHELD_EMAIL.length; i++)
{
URL url = getPushURL(HANDHELD_EMAIL[i]);
System.out.println("Sending to " + HANDHELD_EMAIL[i]);
System.out.println("URL="+url.toString());
mHttpConnection = (HttpURLConnection)url.openConnection();
mHttpConnection.setDoInput(true); // For receiving the confirmation
mHttpConnection.setDoOutput(true); // For sending data
mHttpConnection.setRequestMethod("POST"); // Post the data to the BES
OutputStream outStream = mHttpConnection.getOutputStream();
outStream.write(aData);
outStream.close();
int response = mHttpConnection.getResponseCode();
System.out.println("response code : " + response);
mHttpConnection.disconnect();
}
}
catch(IOException e)
{
System.err.println(e);
}
}
class ShutdownHook extends Thread
{
public void run()
{
System.out.println("Shutting down");
try
{
if(mServerSocket != null)
{
mServerSocket.close();
}
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
class PushTask extends TimerTask
{
public void run()
{
byte[] data = formatData(23,23);
postData(data);
}
}
final static String HANDHELD_PORT = "5700";
static String[] HANDHELD_EMAIL;
static String BES_HOST = "127.0.0.1";
static int BES_PORT = 8080;
// Connection code
final byte CONNECTION_CODE = 1;
final byte DISCONNECTION_CODE = 2;
// Push data time interval
static int ALARM_TIME_INTERVAL;// = 30;
static int DISPLAY_TIME_INTERVAL;// = 30;
Timer mTimer; // Timer for scheduling task
Timer mTimerLong; // Timer long for scheduling task
static ByteArrayOutputStream mTempOutByteStream;
static DataOutputStream mTempDataOutputStream;
static ByteArrayOutputStream mHumidityOutByteStream;
static DataOutputStream mHumidityDataOutputStream
static HttpURLConnection mHttpConnection;
static Socket mSocket;
static ServerSocket mServerSocket;
}
|