aspose file tools*
The moose likes Java Micro Edition and the fly likes When to open MessageConnection (Push Registry)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "When to open MessageConnection (Push Registry)?" Watch "When to open MessageConnection (Push Registry)?" New topic
Author

When to open MessageConnection (Push Registry)?

chetan dhewal
Ranch Hand

Joined: Nov 07, 2000
Posts: 61
Hi
My application uses Push Registry for MMS. The Midlet gets invoked perfectly.
But when i try to receive() the message, it blocks(i have made seperate class for receiving).

My question is, do we have to open the MessageConnection before any incoming message(via Push Registry). But its not possible since midlet is in dormant stage. so how are we going to receive the messagepart.

------------------------------------
class ECGMIDlet
{
public void startApp()
{
if(push registry == true)
{
new MMSReceiver().start();
// some more code follows
}

}
}

class MMSReceiver
{
public void run()
{
//Is it OK to open MessageConnection after push registry has invoked the
//the midlet and the message is waiting to be fetched...
//But MessageConnection will in any case be opened after push registry

Open MessageConnection("mms://:ECGMIDletAppID")

received_msg= (MultipartMessage) msg_con.receive();
//the code gets stuck after this
//some more code after this

}
}
-----------------------------------------------------------

I am able to receive the message , if the connection is already open , means midlet is already running. but i want it to happen via Push Registry.

Thnx


Chetan Dhewal<br />SCMAD,SCJP 1.2
Eduardo Marques
Ranch Hand

Joined: Feb 19, 2005
Posts: 231
When the MIDlet gets invoked by the Push Registry , you must open the connection.
That is the correct way to interface with the Push Registry (with any kind of Connection)

After that, you can either call receive() or set up a message listener to take care of the I/O
asynchronoysly.

Read here - http://developers.sun.com/techtopics/mobility/midp/articles/pushreg/

Note that at MIDlet start up (ie in startApp()) you can check if it was the PR which started the MIDlet and which connection(s) triggered the start-up using PushRegistry.listConnections(true).

The method will return an array of all connections with push data available, which means that if at least one is returned, it was the PR that invoked the MIDlet.


Eduardo Marques
Get ready for your SCMAD certification at J2MECertificate.com - See what all the buzz is about ...
chetan dhewal
Ranch Hand

Joined: Nov 07, 2000
Posts: 61
Hi
My MIDlet gets started by PR through MMS. The MIDlet is is invoked, after that i open a connection for MMS, and then start a seperate thread to receive it. As soon as the receive method is called the code hangs there.

My question is should we open the connection before any message arrives ?
If this is what is to be done , then what is the use of PR(just to invoke the MIDlet, what about the MMS that is standing at the door to be received). Another problem is , that my MIDlet is receiveing only the first MMS that is sent to it, after it every call to receive returns the first MMS only, the subsequent MMS are not received. Is it a bug, or I am not doing things in a proper channel.
Thnx
chetan dhewal
Ranch Hand

Joined: Nov 07, 2000
Posts: 61
Hi,
One problem is solved i.e. receiving the same message over and over again. but the real problems remains intact.
thnx
Eduardo Marques
Ranch Hand

Joined: Feb 19, 2005
Posts: 231
There should be some kind of synchronisation problem.
Better post your code.
chetan dhewal
Ranch Hand

Joined: Nov 07, 2000
Posts: 61
Hi,
Better paste this code in Edit plus or TextPad to be understood.
Doesnt the MIDlet get system resources when run through OTA. What are the points that should be taken care of before doing PR through OTA.
Like classnames in PR entry and the name of the MIDlet should be same etc etc.

When i run it through PR via OTA, the notifyIncomingMessage() is not called, then the code hangs at receive(). The RMS also doesnt seems to be working. It doesn picks up the already saved data.

PR Entry - mms://:ECGMIDletAppID , ECGMIDlet, *

Here's the code code
----------------------------------------------------------------------

// ECG2
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import javax.microedition.rms.* ;
import java.util.*;




