Hi! Welcome...

I am Ashish. I am Member of Apache MINA PMC, ASF Committer and avid Code hacker. Contact me at paliwalashish at gmail dot com. I work for Terracotta [http://www.terracotta.org] as Solution Architect. I contribute to jsmpp [http://code.google.com/p/jsmpp/] as well.

26 January 2009 ~ 16 Comments

[SMPP] Sending long SMS through SMPP


SMS is designed to send small text messages upto 140 chars. It actually depends upon the encoding. Using 7 bit encoding, you can send 160 chars, using Unicode, you can only send 70 chars. The data is carried in User Data section.

This post talks about ways of sending Long message using SMPP

Using SMPP we can send long SMS by two means

Using Payload option

Submit SM PDU has a field called message_payload. Set the data in it and you can send upto 64K octets. But this option is not supported by a lot of provider. Verify with your provider, if they support the same.

Using Message fragementation

This format is more common and widely supported. Essentially, the long SMS is divided into smaller messages and send with a particular format, by setting UDH.

The key TLV options used are

sar_msg_ref_num - The reference number for a particular concatenated short message

sar_total_segments - Indicates the total number of short messages within the concatenated short message

sar_segment_seqnum - Indicates the sequence number of a particular short message fragment within the concatenated short message

How do you set these, depends upon the SMPP library you use. Will brief the algo

 

  • Break the long sms into parts, so that fragement can fit into single Submit SM PDU
  • The total number of fragements shall give the value of sar_total_segments
  • Generate a unique identifier value, this becomes the values of sar_msg_ref_num
  • Now in a loop send the message fragements, with values in step above. sar_segment_seqnum values shall be the value of the loop count (ensure that it starts from 1)

This is it :-)

Tags: ,

06 January 2009 ~ 4 Comments

[MPXJ Series] Writing MS Project File using MPXJ


In the first part, we shall explore what are the different ways of writing a MS Project file using MPXJ.

Pre-requisite

The article assumes that you have downloaded MPXJ release and related dependencies. If not here are the links for download

 

Now let’s get back to the original intent of reading MPP files

Lets see the simplest form of reading an MPP file

try {
    ProjectWriter writer = ProjectWriterUtility.getProjectWriter("test.mpx");
    ProjectFile project = new ProjectFile();
 
    // Operations on Project File
    writer.write(project, "test.mpx");
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (IOException ioEx) {
    ioEx.printStackTrace();
}

ProjectWriterUtility.getProjectWriter(fileName), provides an appropriate Reader class, based on the version of MS Project file. This is the simplest way to create a writer for an Project file. ProjectFile is the class that contains details about the Schedule, we can iterate and extract information from it. We shall discuss the details on ProjectFile in subsequent posts. ProjectWriterUtility class checks for the file extension and returns an appropriate writer.

Alternative ways

  • For writing MPX file, you can use net.sf.mpxj.mpx.MPXWriter
  • For writing XML Project file, use net.sf.mpxj.mspdi. MSPDIWriter
Tags:

16 December 2008 ~ 0 Comments

[MPXJ Series] Reading MS Project Files using MPXJ


It’s been a while since my last post on using MPXJ. You can find it here. Since then have been observing the search items and have found that need to have some simple posts related to MPXJ. So starting a series on MPXJ, which I shall keep updating as and when I learn more about it.

In the first part, let’s explore what are the different ways of reading a MPP file in MPXJ.

Pre-requisite

The article assumes that you have downloaded MPXJ release and related dependencies. If not here are the links for download

  • MPXJ - http://sourceforge.net/project/showfiles.php?group_id=70649
  • Dependencies - http://mpxj.sourceforge.net/dependencies.html

Now let’s get back to the original intent of reading MPP files

Lets see the simplest form of reading an MPP file

try {
     ProjectReader reader = ProjectReaderUtility.getProjectReader(fileName);
     ProjectFile projectFile = reader.read(fileName);
     // ... process Project file here
} catch (Exception e) {
     e.printStackTrace();
}

ProjectReaderUtility.getProjectReader(), provides an appropriate Reader class, based on the version of MS Project file. This is the simplest way to read an MPP file. ProjectFile is the class that contains details about the Schedule, we can iterate and extract information from it.

Alternative ways

  • For reading MPX file, you can use net.sf.mpxj.mpx.MPXReader
  • For reading MPP,MPT file, use net.sf.mpxj.mpp.MPPReader
  • For reading MSPDI, use net.sf.mpxj.mspdi.MSPDIReader

15 December 2008 ~ 1 Comment

Multi Threaded Trap Receiver using SNMP4J


The entry briefly touched upon creating a multithreaded Trap receiver using SNMP4J. This is based on example shipped with SNMP4J

Pre-requisite

  •  SNMP4J
  •  Log4j

Structure

For complete source have a look at the class org.snmp4j.test. MultiThreadedTrapReceiver.java shipped with SNMP4J distribution

Logical structure of the program

  • Create Socket for accepting Traps
  • Create a Thread pool to process PDU’s
  • Process the Traps

Let’s get into the code

Lets address the Listener creation steps

private void init() throws UnknownHostException, IOException {
 
    threadPool = ThreadPool.create("Trap", 2);
 
    dispatcher = MultiThreadedMessageDispatcher(threadPool,
                                           new MessageDispatcherImpl());
 
    listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress",
                                                "udp:0.0.0.0/162"));
 
    TransportMapping transport;
 
    if (listenAddress instanceof UdpAddress) {
      transport = new DefaultUdpTransportMapping((UdpAddress)listenAddress);
    } else {
      transport = new DefaultTcpTransportMapping((TcpAddress)listenAddress);
    }
 
    snmp = new Snmp(dispatcher, transport);
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
    snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
 
    USM usm = new USM(SecurityProtocols.getInstance(),
                      new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(usm);
    snmp.listen();
  }

