|
@@ -0,0 +1,153 @@
|
|
|
+package com.nokia.sms.blacklist;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.net.Socket;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+
|
|
|
+import com.nokia.sms.blacklist.exception.ConnectFailedException;
|
|
|
+import com.nokia.sms.blacklist.exception.PhoneNumberCanNotBeNullException;
|
|
|
+import com.nokia.sms.blacklist.message.ClientMessageUtil;
|
|
|
+import com.nokia.sms.blacklist.message.ClientParseUtil;
|
|
|
+import com.nokia.sms.blacklist.message.entity.BindRespStatus;
|
|
|
+import com.nokia.sms.blacklist.message.entity.DelBlkBody;
|
|
|
+import com.nokia.sms.blacklist.message.entity.DelBlkRespStatus;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+
|
|
|
+ * 短信一键解封客户端
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class BlkDelClient {
|
|
|
+ private Socket socket;
|
|
|
+ private InputStream inputStream;
|
|
|
+ private OutputStream outputStream;
|
|
|
+
|
|
|
+ private boolean connected = false;
|
|
|
+
|
|
|
+ private long sequenceNumber;
|
|
|
+
|
|
|
+
|
|
|
+ private BlkDelClient() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 对外提供服务 删除黑名单
|
|
|
+ */
|
|
|
+ public void delBlk(String phoneNumber)
|
|
|
+ throws IOException, NoSuchAlgorithmException, ConnectFailedException, PhoneNumberCanNotBeNullException {
|
|
|
+ log.debug("开始删除短信黑名单,号码 {} ", phoneNumber);
|
|
|
+
|
|
|
+
|
|
|
+ if (!connected) {
|
|
|
+
|
|
|
+ connect();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ stopHeartbeat();
|
|
|
+
|
|
|
+ doDelBlk(phoneNumber);
|
|
|
+
|
|
|
+ startHeartbeat();
|
|
|
+ }
|
|
|
+
|
|
|
+ private DelBlkRespStatus doDelBlk(String phoneNumber) throws PhoneNumberCanNotBeNullException, IOException {
|
|
|
+ DelBlkBody delBlkBody = new DelBlkBody(phoneNumber);
|
|
|
+
|
|
|
+ byte[] smitDelBlk = ClientMessageUtil.getSmitDelBlkMessage(delBlkBody);
|
|
|
+ outputStream.write(smitDelBlk);
|
|
|
+ outputStream.flush();
|
|
|
+ log.debug("已发送绑定请求消息 SMIT_BIND");
|
|
|
+
|
|
|
+ byte[] resp = new byte[128];
|
|
|
+ int count = inputStream.read(resp);
|
|
|
+ log.debug("收到服务器回复...长度 {} 字节", count);
|
|
|
+
|
|
|
+ return ClientParseUtil.parseSmitDelBlkRespMessage(resp);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 连接到服务器
|
|
|
+ */
|
|
|
+ private void connect() throws IOException, NoSuchAlgorithmException, ConnectFailedException {
|
|
|
+ socket = new Socket();
|
|
|
+ InetAddress address = InetAddress.getByName(BlkConfig.getConfig("blkServer.ipAddress"));
|
|
|
+ int port = Integer.parseInt(BlkConfig.getConfig("blkServer.port"));
|
|
|
+ int connectTimeout = Integer.parseInt(BlkConfig.getConfig("blkServer.connectTimeout"));
|
|
|
+
|
|
|
+ socket.connect(new InetSocketAddress(address, port), connectTimeout);
|
|
|
+
|
|
|
+ inputStream = socket.getInputStream();
|
|
|
+ outputStream = socket.getOutputStream();
|
|
|
+
|
|
|
+ byte[] smitBind = ClientMessageUtil.getSmitBindMessage();
|
|
|
+ outputStream.write(smitBind);
|
|
|
+ outputStream.flush();
|
|
|
+ log.debug("已发送绑定请求消息 SMIT_BIND");
|
|
|
+
|
|
|
+ byte[] resp = new byte[128];
|
|
|
+ int count = inputStream.read(resp);
|
|
|
+ log.debug("收到服务器回复...长度 {} 字节", count);
|
|
|
+
|
|
|
+ BindRespStatus status = ClientParseUtil.parseSmitBindRespMessage(resp);
|
|
|
+ if (BindRespStatus.SUCCESS.equals(status)) {
|
|
|
+ connected = true;
|
|
|
+ log.info("服务器连接成功!!!");
|
|
|
+ } else {
|
|
|
+ log.error("服务器连接失败,错误码: {}", status);
|
|
|
+ throw new ConnectFailedException("连接失败,状态码 : " + status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 关闭连接
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private void close() throws IOException {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ inputStream = null;
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ outputStream = null;
|
|
|
+ }
|
|
|
+ if (socket != null) {
|
|
|
+ socket.close();
|
|
|
+ socket = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 停止心跳
|
|
|
+ */
|
|
|
+ private void stopHeartbeat() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 开启心跳
|
|
|
+ */
|
|
|
+ private void startHeartbeat() {
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ int count = 1000;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getNextSequence() {
|
|
|
+ if (sequenceNumber < 4294967295L) {
|
|
|
+ return sequenceNumber + 1L;
|
|
|
+ } else {
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|