public class ECGMIDlet extends MIDlet implements CommandListener, MessageListener, Runnable
{

private static byte application_state;

private int patient_selected;

private Display application_display;

private List stored_ECG;


private MessageConnection main_msg_con;
private MMSReceiver receiver;
private Thread receiver_thread;
private boolean new_mms_flag;


private byte[] received_mms_data;


private ECGRMS ECGRMS_object;
private String[][] main_List_Data;



private boolean application_end;
private boolean run_executed;


public ECGMIDlet()
{
String[] incoming_con = PushRegistry.listConnections(true);
new_mms_flag=false;

try
{
//main_msg_con = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
//receiver = new MMSReceiver(this,main_msg_con);
//receiver_thread=new Thread(receiver);


if( incoming_con==null || incoming_con.length==0 )
{
application_state=1;

//main_msg_con.setMessageListener(this);
//receiver_thread.start();

}
else
{
application_state=2;
System.out.println("ECGMIDlet() : state=2 SET! ");
//main_msg_con.setMessageListener(this);
//receiver_thread.start();
}

//ECGRMS_object = new ECGRMS();

//application_display=Display.getDisplay(this);

System.out.println("Exiting ECGMIDlet constructor ");

}
catch(Exception e)
{
System.out.println("constructor : " + e);
}


}



public void startApp()
{
try
{
ECGRMS_object = new ECGRMS();
application_display=Display.getDisplay(this);
main_msg_con = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
receiver = new MMSReceiver(this,main_msg_con);
receiver_thread=new Thread(receiver);
main_msg_con.setMessageListener(this);
System.out.println("StartApp() : open, setMessageListener");
}
catch(Exception e)
{
System.out.println("startApp: open : " + e);
}


if(application_state == 1)
{
//main_msg_con.setMessageListener(this);
receiver_thread.start();

buildMainList();

if(stored_ECG != null)
{
addCmd();
stored_ECG.setCommandListener(this);

application_display.setCurrent(stored_ECG);
}
else
{
System.out.println("No DATA to be shown");
//notifyDestroyed();
}
}

if(application_state == 2)
{

try
{
/*
//main_msg_con.setMessageListener(this);
receiver_thread.start();

while( ! receiver.isRunExecuted())
{
System.out.println("startApp: state=2: sleep");
Thread.sleep(1000);
}

//receiver.setRunExecuted(false);
//application_state=1;
System.out.println("startApp: state=2: before saveData()");
ECGRMS_object.saveData(received_mms_data);
buildMainList();
addCmd();
stored_ECG.setCommandListener(this);

application_display.setCurrent(new Graph(this,received_mms_data));
*/
System.out.println("startApp: state=2 : before buildlist ");
buildMainList();

if(stored_ECG != null)
{
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(stored_ECG);
}
else
{
System.out.println("No DATA to be shown from startApp");
//notifyDestroyed();
}


Thread t=new Thread(this);
t.start();
System.out.println("Thread started... ");


//while( !run_executed )
{
//System.out.println("startApp (new): state=2: sleep");
//Thread.sleep(1000);
}

//ECGRMS_object.saveData(received_mms_data);
//buildMainList();
//addCmd();
//stored_ECG.setCommandListener(this);

//application_display.setCurrent(new Graph(this,received_mms_data));



}
catch(Exception e)
{
System.out.println("startApp: status=2 : " + e);
}
}

}


public void run()
{
if(application_state == 2)
{
try
{
//MessageConnection msg_con1 = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
//msg_con.setMessageListener(main_Object_reference);
System.out.println("Before receive : Main");
MultipartMessage received_msg = (MultipartMessage) main_msg_con.receive();
System.out.println("After receive : Main");
MessagePart msg_part = ((MultipartMessage)received_msg).getMessagePart("id1");
fillData(msg_part.getContent(), received_msg.getAddress(), received_msg.getSubject() );
//run_executed=true;

ECGRMS_object.saveData(received_mms_data);
buildMainList();
addCmd();
stored_ECG.setCommandListener(this);

application_display.setCurrent(new Graph(this,received_mms_data));


System.out.println("run : 1: after wait");
}
catch(Exception e)
{
System.out.println("run: state=2 : " + e);
}


}
}


public void notifyIncomingMessage(MessageConnection conn)
{
System.out.println("notifyIncomingMessage() : " + conn.toString());

if(main_msg_con == conn && application_state==1)
{
new_mms_flag=true;
receiver.handleMessage();
processNewMMS();
}

if(main_msg_con == conn && application_state==2)
{
System.out.println("NotifyIncoming msg: state=2");
}

}


public void processNewMMS()
{
try
{
while(!receiver.isRunExecuted())
{
System.out.println("processNewMessage: sleep");
Thread.sleep(100);
}

receiver.setRunExecuted(false);

ECGRMS_object.saveData(received_mms_data);
buildMainList();
addCmd();
stored_ECG.setCommandListener(this);
application_display.setCurrent(new Graph(this,received_mms_data));

}
catch(Exception e)
{
System.out.println("processNewMessage() : " + e);
}
}


public void fillData(byte[] data, String mob_no, String patient_name)
{
// ';' - 59
//Masking - ( byte[index] & 255)

String str= new String(data) + ';' + mob_no + ";" + patient_name ;

received_mms_data = str.getBytes();

}




public void buildMainList()
{
String[] arr = null;

try
{
main_List_Data = ECGRMS_object.retreiveList();


if( main_List_Data != null )
{
arr = new String[main_List_Data.length];


for(int i=0; i<main_List_Data.length; i++)
{
arr[i] = main_List_Data[i][2];
}

stored_ECG = new List("Exclusive", Choice.IMPLICIT, arr, null);

}
else
{
arr=new String[1];
arr[0] = "No Data to be shown.....";
stored_ECG = new List("Exclusive", Choice.IMPLICIT, arr, null);
}
}
catch(Exception e)
{
System.out.println("buildMainList() : " + e );
}

}




public void addCmd()
{
stored_ECG.addCommand(new Command("Exit", Command.BACK, 0));

stored_ECG.addCommand(new Command("View", Command.ITEM, 0));
stored_ECG.addCommand(new Command("Open", Command.ITEM, 0));
stored_ECG.addCommand(new Command("Forward", Command.ITEM, 0));
stored_ECG.addCommand(new Command("Delete",Command.ITEM,0));

}




public void commandAction(Command c, Displayable d)
{

patient_selected= ((List)d).getSelectedIndex() ;
try
{
if(c.getLabel().equals("View"))
{
application_display.setCurrent(new Graph(this, getSelectedECGByteArray( Integer.parseInt(main_List_Data[getSelectedECGIndex()][0]) ) ) );
}
}
catch(Exception e)
{
System.out.println("commandAction : View : " + e);
}


if(c.getLabel().equals("Delete"))
{
application_display.setCurrent(new DeleteECG(this, Integer.parseInt(main_List_Data[getSelectedECGIndex()][0]) ));
}


if(c.getLabel().equals("Forward"))
{
application_display.setCurrent(new ForwardECG(this));
}

}


public ECGRMS getECGRMSObject()
{
return ECGRMS_object;
}

public byte[] getSelectedECGByteArray(int Id) throws RecordStoreException
{
return ECGRMS_object.retreiveData(Id);
}

public int getSelectedECGIndex()
{
return patient_selected;
}

public boolean isApplicationEnd()
{
return application_end;
}

public Display getMainDisplay()
{
return application_display;
}


public int getApplicationState()
{
return application_state;
}

public List getMainList()
{
return stored_ECG;
}


public void pauseApp()
{

}


public void destroyApp(boolean unconditional)
{
System.out.println("Destroyed");
application_end=true;
receiver.handleMessage();

}


}


