BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-20-2005, 12:13 PM   #1 (permalink)
New Member
 
Join Date: Oct 2005
Model: 7100t
Carrier: Rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default BackGround Listener Thread

Please Login to Remove!

Hi,

Post is a bit long, I will pay the person $50 CDN(by paypal) for your valuable time if you can help me implement this.

I am trying to implement a client/server application. The server makes a push to a port on the device and the custom application receives the data at this port and uses it.

THE PROBLEM: The application runs as desired if i do not make the Listener Thread a background thread. But when i make the listener thread run in the background it does not receive any messages. In the JDE i can see its state as "wait for notify" [as i use acceptandopen(StreamConnectionNotifier) which blocks until data is received on port], but it never reads the data. I am testing this in the simulator, is this a bug in the simulator? I am attaching the relevant code below, Please help. I have made the settings for the project to "Load on startup" and run as "System Module"


public class Listener extends Thread
{
//constants
private static final String URL = "http://:912";
//fields
private static Listener _listeningThread;
private StreamConnectionNotifier _notify;

public void run()
{
StreamConnection stream = null;
InputStream input = null;
try {
for(;;)
{
_notify = (StreamConnectionNotifier)Connector.open(URL);

//NOTE: this blocks until data is received
stream = _notify.acceptAndOpen();
input = stream.openInputStream();

//extract the data from the input stream
StringBuffer sb = new StringBuffer();
int datum = -1;
while ( -1 != (datum = input.read()) )
{
sb.append((char)datum);
}
input.close();
_notify.close();

//other CODE to use received data
}
} catch (IOException e){
//likely the stream was closed
System.err.println(e.toString());
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (Exception ex) {
}
try {
if (_notify != null) {
_notify.close();
}
} catch (Exception ex) {
}
}
}

//constructor
public static void main(String[] args)
{
_listeningThread = new Listener();
_listeningThread.start();

}
}

Thanks for you help!
Ash
Offline  
Old 11-20-2005, 12:59 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Oct 2004
Posts: 60
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code for your listener Thread is okay. The problem is that you never lauch the app into the EventThread.

Your listener class should be an inner class and you should have a main app that looks like this

