package com.msp.smg;
import java.util.List;
import org.apache.log4j.Logger;
import com.logica.smpp.Connection;
import com.logica.smpp.Data;
import com.logica.smpp.Receiver;
import com.logica.smpp.Session;
import com.logica.smpp.TCPIPConnection;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.BindResponse;
import com.logica.smpp.pdu.BindTransmitter;
import com.logica.smpp.pdu.Response;
import com.logica.smpp.pdu.SubmitSM;
import com.logica.smpp.pdu.UnbindResp;
import com.msp.smg.Queue;
public class RequestWorker extends Thread {
private final static Logger logger = Logger.getLogger(RequestWorker.class);
private boolean isRuning = true;
private boolean isConnectSMSC = false;
private boolean isBound = false;
private Session session = null;
private String sysId = "";
private String syspwd = "";
private String smscIP = "";
private int smscPort = 0;
private Receiver receiver = null;
ResponseWorker responseWorker = null;
private List<SendSM> sendsmList;
/**
* 连接到短信中心
*/
private void connectSMSC() {
try {
Connection conn = new TCPIPConnection(smscIP, smscPort);
session = new Session(conn);
isConnectSMSC = true;
} catch (Exception ex) {
isConnectSMSC = false;
ex.printStackTrace();
}
}
/**
* 实现绑定到短信中心的功能
*/
private void bindSMSC() {
if (isConnectSMSC && session != null) {
try {
BindRequest bindReq = new BindTransmitter();
bindReq.setSystemId(sysId);
bindReq.setPassword(syspwd);
BindResponse bindResponse = null;
bindResponse = session.bind(bindReq);
if (bindResponse.getCommandStatus() == Data.ESME_ROK) {
receiver = session.getReceiver();
if (responseWorker == null) {
responseWorker = new ResponseWorker();
}
responseWorker.setReceiver(receiver);
responseWorker.start();
isBound = true;
} else {
isBound = false;
}
} catch (Exception ex) {
isBound = false;
ex.printStackTrace();
}
} else {
logger.info("连接到短信中心的connection失效!");
}
}
public SendSM getSendSM() {
SendSM sendsm = null;
try {
if (sendsmList != null && sendsmList.size() > 0) {
sendsm = (SendSM) sendsmList.remove(0);
}
} catch (Exception e) {
sendsm = null;
}
return sendsm;
}
public void run() {
SendSM sendsm = null;
while (isRuning) {
try {
// System.out.println("smscIP=" + smscIP + ",smscPort=" +
// smscPort
// + ",sysId=" + sysId + ",syspwd=" + syspwd);
/*
* while (!isConnectSMSC) { this.connectSMSC(); } if (!isBound) {
* this.bindSMSC(); } while (isBound && this.getSendSM() !=
* null) { sendShortMessage(sendsm); } ; this.unBind();
*/
} finally {
isRuning = true;
}
}
}
/*
* 发送短信
*/
public void sendShortMessage(SendSM sendsm) {
SubmitSM request = new SubmitSM();
try {
request.setSourceAddr(sendsm.getSourceAddr());
request.setDestAddr(sendsm.getDescAddr());
request.setShortMessage(sendsm.getShortMessage());
session.submit(request);
// getStatus(response);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
}
public void unBind() {
try {
UnbindResp response = session.unbind();
getStatus(response);
} catch (Exception e) {
e.printStackTrace();
}
}
public void getStatus(Response response) {
if (response.getCommandStatus() == Data.ESME_ROK) {
//
} else {
}
}
public void setSmscIP(String smscIP) {
this.smscIP = smscIP;
}
public void setSmscPort(int smscPort) {
this.smscPort = smscPort;
}
public void setSysId(String sysId) {
this.sysId = sysId;
}
public void setSyspwd(String syspwd) {
this.syspwd = syspwd;
}
}