接上一篇: http://618119.com/archives/2007/11/29/34.html
针对每个SMPPPacket的处理,将其以接口方式进行处理,接口定义如下:
- 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;
- }
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,然后进行处理.
- package com.lizongbo.smpp.server;
- import java.util.*;
- import org.apache.mina.common.*;
- import ie.omk.smpp.*;
- import ie.omk.smpp.message.*;
- import ie.omk.smpp.version.*;
- public class SMPPServerSessionHandler
- extends IoHandlerAdapter {
- private Map<Long,
- SMPPPacketHandler> handles = null;
- private int readerIdleTimeout = 120;
- public void sessionOpened(IoSession session) throws Exception {
- session.setIdleTime(IdleStatus.READER_IDLE, readerIdleTimeout);
- }
- public void messageReceived(IoSession session, Object message) throws
- Exception {
- SMPPPacket packet = (SMPPPacket) message;
- // System.out.println(”收到消息” + packet);
- int id = packet.getCommandId();
- if (!SMPPVersion.getDefaultVersion().isSupported(id)) {
- StringBuffer err = new StringBuffer(120)
- .append(SMPPVersion.getDefaultVersion().toString())
- .append(” does not support command ID 0x”)
- .append(Integer.toHexString(id));
- throw new VersionException(err.toString());
- }
- long l = id & 0×00000000FFFFFFFFl;
- // Integer.decode(”0×80000005″)要出错,被迫用Long类型,
- //因此在这里需要将整数的id通过高位补0来实现,而不能直接进行强类型转换
- SMPPPacketHandler handler = handles.get(Long.valueOf(l));
- if (handler != null) {
- handler.process(session, packet);
- }
- else {
- //System.out.println(”丢弃的消息为:” + id + packet);
- //throw new BadCommandIDException();
- }
- }
- public Map getHandles() {
- return handles;
- }
- public int getReaderIdleTimeout() {
- return readerIdleTimeout;
- }
- public void setHandles(Map handles) {
- this.handles = handles;
- }
- public void setReaderIdleTimeout(int readerIdleTimeout) {
- this.readerIdleTimeout = readerIdleTimeout;
- }
- }