---
title: "用Spring,smppapi,mina, commons ssl快速实现安全的smp(3)"
author: "lanmh"
date: "2008-07-21T15:38:37Z"
tags: ["Apache", "HTML", "IE", "Mina", "Spring"]
slug: "spring-smppapi-mina-commons-ssl-smp-3"
draft: false
lastmod: "2008-07-21T15:38:37Z"
id: 216954
url: "https://lanmh.iteye.com/blog/216954"
---

## 作者：[lizongbo](http://618119.com/) [版权声明](http://creativecommons.org/licenses/by/3.0/deed.zh)：可以任意转载，转载时请务必以超链接形式标明文章[原始出处](http://618119.com/archives/2007/12/03/35.html)和作者信息及[本版权声明](http://618119.com/archives/2007/12/03/35.html)。 [http://618119.com/archives/2007/12/03/35.html](http://618119.com/archives/2007/12/03/35.html)

* * *

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

 

[view plain](http://618119.com/archives/2007/12/03/35.html)[copy to clipboard](http://618119.com/archives/2007/12/03/35.html)[print](http://618119.com/archives/2007/12/03/35.html)[?](http://618119.com/archives/2007/12/03/35.html)

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 plain](http://618119.com/archives/2007/12/03/35.html)[copy to clipboard](http://618119.com/archives/2007/12/03/35.html)[print](http://618119.com/archives/2007/12/03/35.html)[?](http://618119.com/archives/2007/12/03/35.html)

01. package com.lizongbo.smpp.server;
02. import java.util.\*;
03. import org.apache.mina.common.\*;
04. import ie.omk.smpp.\*;
05. import ie.omk.smpp.message.\*;
06. import ie.omk.smpp.version.\*;
07. public class SMPPServerSessionHandler
08. extends IoHandlerAdapter {
09. private Map&lt;Long,
10. SMPPPacketHandler&gt; 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. }