ITeye博客

用Spring,smppapi,mina, commons ssl快速实现安全的smp(3)

lanmh · 2008-07-21 15:38 · MINA · HTML · Apache · Spring · Mina · IE · Markdown

作者:lizongbo 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明。 http://618119.com/archives/2007/12/03/35.html


接上一篇: http://618119.com/archives/2007/11/29/34.html
针对每个SMPPPacket的处理,将其以接口方式进行处理,接口定义如下:

 

view plaincopy to clipboardprint?

  1. package com.lizongbo.smpp.server;
  2. import org.apache.mina.common.IoSession;
  3. import ie.omk.smpp.message.SMPPPacket;
  4. import ie.omk.smpp.BadCommandIDException;
  5. public interface SMPPPacketHandler {
  6. void process(IoSession session, SMPPPacket packet) throws
  7. BadCommandIDException;
  8. }
package com.lizongbo.smpp.server;
import org.apache.mina.common.IoSession;
import ie.omk.smpp.message.SMPPPacket;
import ie.omk.smpp.BadCommandIDException;
public interface SMPPPacketHandler {
void process(IoSession session, SMPPPacket packet) throws
BadCommandIDException;
}

 

SMPPServerSessionHandler中通过commandid来找到相应的handler,然后进行处理.

 

view plaincopy to clipboardprint?

  1. package com.lizongbo.smpp.server;
  2. import java.util.*;
  3. import org.apache.mina.common.*;
  4. import ie.omk.smpp.*;
  5. import ie.omk.smpp.message.*;
  6. import ie.omk.smpp.version.*;
  7. public class SMPPServerSessionHandler
  8. extends IoHandlerAdapter {
  9. private Map<Long,
  10. SMPPPacketHandler> handles = null;
  11. private int readerIdleTimeout = 120;
  12. public void sessionOpened(IoSession session) throws Exception {
  13. session.setIdleTime(IdleStatus.READER_IDLE, readerIdleTimeout);
  14. }
  15. public void messageReceived(IoSession session, Object message) throws
  16. Exception {
  17. SMPPPacket packet = (SMPPPacket) message;
  18. // System.out.println(”收到消息” + packet);
  19. int id = packet.getCommandId();
  20. if (!SMPPVersion.getDefaultVersion().isSupported(id)) {
  21. StringBuffer err = new StringBuffer(120)
  22. .append(SMPPVersion.getDefaultVersion().toString())
  23. .append(” does not support command ID 0x”)
  24. .append(Integer.toHexString(id));
  25. throw new VersionException(err.toString());
  26. }
  27. long l = id & 0×00000000FFFFFFFFl;
  28. // Integer.decode(”0×80000005″)要出错,被迫用Long类型,
  29. //因此在这里需要将整数的id通过高位补0来实现,而不能直接进行强类型转换
  30. SMPPPacketHandler handler = handles.get(Long.valueOf(l));
  31. if (handler != null) {
  32. handler.process(session, packet);
  33. }
  34. else {
  35. //System.out.println(”丢弃的消息为:” +  id + packet);
  36. //throw new BadCommandIDException();
  37. }
  38. }
  39. public Map getHandles() {
  40. return handles;
  41. }
  42. public int getReaderIdleTimeout() {
  43. return readerIdleTimeout;
  44. }
  45. public void setHandles(Map handles) {
  46. this.handles = handles;
  47. }
  48. public void setReaderIdleTimeout(int readerIdleTimeout) {
  49. this.readerIdleTimeout = readerIdleTimeout;
  50. }
  51. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。