接上一篇: http://618119.com/archives/2007/11/29/34.html
针对每个SMPPPacket的处理,将其以接口方式进行处理,接口定义如下:
[code]
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;
}
[/code]
SMPPServerSessionHandler中通过commandid来找到相应的handler,然后进行处理.
[code]
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;
}
}
[/code]
Tags: apache mina, commons ssl, smpp, smppapi, smsc, spring, 短信Related posts
Tags: apache mina, commons ssl, smpp, smppapi, smsc, spring, 短信
[...] 用Spring,smppapi,apache mina, commons ssl快速实现安全的smpp smsc (四) 作者:lizongbo 发表于:9:18 am. 星期四, 12月 6th, 2007 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明。 http://618119.com/archives/2007/12/06/39.html 接上一篇: http://618119.com/archives/2007/12/03/35.html [...]