SMPP Server CloudHopper - how I should receive multipart messages? - java

I have CloudHopper SMPP server, at this moment I can receive a simple short messages.
if (pduRequest.getCommandId() == SmppConstants.CMD_ID_SUBMIT_SM) {
SubmitSm request = (SubmitSm) pduRequest;
request.getShortMessage();
....
}
But what I should do to receive long (Multipart) message?
I don't know what object I have to use ...
Help me, please.
Many thanks.

The following processes a multipart long message PDU that you would get when receiving a long message that has been split into multiple PDUs:
import com.cloudhopper.commons.charset.GSMCharset;
import com.cloudhopper.commons.gsm.GsmUtil;
import com.cloudhopper.smpp.pdu.DeliverSm;
import com.cloudhopper.smpp.util.SmppUtil;
...
DeliverSm mobileOriginatedMessage = (DeliverSm) pduRequest;
boolean isUdh = SmppUtil.isUserDataHeaderIndicatorEnabled(mobileOriginatedMessage.getEsmClass());
if (isUdh) {
byte[] userDataHeader = GsmUtil.getShortMessageUserDataHeader(messageBytes);
int thisMessageId = userDataHeader[3] & 0xff;
int totalMessages = userDataHeader[4] & 0xff;
int currentMessageNum = userDataHeader[5] & 0xff;
messageBytes = GsmUtil.getShortMessageUserData(messageBytes);
GSMCharset gsmCharset = new GSMCharset();
String message = gsmCharset.decode(messageBytes); // Example decoding, depends on charset used
System.out.println("thisMessageId: " + thisMessageId); // unique to message, same across all message parts
System.out.println("totalMessages: " + totalMessages);
System.out.println("currentMessageNum: " + currentMessageNum);
System.out.println("Message: " + message);
}
...
The above shows how to:
Determine if a PDU is multipart long (UDH) message
Get all the UDH header information so you can know
what message the part belongs to
what part number was received in order to put the message back together in the right order
and what the total number of parts you are expecting is
Get the actual message text of each part

Related

Get the Correlation ID from MQ as string

I am setting the Correlation ID while sending the message to MQ. And I need to use the CorrelationID from the COA of the message I pushed for the further processing.
I am setting the correlation ID and sending the message to queue using the below code.
MQMessage message = createMQMessage("12345");
message.write("Some message to push".getBytes());
queue.put(message);
private MQMessage createMQMessage(String corrID){
MQMessage message = new MQMessage();
message.messageFlags = MQConstants.MQMF_SEGMENTATION_ALLOWED;
if (ackQueueName != null) {
message.messageType = MQConstants.MQMT_REQUEST;
message.replyToQueueManagerName = ackQueueManagerName;
message.replyToQueueName = ackQueueName;
message.report = MQConstants.MQRO_COA | MQConstants.MQRO_COD;
message.correlationId = corrID.getBytes();
}
return message;
}
I am reading the replyQueue from another application to get the COA and extract the correlation ID for further processing.
But the correlation ID is in byte[] format and I used the below method getHexString to get the string. But all I got is 48 digit Hex format of my correlation ID like
414d5120514d41444556202020202020b5ca0d5b13b3bb20
public static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
All I need is the approach to convert the 48digit HexString to the Original Correlation ID I set. I tried using the below method to convert, but its giving me the junk data.
public static String hexStringToByteArray(String hex) {
int l = hex.length();
byte[] data = new byte[l/2];
for (int i = 0; i < l; i += 2) {
data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ Character.digit(hex.charAt(i+1), 16));
}
return new String(data);
}
Your report options are not explicitly set.
MQRO_COPY_MSG_ID_TO_CORREL_ID This is the default action, and
indicates that if a report or reply is generated as a result of this
message, the MsgId of this message is copied to the CorrelId of the
report or reply message.
Because of the default action, you will never receive the initial Correlation ID of your message. As Roger mentioned in his answer, it is bad practice to set the MsgID programmatically. Simply use the following option to have the CorrelID copied to the report message:
MQRO_PASS_CORREL_ID If a report or reply is generated as a result of
this message, the CorrelId of this message is copied to the CorrelId
of the report or reply message.
message.report = MQConstants.MQRO_COA
| MQConstants.MQRO_COD
| MQConstants.MQRO_PASS_CORREL_ID;
I set the message ID myself, now I am getting it fine
Do NOT set the message's messageId yourself. Let the queue manager create a unique messageId for each message that is put to a queue. If you need to pass information along with the message either (1) put it in the message data/payload or (2) add a Message Property (aka Named Property) to the message.
If you create your own messageId then there is a chance of duplicate messageIds which is a very bad thing and goes against Best Practices. IBM MQ Best Practices says to let the queue manager create a unique messageId.

Bluetooth LE data JSON in 20 bytes

