|
@@ -6,13 +6,12 @@ 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.ConnectionOutOfSyncException;
|
|
|
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;
|
|
@@ -32,15 +31,25 @@ public class BlkDelClient {
|
|
|
// 0 - 4294967295 0xFFFFFFFF
|
|
|
private long sequenceNumber;
|
|
|
|
|
|
+ private String systemId = "smsTousu";
|
|
|
+ private String password = "123456";
|
|
|
+
|
|
|
// 限制产生多个对象
|
|
|
private BlkDelClient() {
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args)
|
|
|
+ throws NoSuchAlgorithmException, IOException, ConnectFailedException, ConnectionOutOfSyncException {
|
|
|
+ BlkDelClient blkDelClient = new BlkDelClient();
|
|
|
+ blkDelClient.connect();
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
* 对外提供服务 删除黑名单
|
|
|
*/
|
|
|
public void delBlk(String phoneNumber)
|
|
|
- throws IOException, NoSuchAlgorithmException, ConnectFailedException, PhoneNumberCanNotBeNullException {
|
|
|
+ throws IOException, NoSuchAlgorithmException, ConnectFailedException, PhoneNumberCanNotBeNullException,
|
|
|
+ ConnectionOutOfSyncException {
|
|
|
log.debug("开始删除短信黑名单,号码 {} ", phoneNumber);
|
|
|
|
|
|
// 1. 检查连接状态
|
|
@@ -60,7 +69,7 @@ public class BlkDelClient {
|
|
|
private DelBlkRespStatus doDelBlk(String phoneNumber) throws PhoneNumberCanNotBeNullException, IOException {
|
|
|
DelBlkBody delBlkBody = new DelBlkBody(phoneNumber);
|
|
|
// 发送消息
|
|
|
- byte[] smitDelBlk = ClientMessageUtil.getSmitDelBlkMessage(delBlkBody);
|
|
|
+ byte[] smitDelBlk = ClientMessageUtil.getSmitDelBlkMessage(delBlkBody, getNextSequence());
|
|
|
outputStream.write(smitDelBlk);
|
|
|
outputStream.flush();
|
|
|
log.debug("已发送绑定请求消息 SMIT_BIND");
|
|
@@ -69,13 +78,14 @@ public class BlkDelClient {
|
|
|
int count = inputStream.read(resp);
|
|
|
log.debug("收到服务器回复...长度 {} 字节", count);
|
|
|
// 解析消息
|
|
|
- return ClientParseUtil.parseSmitDelBlkRespMessage(resp);
|
|
|
+ return ClientMessageUtil.parseSmitDelBlkRespMessage(resp);
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
* 连接到服务器
|
|
|
*/
|
|
|
- private void connect() throws IOException, NoSuchAlgorithmException, ConnectFailedException {
|
|
|
+ private void connect()
|
|
|
+ throws IOException, NoSuchAlgorithmException, ConnectFailedException, ConnectionOutOfSyncException {
|
|
|
socket = new Socket();
|
|
|
InetAddress address = InetAddress.getByName(BlkConfig.getConfig("blkServer.ipAddress"));
|
|
|
int port = Integer.parseInt(BlkConfig.getConfig("blkServer.port"));
|
|
@@ -86,7 +96,7 @@ public class BlkDelClient {
|
|
|
inputStream = socket.getInputStream();
|
|
|
outputStream = socket.getOutputStream();
|
|
|
// 向客户端发送 绑定请求
|
|
|
- byte[] smitBind = ClientMessageUtil.getSmitBindMessage();
|
|
|
+ byte[] smitBind = ClientMessageUtil.getSmitBindMessage(systemId, password);
|
|
|
outputStream.write(smitBind);
|
|
|
outputStream.flush();
|
|
|
log.debug("已发送绑定请求消息 SMIT_BIND");
|
|
@@ -95,10 +105,11 @@ public class BlkDelClient {
|
|
|
int count = inputStream.read(resp);
|
|
|
log.debug("收到服务器回复...长度 {} 字节", count);
|
|
|
// 解析消息
|
|
|
- BindRespStatus status = ClientParseUtil.parseSmitBindRespMessage(resp);
|
|
|
+ BindRespStatus status = ClientMessageUtil.parseSmitBindRespMessage(resp);
|
|
|
if (BindRespStatus.SUCCESS.equals(status)) {
|
|
|
connected = true;
|
|
|
log.info("服务器连接成功!!!");
|
|
|
+ startHeartbeat();
|
|
|
} else {
|
|
|
log.error("服务器连接失败,错误码: {}", status);
|
|
|
throw new ConnectFailedException("连接失败,状态码 : " + status);
|
|
@@ -134,11 +145,33 @@ public class BlkDelClient {
|
|
|
/*
|
|
|
* 开启心跳
|
|
|
*/
|
|
|
- private void startHeartbeat() {
|
|
|
+ private void startHeartbeat() throws ConnectionOutOfSyncException {
|
|
|
// 一旦开启需要一直发送
|
|
|
while (true) {
|
|
|
- int count = 1000;
|
|
|
-
|
|
|
+ try {
|
|
|
+ // 间隔3000ms
|
|
|
+ Thread.sleep(3000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送心跳消息
|
|
|
+ long sequenceNumberSend = getNextSequence();
|
|
|
+ byte[] smitActiveTest = ClientMessageUtil.getSmitActiveTestMessage(sequenceNumberSend);
|
|
|
+ outputStream.write(smitActiveTest);
|
|
|
+ outputStream.flush();
|
|
|
+ // 读取心跳消息的返回
|
|
|
+ byte[] buffer = new byte[48];
|
|
|
+ int count = inputStream.read(buffer);
|
|
|
+ log.debug("接收到服务器返回的消息 {} 字节", count);
|
|
|
+ long sequenceNumberReceive = ClientMessageUtil.parseSmitActiveTestRespMessage(buffer);
|
|
|
+ if (sequenceNumberReceive != sequenceNumberSend) {
|
|
|
+ throw new ConnectionOutOfSyncException();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|