// ECGMIDlet END
//----------------------------------------------------------------------------------------------------------




// ECGRMS class Begins
//----------------------------------------------------------------------------------------------------------

class ECGRMS
{
private RecordStore ECG_RecordStore;

private RecordEnumeration record_enumeration;




public ECGRMS()
{

}


public void saveData(byte[] received_mms_data) throws RecordStoreException
{
try
{
System.out.println("1");
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;
System.out.println("2");
ECG_RecordStore.addRecord(received_mms_data, 0, received_mms_data.length) ;
System.out.println("3");
ECG_RecordStore.closeRecordStore();
System.out.println("4");

}
catch (Exception e)
{
System.out.println("saveData() : " + e);
e.printStackTrace() ;
}

}



public void deleteData(int Id) throws RecordStoreException
{
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;

ECG_RecordStore.deleteRecord(Id);

ECG_RecordStore.closeRecordStore();
}



public byte[] retreiveData(int Id) throws RecordStoreException
{
ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;

byte[] arr = ECG_RecordStore.getRecord(Id);

String str=new String(arr, 0, 1799);

ECG_RecordStore.closeRecordStore();

return str.getBytes();

}


public String[][] retreiveList() throws RecordStoreException
{
String list[][]=null;
int num_records;

ECG_RecordStore = RecordStore.openRecordStore("ECGDatabase", true, RecordStore.AUTHMODE_PRIVATE, false ) ;
record_enumeration = ECG_RecordStore.enumerateRecords(null,null,false);

num_records = record_enumeration.numRecords();



if( num_records != 0 )
{
list = new String[num_records][3];
int i,j;
i=j=-1;

while( record_enumeration.hasNextElement() )
{
list[++i][0] = "" + record_enumeration.nextRecordId();
}

byte arr[];

for(i=0; i < num_records; i++)
{
arr = ECG_RecordStore.getRecord( Integer.parseInt(list[i][0]) );
String str = new String(arr, 1801, arr.length - 1801 );

list[i][1]= str.substring( 0, str.indexOf(';') );
list[i][2]= str.substring( str.indexOf(';') + 1, str.length() - 1 );
}
}

ECG_RecordStore.closeRecordStore();

return list;

}


}