I have a Bluetooth LE module on Arduino which sends a JSON string to an Android application.
The JSON string look like this:
{'d_stats':[{'t':'26.62','h':'59.64','p':'755.23','a':'109.02','hrm':'0.00'}]}
The Android app receives packets of 20 bytes (20 characters limit) and I can't find a method to put all packets together when the last packet was received.
Is there a way to know when the last packet is received?
Edit: the bluetooth sends data at a constant interval of time. There is a button connected to the Arduino board which, when pushed, will send other data via Bluetooth. The problem is that it overlaps with the timed transmission.
I found the solution, although not very elegant.
Instead of sending the whole JSON string, BLE will send a key/value pair in a single packet.
In C first:
char passMsg(String akey, char* origMsg){
// akey = object key must be 4 characters long
// origMsg + akey must be shorter than 20 characters
char* newmsg = origMsg;
size_t prevlen = strlen(newmsg);
memset(newmsg + prevlen, ' ', 15 - prevlen);
*(newmsg + 15) = '\0';
String bleMsg = akey + ":"+newmsg;
ble.print("AT+BLEUARTTX=");
ble.println(bleMsg);
}
This way I pass a string like this: temp:20.45
Then in Android/Java:
String[] rawString = data.replace(" ", "").split(":");
if(rawString.length>1){
String apiCallKey = rawString[0];
String apiCallVal = rawString[1];
callAPI(apiCallKey,apiCallVal);
}
Where data is raw data from Bluetooth.
Phew...

PUSH Notifications for >1000 devices through GCM Server(Java)

I have a GCM-backend Java server and I'm trying to send to all users a notification msg. Is my approach right? To just split them into 1000 each time before giving the send request? Or is there a better approach?
public void sendMessage(#Named("message") String message) throws IOException {
int count = ofy().load().type(RegistrationRecord.class).count();
if(count<=1000) {
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(count).list();
sendMsg(records,message);
}else
{
int msgsDone=0;
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).list();
do {
List<RegistrationRecord> regIdsParts = regIdTrim(records, msgsDone);
msgsDone+=1000;
sendMsg(regIdsParts,message);
}while(msgsDone<count);
}
}
The regIdTrim method
private List<RegistrationRecord> regIdTrim(List<RegistrationRecord> wholeList, final int start) {
List<RegistrationRecord> parts = wholeList.subList(start,(start+1000)> wholeList.size()? wholeList.size() : start+1000);
return parts;
}
The sendMsg method
private void sendMsg(List<RegistrationRecord> records,#Named("message") String message) throws IOException {
if (message == null || message.trim().length() == 0) {
log.warning("Not sending message because it is empty");
return;
}
Sender sender = new Sender(API_KEY);
Message msg = new Message.Builder().addData("message", message).build();
// crop longer messages
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
for (RegistrationRecord record : records) {
Result result = sender.send(msg, record.getRegId(), 5);
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getRegId());
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
// if the device is no longer registered with Gcm, remove it from the datastore
ofy().delete().entity(record).now();
} else {
log.warning("Error when sending message : " + error);
}
}
}
}
Quoting from Google Docs:
GCM is support for up to 1,000 recipients for a single message. This capability makes it much easier to send out important messages to your entire user base. For instance, let's say you had a message that needed to be sent to 1,000,000 of your users, and your server could handle sending out about 500 messages per second. If you send each message with only a single recipient, it would take 1,000,000/500 = 2,000 seconds, or around half an hour. However, attaching 1,000 recipients to each message, the total time required to send a message out to 1,000,000 recipients becomes (1,000,000/1,000) / 500 = 2 seconds. This is not only useful, but important for timely data, such as natural disaster alerts or sports scores, where a 30 minute interval might render the information useless.
Taking advantage of this functionality is easy. If you're using the GCM helper library for Java, simply provide a List collection of registration IDs to the send or sendNoRetry method, instead of a single registration ID.
We can not send more than 1000 push notification at time.I searched a lot but not result then i did this with same approach split whole list in sub lists of 1000 items and send push notification.

App Engine Backend with Google Cloud Messaging sending message to more than 1000 users

