China Telecom SMS(SGIP)

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Properties;

import sgip.tool.Bind;
import sgip.tool.BindResp;
import sgip.tool.SGIP_Command;
import sgip.tool.Submit;
import sgip.tool.SubmitResp;
import sgip.tool.Unbind;

import com.xx.entity.Message;
import com.xx.util.MessageUtil;

public class ChinaTelecomConnector implements Connector{
	public static final String UCS2 = "ISO-10646-UCS-2";
	
	private static final String ip = "SERVERIP";
	private static final String port = "PORT";
	private static final String username = "USERNAME";
	private static final String password = "PASSWORD";
	private static final String companypid = "COMPANYPID";
	private static final String spid = "SPID";
	
	private Socket socket = null;
	private String serverIp;
	private int serverPort;
	private String loginUsername;
	private String loginPassword;
	private Long loginCompanyId;
	private String loginSPID;
	
	private String chargerNumber;
	
	//public ChinaTelecomConnector(){}
	//由spring注入一些参数
	public ChinaTelecomConnector(Properties properties){
		intSocketParamters(properties);
	}
	
	
	private  void intSocketParamters(Properties properties){
		if(properties.containsKey(ip)){
			serverIp = (String) properties.get(ip);
		}
		if(properties.containsKey(port)){
			serverPort = Integer.parseInt((String) properties.get(port));
		}
		if(properties.containsKey(username)){
			loginUsername = (String) properties.get(username);
		}
		if(properties.containsKey(password)){
			loginPassword = properties.getProperty(password);
		}
		
		if(properties.containsKey(companypid)){
			loginCompanyId = Long.parseLong((String) properties.get(companypid));
		}
		
		if(properties.containsKey(spid)){
			loginSPID = (String) properties.get(spid);
			chargerNumber = loginSPID;
		}
		
		//socket = new Socket(serverIp, serverPort)
	}
	
	
	/**
	 * 每一个Submit提交最后可以提交140字节,也是70个汉字,如果发送的是普通短信,TP_udhi为0,如果为长短信tp_udhi为1
	 * 发送长短信时,要将短信息UCS2编码,然后拆分成 67个字,也就是134个字段为一条短信用submit提交,因为还有6个字节要加th_udhiHead做为长短信的标识
	 * 这里结果,设为只要成功发送长信息中的一条短信成功,就返true
	 */
	@Override
	public  boolean sendSms(Message message,int count,String number) {
		// TODO Auto-generated method stub
		boolean bool = false;
		try {
			System.out.println(chargerNumber + " - " + loginSPID);
			//String sendContent = changeCharset(message.getContent(),GBK);
			String sendContent = message.getContent();
			socket = new Socket(serverIp, serverPort);
			OutputStream out = socket.getOutputStream();
			InputStream input = socket.getInputStream();
			SGIP_Command sgipCommand = new SGIP_Command();
			SGIP_Command tmp = null;
			BindResp bindResp = null;
			Submit submit = null;
			SubmitResp submitResp = null;
			Unbind unbind = null;
			
			//Step 1 -- Bind
			Bind bind = new Bind(Long.parseLong(loginSPID),1, loginUsername, loginPassword);
			bind.write(out);
			
			tmp = sgipCommand.read(input);
			if(tmp.getCommandID() == SGIP_Command.ID_SGIP_BIND_RESP){
				bindResp = (BindResp) tmp;
				bindResp.readbody();
				if(bindResp.GetResult() == 0){
					
				}
			}
			
			//Step 2 Submit
			//System.out.println("Content Length " + sendContent.length() );
			String[] messages = MessageUtil.splitMessage(sendContent);
			submit = new Submit(Long.parseLong(loginSPID),// node id
					loginSPID,//########################################SPNumber
					chargerNumber,//###############################################ChargeNumbers
					count,//############################################UserCount
					number,//###########################################UserNumbers
					loginCompanyId+"",//#######################################CorporateId corpId
					"",//##########################################ServiceType
					0,//################################################FeeType
					"0",//##############################################FeeValue
					"0",//##############################################GivenValue
					0,//################################################AgentFlag
					0,//################################################MorelatetoMTFlag
					0,//################################################Priority
					"",//###############################################ExpireTime
					"",//###############################################ScheduleTime
					1,//################################################ReportFlag
					0,//################################################TP_pid
					messages.length > 1 ? 1 : 0,//######################TP_udhi
					8,//################################################MessageCoding
					0,//################################################MessageType
					sendContent.length(),//####################MessageLength 最后两个字段的内容可以说是多余的,真正发送信息的是submit.setContent(8,ucs2String)发法
					sendContent//##############################MessageContent
					);
			//submit.setBinContent(8, sendContent.getBytes());
			
			
			byte[] allContentByte = sendContent.getBytes("ISO-10646-UCS-2");
			//System.out.println("byte Length " + allContentByte.length);
			int maxMsgLen = 140;
			
			for(int i=0;i<messages.length;i++){
				byte[] shortMessageUCSE = messages[i].getBytes("ISO-10646-UCS-2");
				byte[] tp_udhiHead = getTPUDHIHead(messages.length, i+1);// Get Tp_udhiHead
				byte[] contentBytes = null;
				if(messages.length > 1){
					//contentBytes = addByte(shortMessageUCSE, tp_udhiHead);
					//拆分短信字节和tp_udhiHead拼成submit需要提交字节,然后把转成字条串,再调用submit.setContent方法
					contentBytes = getSpliteLongMessageBytes(tp_udhiHead,allContentByte,i*(maxMsgLen - 6),(i+1)*(maxMsgLen-6));
				}else{
					contentBytes = shortMessageUCSE;
				}
				String ucs2String = new String(contentBytes,"ISO8859-1");
				submit.setContent(8,ucs2String);
				submit.write(out);
				out.flush();
				tmp = sgipCommand.read(input);
				if(tmp.getCommandID() == SGIP_Command.ID_SGIP_SUBMIT_RESP){
					submitResp = (SubmitResp) tmp;
					submitResp.readbody();
					if(submitResp.getResult() == 0){
						bool = true;//Successful Send
					}
					/*else{
						System.out.println(submitResp.getResult());
						bool = false;
					}*/
				}
			}
			
			//Step 3 Unbind
			unbind = new Unbind(Long.parseLong(loginSPID));
			unbind.write(out);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return bool;
	}

	@Override
	public String getResponse() {return null;}

	
	public static String changeCharset(String str, String newCharset) {
		String returnStr = null;
		try {
			if (str != null) {
				byte[] bs = str.getBytes();
				returnStr =  new String(bs, newCharset);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnStr;
	}
	
	public void setLoginSPID(String loginSPID) {
		this.loginSPID = loginSPID;
	}

	public void setChargerNumber(String chargerNumber) {
		this.chargerNumber = chargerNumber;
	}
	
	
	public static byte[] getSpliteLongMessageBytes(byte[] tpUdhiHead,byte[] messageUCS2 ,int start,int end){
		if(end > messageUCS2.length){
			end = messageUCS2.length; 
		}
		byte[] msgb = new byte[end - start + 6];
		System.arraycopy(tpUdhiHead, 0, msgb, 0, 6);
		System.arraycopy(messageUCS2, start, msgb, 6, end - start);
		return msgb;
		
	}
	
	public static byte[] getTPUDHIHead(int messageLength,int number){
		byte[] tp_udhiHead = new byte[6];
		tp_udhiHead[0] = 0x05;
		tp_udhiHead[1] = 0x00;
		tp_udhiHead[2] = 0x03;
		tp_udhiHead[3] = 0x0A;
		tp_udhiHead[4] = (byte)messageLength;//长短信总长度
		tp_udhiHead[5] = (byte)number;//第几条短信
		return tp_udhiHead;
	}
	
	public static byte[] addByte(byte[] tp_udhiHead,byte[] content){
		int allLength = tp_udhiHead.length + content.length;
		byte[] udhi = new byte[allLength];
		for(int i = 0;i< tp_udhiHead.length;i++){
			udhi[i] = tp_udhiHead[i];
		}
		for(int j=0;j<content.length;j++){
			udhi[tp_udhiHead.length+j] = content[j];
		}
		return udhi;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学英语的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值