// ECGRMS class END
//----------------------------------------------------------------------------------------------------------




// ForwardECG class Starts
//----------------------------------------------------------------------------------------------------------


class ForwardECG extends Form implements CommandListener
{

private ECGMIDlet main_Object_reference;

private TextField mobile_no;

public ForwardECG(ECGMIDlet app)
{
super("Forward");

main_Object_reference=app;

append(mobile_no = new TextField("Mobile No. ", "", 11, TextField.NUMERIC));
addCmd();

setCommandListener(this);
}


public String getMobileNumber()
{
return mobile_no.getString();
}


public void addCmd()
{
addCommand(new Command("Back", Command.BACK, 0));
addCommand(new Command("Send", Command.CANCEL, 0));
}


public void commandAction(Command c, Displayable d)
{

if(c.getLabel().equals("Back"))
{
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}

if(c.getLabel().equals("Send"))
{
System.out.println("ECG of " + main_Object_reference.getSelectedECGIndex() + " sent to " + getMobileNumber() );
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}

}


}

// ForwardECG class END
//----------------------------------------------------------------------------------------------------------




// DeleteECG class Begins
//----------------------------------------------------------------------------------------------------------


class DeleteECG extends Form implements CommandListener
{

private ECGMIDlet main_Object_reference;
private int Id;

public DeleteECG(ECGMIDlet app, int Id)
{
super("Delete");

main_Object_reference=app;
this.Id = Id;

append("Are you sure you want to delete " + main_Object_reference.getSelectedECGIndex() + " ?");
addCmd();

setCommandListener(this);
}

public void addCmd()
{
addCommand(new Command("Yes", Command.BACK , 0));
addCommand(new Command("No" , Command.CANCEL, 0));
}


public void commandAction(Command c, Displayable d)
{
try
{
if(c.getLabel().equals("Yes"))
{
main_Object_reference.getECGRMSObject().deleteData(Id);

main_Object_reference.buildMainList();
main_Object_reference.addCmd();
main_Object_reference.getMainList().setCommandListener(main_Object_reference);

main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}


if(c.getLabel().equals("No"))
{
main_Object_reference.getMainDisplay().setCurrent( main_Object_reference.getMainList() );
}
}
catch(Exception e)
{
System.out.println("DeleteECG : commandAction: " + e);
}
}


}