public MyApp extends UIApplication {
public MyApp() {
_listeningThread = new Listener();
_listeningThread.start();
}

public static void main(String[] args)
{
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
}

This should get your application working properly.
Offline  
Old 11-20-2005, 04:09 PM   #3 (permalink)
New Member
 
Join Date: Oct 2005
Model: 7100t
Carrier: Rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by berryapps
The code for your listener Thread is okay. The problem is that you never lauch the app into the EventThread.

Your listener class should be an inner class and you should have a main app that looks like this
Hi,
I have tried doing that also but it does not seem to receive messages and the thread is in wait for notify state still. I see the push being received by MDS but the thread never finds it and is still blocked

The code i tried is :
Code:
public class Listener extends UiApplication
{
        //constants
        private static final String URL = "http://:912";
       //fields
        private ListeningThread _listeningThread;
        public static void main(String[] args)
        {
                Listener theApp = new Listener();
                theApp.enterEventDispatcher();
        }

        //inner classes
        private class ListeningThread extends Thread
        {
                private StreamConnectionNotifier _notify;

                public void run()
                {
                    StreamConnection stream = null;
                    InputStream input = null;
                    try {
                        for(;;)
                        {
                            _notify = (StreamConnectionNotifier)Connector.open(URL);

                            //NOTE: this blocks until data is received
                            stream = _notify.acceptAndOpen();
                            input = stream.openInputStream();
                            
                            //extract the data from the input stream
                            StringBuffer sb = new StringBuffer();
                            int datum = -1;
                            while ( -1 != (datum = input.read()) )
                            {
                            sb.append((char)datum);
                            }
                            input.close();
                            _notify.close();

                 //code goes here to save data 
                   }
                } catch (IOException e){
                //code goes here
                 } finally {
                    try {
                        if (stream != null) {
                        stream.close();
                        }
                    } catch (Exception ex) {
                    }
                    try {
                        if (_notify != null) {
                        _notify.close();
                        }
                    } catch (Exception ex) {
                    }
                }
               }
    }
        //constructor
    public Listener()
    {
                _listeningThread = new ListeningThread();
                _listeningThread.start();

        }
}
Any ideas??
Thanks,
Ash
Offline  
Old 11-22-2005, 08:22 AM   #4 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

not entirely sure but why do you use http:// without a (HttpConnection) Connector.open?
a socket connection should work and the example from the API is as follows:

// Create the server listening socket for port 1234
ServerSocketConnection scn = (ServerSocketConnection)
Connector.open("socket://:1234");
// Wait for a connection.
SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
Offline  
Old 11-22-2005, 09:56 AM   #5 (permalink)
New Member
 
Join Date: Oct 2005
Model: 7100t
Carrier: Rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by simon.hain
not entirely sure but why do you use http:// without a (HttpConnection) Connector.open?
a socket connection should work and the example from the API is as follows:

// Create the server listening socket for port 1234
ServerSocketConnection scn = (ServerSocketConnection)
Connector.open("socket://:1234");
// Wait for a connection.
SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
Hi Simon,

So you are saying that i can make a socket to listen to data on a port and it will do the job for me. Actually the above code works fine as long as it is not a background process. I will try this out and see if it works. Thanks for your help.

Ash
Offline  
Old 11-24-2005, 06:11 AM   #6 (permalink)
New Member
 
Join Date: Oct 2005
Model: no
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I am facing the same problem here. I have used a SocketConnection. My thread keeps running but cant seem to read the incoming stream...in fact i use a InputStreameader....the thread never gets into the in.ready() loop...i can see the MDS receiving the data though.
Offline  
Old 11-25-2005, 07:10 AM   #7 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll post some (working) code from one of our applications:
(declarations picked together from different places, hope i missed none)

Code:
private static final String URL = "http://:100"; //PORT 100 
private static final int CHUNK_SIZE = 256;
private ListeningThread _listeningThread;
private StreamConnectionNotifier _notify;
private boolean _stop = false;
private byte[] data;

StreamConnection stream = null;
            InputStream input = null;
            while (!_stop) {
                try {
                    synchronized(this) {
                        _notify = (StreamConnectionNotifier)Connector.open(URL + ";deviceside=false");
                    }
                    
                    while (!_stop) {
                        Application.getApplication().requestForeground();
                        stream = _notify.acceptAndOpen();
                        try {
                            input = stream.openInputStream();
                            DataBuffer db = new DataBuffer();
                            byte[] data = new byte[CHUNK_SIZE];
                            int chunk = 0;
                            while ( -1 != (chunk = input.read(data)) ) {
                                db.write(data, 0, chunk);
                            }
                            input.close();
                            stream.close();
                            data = db.getArray();
                            updateBitmap(data);
                        } catch (IOException e1) {
                            System.err.println(e1.toString());
                            if ( input != null ) {
                                try {
                                    input.close();
                                } catch (IOException e2) {
                                }
                            }
                            if ( stream != null ) {
                                try {
                                    stream.close();
                                } catch (IOException e2) {
                                }
                            }
                        }
                    }
                    
                    _notify.close();
                    _notify = null;   
                } catch (IOException ioe) {
                    System.err.println(ioe.toString());
                    if ( _notify != null ) {
                        try {
                            _notify.close();
                            _notify = null;
                        } catch ( IOException e ) {
                        }
                    }
                }
            }
hope that helps,
simon

Last edited by simon.hain : 11-25-2005 at 07:13 AM.
Offline  
Old 11-25-2005, 09:40 AM   #8 (permalink)
New Member
 
Join Date: Oct 2005
Model: 7100t
Carrier: Rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi simon,
Thanks. I have tried the above code, it was in the API. But like i said it does not work when the thread is running in the background. I guess i am going to sign my code and try running it on the device. Maybe it is just a simulator bug or limitation.
Thanks for your continued effort to help. I will post my results on the board.
Ash
Offline  
Old 12-09-2005, 10:01 AM   #9 (permalink)
New Member
 
Join Date: Oct 2005
Model: 7100t
Carrier: Rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem Solved !!!

The problem was that the auto start thread was being launched before the app has the event thread so it was running but not being registered due to which it was not listening at the port.
After countless hours and suggestions and help the simple solution is just make the thread sleep.

Right before you call the thread put the following line
Thread.sleep(1000);

the sleeping gives time for the event thread to be launched and then this thread is launched so it works fine. Thank you all for you help.

Ash
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.