lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2007年11月29日

用Spring,smppapi,apache mina, commons ssl快速实现安全的smpp smsc (二)

Filed under: Java,SSL — 标签:, , , , , , — lizongbo @ 14:39

接上一篇: http://618119.com/archives/2007/11/27/31.html

对smpp 数据包进行编码和解码的相关代码如下:

ProtocolCodecFactory的代码为:

[code]
package com.lizongbo.smpp.server.codec;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolDecoder;
public class SMPPProtocolCodecFactory
implements ProtocolCodecFactory {
ProtocolDecoder decoder = new SMPPProtocolDecoder();
ProtocolEncoder encoder = new SMPPProtocolEncoder();
public ProtocolEncoder getEncoder() throws Exception {
return encoder;
}
public ProtocolDecoder getDecoder() throws Exception {
return decoder;
}
}

[/code]

数据包解码的ProtocolDecoder为

[code]

package com.lizongbo.smpp.server.codec;

import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import ie.omk.smpp.message.SMPPProtocolException;
import ie.omk.smpp.BadCommandIDException;
import ie.omk.smpp.util.PacketFactory;
import org.apache.log4j.Logger;
import ie.omk.smpp.message.SMPPPacket;
import java.io.IOException;

public class SMPPProtocolDecoder
implements ProtocolDecoder {
protected Logger log = Logger.getLogger(getClass());
public void decode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {

try {
if (in.remaining() >= 4 && (in.remaining() >= in.getInt(in.position()))) {
byte[] byteLen = new byte[4];
in.get(byteLen);
int len = readInt(byteLen);
//System.out.println(“消息长度为:” + len);
if (in.remaining() >= (len – 4)) {
byte[] cmds = new byte[len – 4];
in.get(cmds);
SMPPPacket pak = null;
int id = -1;
id = readInt(cmds);
pak = PacketFactory.newInstance(id);
if (pak != null) {
byte[] reqb = new byte[4 + len];
System.arraycopy(byteLen, 0, reqb, 0, 4);
System.arraycopy(cmds, 0, reqb, 4, cmds.length);
pak.readFrom(reqb, 0);
if (log.isDebugEnabled()) {
StringBuilder sb = new StringBuilder(“收到的包为: “);
int l = pak.getLength();
int s = pak.getCommandStatus();
int n = pak.getSequenceNum();
sb.append(“commandId:”).append(Integer.toHexString(id))
.append(” len:”).append(Integer.toString(l))
.append(” commandStatus:”).append(Integer.toString(s))
.append(” sequenceNum:”).append(Integer.toString(n));
log.debug(sb);
}
out.write(pak);
}
}
}
}
catch (BadCommandIDException x) {
throw new SMPPProtocolException(“Unrecognised command received”, x);
}

}

public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws
Exception {
}

public void dispose(IoSession session) throws Exception {
}

public final int readInt(byte[] b) throws IOException {
int ch1 = b[0];
int ch2 = b[1];
int ch3 = b[2];
int ch4 = b[3];
// if ( (ch1 | ch2 | ch3 | ch4) < 0) {
// throw new EOFException();
// }
// see: http://618119.com/archives/2007/10/27/18.html
return ( ( (ch1 & 0xff) << 24) + ( (ch2 & 0xff) << 16) +
( (ch3 & 0xff) << 8) + (ch4 << 0));
}
}
[/code]

数据包的ProtocolEncoder非常简单了:

[code]

package com.lizongbo.smpp.server.codec;

import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.apache.mina.common.ByteBuffer;
import ie.omk.smpp.message.SMPPPacket;
import java.io.ByteArrayOutputStream;

public class SMPPProtocolEncoder
implements ProtocolEncoder {
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
if (message == null) {
return;
}
SMPPPacket packet = (SMPPPacket) message;
ByteArrayOutputStream outb = new ByteArrayOutputStream(packet.getLength());
packet.writeTo(outb);
byte[] b = outb.toByteArray();
ByteBuffer buf = ByteBuffer.allocate(b.length, true);
buf.setAutoExpand(true);
buf.put(b);
buf.flip();
session.write(buf);
}

public void dispose(IoSession session) throws Exception {
}

}
[/code]

Powered by WordPress