// Delete class END
//-----------------------------------------------------------------------------------------------------------




// Graph class starts
//----------------------------------------------------------------------------------------------------------


class Graph extends Canvas implements CommandListener
{

ECGMIDlet main_Object_reference;
int window_x1;

Timer timer;
TimerTask task;
boolean right,left;
private byte[] arr;



public Graph(ECGMIDlet app, byte[] arr)
{
main_Object_reference=app;
this.arr=arr;
timer=new Timer();
setCommandListener(this);
addCmd();
}



public void paint(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);

g.drawString("" + main_Object_reference.getMainList().getString(main_Object_reference.getSelectedECGIndex()) ,0,0,Graphics.TOP | Graphics.LEFT);

drawGraph(g,window_x1);

}



public void drawGraph(Graphics g, int window_x1)
{
int x1,y1,x2,y2,j;

int maxX=getWidth();
int maxY=getHeight();

j=0;
x1=j;

y1= ( maxY - (arr[window_x1] & 255) ) / 2;

for(int i=1; i<maxX; i++)
{
x2=++j;
y2= (maxY - (arr[ window_x1 + (3 * (i*2)) ] & 255) ) / 2;

g.drawLine(x1,y1,x2,y2);

x1=x2;
y1=y2;
}



}



public void keyPressed(int keyCode)
{
right=left=false;

task= new TimerTask()
{
public void run()
{
if( ( window_x1 < (12 * ( (300 - getWidth()) / 2 ) ) ) && right==true )
{
window_x1 +=12;
repaint();
}


if( (window_x1 > 0) && left==true)
{
window_x1 -=12;
repaint();
}
}
};


if( ( window_x1 < (12 * ( (300 - getWidth()) / 2 ) ) ) && getGameAction(keyCode) == Canvas.RIGHT )
{
right=true;
timer.schedule(task,0,100);
}


if( (window_x1 > 0) && getGameAction(keyCode) == Canvas.LEFT )
{
left=true;
timer.schedule(task,0,100);
}

}



public void keyReleased(int keyCode)
{
task.cancel();
}


public void keyRepeated(int keyCode)
{

}


private void addCmd()
{
addCommand(new Command("Back", Command.BACK, 0));
addCommand(new Command("Zoom",Command.ITEM,0));
}



public void commandAction(Command c, Displayable d)
{

if(c.getLabel().equals("Back"))
main_Object_reference.getMainDisplay().setCurrent(main_Object_reference.getMainList());


if(c.getLabel().equals("Zoom"))
{
System.out.println("Zoom...");
}

}


}

// Graph class END
//----------------------------------------------------------------------------------------------------------





//MMSSender class & MMSReceiver
//----------------------------------------------------------------------------------------------------------

class MMSSender implements Runnable
{
private ECGMIDlet main_Object_reference;

private MessageConnection msg_con;
private MultipartMessage sending_msg;
private MessagePart msg_part;

private boolean run_executed;

public MMSSender(ECGMIDlet ob, MessageConnection msg_con)
{
this.msg_con=msg_con;
main_Object_reference=ob;
run_executed=false;
}

public boolean isRunExecuted()
{
return run_executed;
}

public void setRunExecuted(boolean executed)
{
run_executed=executed;
}



public void run()
{
try
{
sending_msg= (MultipartMessage)msg_con.newMessage(MessageConnection.MULTIPART_MESSAGE);

}
catch(Exception e)
{
System.out.println("MMSSender: run(): " + e);
}

}


}




