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

2007年12月6日

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

Filed under: Java,SSL — 标签:, , , , , , — lizongbo @ 09:18

接上一篇: http://618119.com/archives/2007/12/03/35.html

对 SMPPPacket的业务处理代码:

1.处理 BIND_RECEIVER消息

[code]

package com.lizongbo.smpp.server.handlers;

import org.apache.mina.common.*;
import com.lizongbo.smpp.server.*;
import ie.omk.smpp.*;
import ie.omk.smpp.message.*;
import ie.omk.smpp.message.tlv.*;
import ie.omk.smpp.util.*;
import ie.omk.smpp.version.*;

/**
*
* <p>Title: BindReceiver Handler</p>
*
* <p>Description: smpp BIND_RECEIVER</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: http://618119.com </p>
*
* @author lizongbo
* @version 1.0
*/
public class BindReceiverHandler
implements SMPPPacketHandler {
public void process(IoSession session, SMPPPacket packet) throws
BadCommandIDException {
BindReceiver msg = (BindReceiver) packet;
System.out.println(“sysId=” + msg.getSystemId());
System.out.println(“password=” + msg.getPassword());
//在这里判断账号和密码是否正确
System.out.println(“sysType=” + msg.getSystemType());
System.out.println(“interfaceVer=” + msg.getInterfaceVersion());
System.out.println(“addrTon=” + msg.getAddressTon());
System.out.println(“addrNpi=” + msg.getAddressNpi());
System.out.println(“addressRange=” + msg.getAddressRange());

BindReceiverResp res = (BindReceiverResp) PacketFactory.
newInstance(SMPPPacket.BIND_RECEIVER_RESP);
res.setCommandStatus(0);
res.setSequenceNum(packet.getSequenceNum());
res.setSystemId(“smsc.618119.com”);
res.setOptionalParameter(Tag.SC_INTERFACE_VERSION,
SMPPVersion.getDefaultVersion().getVersionID());
session.write(res);
}

}

[/code]

2.处理BIND_TRANSMITTER消息

[code]

package com.lizongbo.smpp.server.handlers;

import org.apache.mina.common.*;
import com.lizongbo.smpp.server.*;
import ie.omk.smpp.*;
import ie.omk.smpp.message.*;
import ie.omk.smpp.message.tlv.*;
import ie.omk.smpp.util.*;
import ie.omk.smpp.version.*;
/**
*
* <p>Title: BindTransmitter Handler</p>
*
* <p>Description: smpp BIND_TRANSMITTER</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: http://618119.com </p>
*
* @author lizongbo
* @version 1.0
*/
public class BindTransmitterHandler
implements SMPPPacketHandler {
public void process(IoSession session, SMPPPacket packet) throws
BadCommandIDException {
BindTransmitter msg = (BindTransmitter) packet;
System.out.println(“sysId=” + msg.getSystemId());
System.out.println(“password=” + msg.getPassword());
System.out.println(“sysType=” + msg.getSystemType());
System.out.println(“interfaceVer=” + msg.getInterfaceVersion());
System.out.println(“addrTon=” + msg.getAddressTon());
System.out.println(“addrNpi=” + msg.getAddressNpi());
System.out.println(“addressRange=” + msg.getAddressRange());

BindTransmitterResp res = (BindTransmitterResp) PacketFactory.
newInstance(SMPPPacket.BIND_TRANSMITTER_RESP);
res.setCommandStatus(msg.getCommandStatus());
res.setSequenceNum(packet.getSequenceNum());
res.setSystemId(“smsc.618119.com”);
res.setOptionalParameter(Tag.SC_INTERFACE_VERSION,
SMPPVersion.getDefaultVersion().getVersionID());
session.write(res);
}

}
[/code]

3.处理DELIVER_SM_RESP,SUBMIT_SM,UNBIND消息的代码片断.

[code]

public void process(IoSession session, SMPPPacket packet) throws
BadCommandIDException {
DeliverSMResp msg = (DeliverSMResp) packet;
System.out.println(“收到确认的消息编号:” + msg.getMessageId());
}

public void process(IoSession session, SMPPPacket packet) throws
BadCommandIDException {
SubmitSM msg = (SubmitSM) packet;

System.out.println(“serviceType=” + msg.getServiceType());
System.out.println(“source=” + msg.getSource());
System.out.println(“destination=” + msg.getDestination());
System.out.println(“esmClass=” + msg.getEsmClass());
System.out.println(“protocolID=” + msg.getProtocolID());
System.out.println(“priority=” + msg.getPriority());
System.out.println(“delivery=” + msg.getDeliveryTime());

System.out.println(“expiryTime=” + msg.getExpiryTime());
System.out.println(“registered=” + msg.getRegistered());
System.out.println(“replaceIfPresent=” + msg.getReplaceIfPresent());
System.out.println(“dataCoding=” + msg.getDataCoding());
System.out.println(“defaultMsg=” + msg.getDefaultMsg());
System.out.println(“message=” + new String(msg.getMessage()));
SubmitSMResp res = (SubmitSMResp) PacketFactory.
newInstance(SMPPPacket.SUBMIT_SM_RESP);
res.setCommandStatus(0);
res.setSequenceNum(packet.getSequenceNum());
res.setMessageId(“11233”);
session.write(res);
}

public void process(IoSession session, SMPPPacket packet) throws
BadCommandIDException {
Unbind msg = (Unbind) packet;
UnbindResp res = (UnbindResp) PacketFactory.
newInstance(SMPPPacket.UNBIND_RESP);
res.setCommandStatus(0);
res.setSequenceNum(packet.getSequenceNum());
session.write(res);
}

[/code]

2007年12月3日

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

Filed under: Java,SSL — 标签:, , , , , , — lizongbo @ 11:30

接上一篇: 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 & 0x00000000FFFFFFFFl;
// Integer.decode(“0x80000005”)要出错,被迫用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]

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]

« Newer PostsOlder Posts »

Powered by WordPress