Here we see that we have created Transport mapping to Listener on port 162. Next we add all three Message processing models, so that we can process Traps received from all three versions of SNMP (V1, V2C and V3). For SNMPv3, we add a Security model.

That’s it done. We listen and process the traps.

The default implementation of processPdu() in this class, just dumps some statistics on the output.

Customization’s

Let’s see what all can we do with this default example

  • Set only the SNMP version you want to support
  • Customize the Security Model for SNMP V3
  • The whole logic lies on processPDU(). You can take complete control of how to process the message and subsequent processing of the message like dumping it to database etc.

09 December 2008 ~ 0 Comments

Using Auto expanding and shrinking IoBuffer in Apache MINA


Networking applications always need ByteBuffer that can expand on the need basis. Java ByteBuffer doesn’t provide this feature. Fortunately, MINA’s IoBuffer class has implemented this much needed feature.

Creating Auto Expanding Buffer

IoBuffer class in MINA has a property autoExpand, which when set makes the IoBuffer expand on the need basis, very much similar to StringBuffer class.
Let’s see, how we can achieve this

 
IoBuffer buffer = IoBuffer.allocate(8);
buffer.setAutoExpand(true);
 
buffer.putString("12345678", encoder);
// Add more to this buffer
buffer.put((byte)10);
 

When we set this property, the buffer will expand as and when needed. Under the hood, it essentially allocated a new IoBuffer and returns the new instance with the copied data.

So, can we shrink the buffer as well. Yes, if we set the autoshrink property to true.

 
IoBuffer buffer = IoBuffer.allocate(8);
buffer.setAutoShrink(true);
buffer.put((byte)7);
System.out.println("Buffer size = "+buffer.capacity());
buffer.shrink();
buffer.capacity(16);
System.out.println("Buffer size = "+buffer.capacity());
buffer.shrink();
System.out.println("Buffer size = "+buffer.capacity());
 

Here we set autoShrink property to true. To manually shrink the capacity, we use shrink() method. Caution, shrink(), won’t reduce capacity below the minimum capacity (the capacity with which the buffer was created). The second call to shrink(), will reduce the capacity to 8.

Has updated the MINA’s documentation page on IoBuffer today. So please feel free to find more details here