class MMSReceiver implements Runnable
{
private ECGMIDlet main_Object_reference;

private MessageConnection msg_con;
private MultipartMessage received_msg;
private MessagePart msg_part;

private boolean run_executed;

public MMSReceiver(ECGMIDlet ob, MessageConnection msg_con)
{
this.msg_con=msg_con;
main_Object_reference=ob;
run_executed=false;
}

public boolean isRunExecuted()
{
return run_executed;
}

public void setRunExecuted(boolean executed)
{
run_executed=executed;
}


public void run()
{
/*
if(main_Object_reference.getApplicationState() == 2)
{
try
{
MessageConnection msg_con1 = (MessageConnection) Connector.open("mms://:ECGMIDletAppID");
//msg_con1.setMessageListener(main_Object_reference);
received_msg= (MultipartMessage) msg_con1.receive();
msg_part=((MultipartMessage)received_msg).getMessagePart("id1");
main_Object_reference.fillData(msg_part.getContent(), received_msg.getAddress(), received_msg.getSubject() );
run_executed=true;

System.out.println("run : 1: after wait");
}
catch(Exception e)
{
System.out.println("run: state=2 : " + e);
}


}*/

if(main_Object_reference.getApplicationState() == 1)
{
while( ! main_Object_reference.isApplicationEnd() )
{
try
{
System.out.println("run : 1: before wait");

synchronized(this)
{
received_msg=null;
msg_part=null;
wait();
}

if(main_Object_reference.isApplicationEnd())
{
break;
}

System.out.println("run: state=1: before receive()");
received_msg= (MultipartMessage) msg_con.receive();

msg_part=((MultipartMessage)received_msg).getMessagePart("id1");

main_Object_reference.fillData(msg_part.getContent(), received_msg.getAddress(), received_msg.getSubject() );

run_executed=true;

System.out.println("run : 1: after wait");

}
catch(Exception e)
{
System.out.println("run : 3 : " + e);
}
}

System.out.println("Exiting run()");
}


}

public synchronized void handleMessage()
{
System.out.println("notifying");
notify();
System.out.println("returning after notifying");
}


}

//----------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------
chetan dhewal
Ranch Hand

Joined: Nov 07, 2000
Posts: 61
Hi,
I opened up the Network Monitor in WTK 2.2, and found out that , the MMS is not received by the MIDlet under the MMS tab in Network Monitor utility. But then how come the MIDlet got started through PR? What could be the probable reasons that MIDlet got started but isnt receiving the MMS?
Eduardo Marques
Ranch Hand

Joined: Feb 19, 2005
Posts: 231
Hello,

You seem to be doing something clearly wrong in the MessageListener method notifyIncomingMessage.

You are correct of notifying the receiver thread but wrong afterwards when you call processMMS, which does sleeps. The listener should return immediately after notifying the receiver thread, as specified in the WMA specs. Note that the listener is invoked inside a system thread and it is not really a good idea to have it getting blocked.

Since the listener may not be returning, the system may not get round to dispatch your message.
I suspect this is the problem.
Either way, no other functionality other than waking up your "MMS receiver thread" should be inside the listener method (note in particular that you should not also call receive() inside the listener).
Pradeep Gudipati
Greenhorn

Joined: Mar 09, 2006
Posts: 16
Hey Chetan
Please try the application on the actual device or use nokia emulator. default emulator has some serious problems.
I haven't checked your code but i can confidently say that it will work on the device.
I struggled with similar problem for a month until i checked it on the device.
Thanks
Pradeep
chetan dhewal
Ranch Hand

Joined: Nov 07, 2000
Posts: 61
Hi all,
The problem is solved, pradeep you were right. Sun WTK 2.2 has some serious bug relating to Push Registry. I ran my code on WTK 2.5 and it worked in one go. I shouldnt have wasted my 20 days on correcting something which was wrong.
Pradeep Gudipati
Greenhorn

Joined: Mar 09, 2006
Posts: 16
see sharing knowledge shaved off so many days of valuable time...
WTK 2.5 is the best version ever till now
I guess everybody should stop using earlier versions totally

Thanks
Pradeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to open MessageConnection (Push Registry)?