I want to send a message (e.g. Update available) to all users(~15,000). I have implemented App Engine Backend with Google Cloud Messaging to send message.
I have tested on 2 devices. Got message on both. But as google docs says "GCM is support for up to 1,000 recipients for a single message."
My question is how to send same message to remaining 14,000 users in my
case? Or the code below will take care of it?
Below is the code which sends message
import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiNamespace;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Named;
import static com.example.shani.myapplication.backend.OfyService.ofy;
/**
* An endpoint to send messages to devices registered with the backend
* <p/>
* For more information, see
* https://developers.google.com/appengine/docs/java/endpoints/
* <p/>
* NOTE: This endpoint does not use any form of authorization or
* authentication! If this app is deployed, anyone can access this endpoint! If
* you'd like to add authentication, take a look at the documentation.
*/
#Api(name = "messaging", version = "v1", namespace = #ApiNamespace(ownerDomain = "backend.myapplication.shani.example.com", ownerName = "backend.myapplication.shani.example.com", packagePath = ""))
public class MessagingEndpoint {
private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName());
/**
* Api Keys can be obtained from the google cloud console
*/
private static final String API_KEY = System.getProperty("gcm.api.key");
/**
* Send to the first 10 devices (You can modify this to send to any number of devices or a specific device)
*
* #param message The message to send
*/
public void sendMessage(#Named("message") String message) throws IOException {
if (message == null || message.trim().length() == 0) {
log.warning("Not sending message because it is empty");
return;
}
// crop longer messages
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
Sender sender = new Sender(API_KEY);
Message msg = new Message.Builder().addData("message", message).build();
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(1000).list();
for (RegistrationRecord record : records) {
Result result = sender.send(msg, record.getRegId(), 5);
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getRegId());
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
// if the device is no longer registered with Gcm, remove it from the datastore
ofy().delete().entity(record).now();
} else {
log.warning("Error when sending message : " + error);
}
}
}
}
}
I know there are simillar questions but I am using Java language. I found questions which uses php language at backend. so not helpful to me!
Google Cloud Messaging: Send message to "all" users
Sending Push Notification on multiple devices
Is there anyone who has successfully implemented App Engine+Google Cloud Messaging JAVA language?
In the below code line if I replace 1000 with 15,000 Will it solve my problem?
List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(1000).list();
Please please help as soon as possible. And very sorry for my English.. If anyone need other details you are welcome to ask.
Thanks for your time.
A few considerations,
1) Sending notifications to a possibly huge number of users might take significant time, consider using Task Queues to queue that work to be done "offline" outside the 60 sec limit.
2) Now as for the GCM limit, if you need to all your users but GCM allow you 1000 at a time just split them in batches of 1000 and send every batch a message separately.
If you combine both recommendations you should have a fairly scalable process where you query for all your users in 1 request , split that list and just queue sending the message to those users 1000 at a time.
Extension to the #jirungaray answer below is code for sending GCM messages to all registered users,
Here I assume that from android you are registering each mobile-devices for GCM services and storing those device tokens in database.
public class GCM {
private final static Logger LOGGER = Logger.getLogger(GCM.class.getName());
private static final String API_KEY = ConstantUtil.GCM_API_KEY;
public static void doSendViaGcm(List<String> tocken,String message) throws IOException {
Sender sender = new Sender(API_KEY);
// Trim message if needed.
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
Message msg = new Message.Builder().addData("message", message).build();
try{
MulticastResult result = sender.send(msg, tocken, 5);
}catch(Exception ex){
LOGGER.severe("error is"+ex.getMessage());
ex.printStackTrace();
}
}
}
In above code snippet API_KEY can be obtain from google console project ,here I assume that you have already created one google console project and enable GCM api,
you can generate API_KEY as follows
your_google_console_project>> Credentials>> Create New Key >> Server
key >> enter ip address Which you want to allow
access to GCM api[i used 0.0.0.0/0]
Now doSendViaGcm(List tocken,String message) of GCM class performs task of sending messages to all register android mobile devices
here List<String> token is array-list of all device token on which messages will be delivered ,remember this list size should not more than 1000 or else http call will fail.
hope this will help you
thanks

phonegap displaying text in native textbox

I'm making an android app which uses bluetooth using phonegap and I'm having issues handling code because it get's called every 100ms.
I want to try if the issue occurs because my phonegap javascript code is to long causing delays.
To try this I want to dispaly the received message using a native textbox.
Is this possible? and How would I do this?
Thanks
Martijn
PS: I've tried using Toast but since I want to change the displayed value every 100ms this doesn't work.
Code
upon receiving a message this is the order the code is processed
bytes = mmInStream.read(buffer);
s_message = new String(buffer, 0, bytes);
String[] strArray = s_message.split(",");
mHandleReceivedThread = new HandleReceivedThread(strArray);
mHandleReceivedThread.start();
In the HandleReceivedThread:
if(strArrayReceived[0].startsWith("Pressure"))
{
if(strArrayReceived[1].contains("Pressure"))
{
double Pressure = Double.parseDouble(strArrayReceived[1].replace("Pressure", ""));
double PressureResult = Pressure/10;
String PressureValue = String.valueOf(PressureResult);
webView.sendJavascript("PressureValue = " + Pressure + ";");
webView.sendJavascript("document.getElementById('Pressure').innerHTML = " + PressureValue + ";");
Log.e(TAG, "Message fail: " + strArrayReceived[1] );
}
else
{
double Pressure = Double.parseDouble(strArrayReceived[1]
double PressureResult = Pressure/10;
String PressureValue = String.valueOf(PressureResult);
webView.sendJavascript("PressureValue = " + Pressure + ";");
webView.sendJavascript("document.getElementById('Pressure').innerHTML = " + PressureValue + ";");
}
}
this is basically what happens.
And this happens every 100ms , and for some reason the displaying of the Value has lag and sometimes the received message (which should be "Pressure,1500" , or a different value) is Pressure,1500Pressure , causing errors (which I've managed to fix). but this still isn't optimal.
The message is send correctly and if I use a simple chat app they are received correctly.
if there is anything else I should supply , just ask ;-)

Categories