Commit 03b4127d13fc78263188bd009061fca3e85d944d

Authored by 钟易宏
1 parent 392bd186

remove ReadThread.release

@@ -33,10 +33,12 @@ import java.io.OutputStream; @@ -33,10 +33,12 @@ import java.io.OutputStream;
33 import java.lang.ref.WeakReference; 33 import java.lang.ref.WeakReference;
34 import java.net.Socket; 34 import java.net.Socket;
35 import java.util.Arrays; 35 import java.util.Arrays;
  36 +import java.util.concurrent.atomic.AtomicBoolean;
36 37
37 import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR; 38 import static com.qnbar.smc.service.SocketService.JUST_OPEN_DOOR;
38 import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR; 39 import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR;
39 import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR_AND_GET_MOVIE; 40 import static com.qnbar.smc.service.SocketService.USER_OPEN_DOOR_AND_GET_MOVIE;
  41 +import java.util.concurrent.atomic.AtomicBoolean;
40 42
41 public class SocketService1 extends BaseService { 43 public class SocketService1 extends BaseService {
42 private static final String TAG = "BackService1"; 44 private static final String TAG = "BackService1";
@@ -104,7 +106,7 @@ public class SocketService1 extends BaseService { @@ -104,7 +106,7 @@ public class SocketService1 extends BaseService {
104 // For heart Beat 106 // For heart Beat
105 private Handler mHandler = new Handler(); 107 private Handler mHandler = new Handler();
106 108
107 - boolean sendRegister = false; 109 + AtomicBoolean sendRegister = new AtomicBoolean(false);
108 110
109 private Runnable heartBeatRunnable = new Runnable() { 111 private Runnable heartBeatRunnable = new Runnable() {
110 112
@@ -115,10 +117,7 @@ public class SocketService1 extends BaseService { @@ -115,10 +117,7 @@ public class SocketService1 extends BaseService {
115 boolean isSuccess = sendMsg(new Gson().toJson(new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL);//就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket 117 boolean isSuccess = sendMsg(new Gson().toJson(new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL);//就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket
116 if (!isSuccess) { 118 if (!isSuccess) {
117 LogUtils.d(TAG, "heart beat error restart"); 119 LogUtils.d(TAG, "heart beat error restart");
118 - mHandler.removeCallbacks(heartBeatRunnable);  
119 - mReadThread.release();  
120 - sendRegister = false;  
121 - releaseLastSocket(); 120 + clearConnect();
122 new InitSocketThread().start(); 121 new InitSocketThread().start();
123 } 122 }
124 } 123 }
@@ -200,7 +199,22 @@ public class SocketService1 extends BaseService { @@ -200,7 +199,22 @@ public class SocketService1 extends BaseService {
200 } 199 }
201 200
202 ///begin socket operation 201 ///begin socket operation
203 - public synchronized boolean sendMsg(String msg) { 202 + private synchronized void initSocket() throws IOException {//初始化Socket
  203 + LogUtils.d(TAG, "serverHost:serverPort:" + serverHost + ":" + serverPort);
  204 + mHandler.post(new Runnable() {
  205 + @Override
  206 + public void run() {
  207 + CToast.makeText(context, "开始连接服务器", 10 * 1000).show();
  208 + }
  209 + });
  210 + Socket so = new Socket(serverHost, serverPort);
  211 + mSocket = new WeakReference<>(so);
  212 + readingMsg.setLength(0);
  213 + mReadThread = new ReadThread();
  214 + mReadThread.start();
  215 + }
  216 +
  217 + private synchronized boolean sendMsg(String msg) {
204 if (null == mSocket || null == mSocket.get()) { 218 if (null == mSocket || null == mSocket.get()) {
205 return false; 219 return false;
206 } 220 }
@@ -224,20 +238,6 @@ public class SocketService1 extends BaseService { @@ -224,20 +238,6 @@ public class SocketService1 extends BaseService {
224 return true; 238 return true;
225 } 239 }
226 240
227 - private synchronized void initSocket() throws IOException {//初始化Socket  
228 - LogUtils.d(TAG, "serverHost:serverPort:" + serverHost + ":" + serverPort);  
229 - mHandler.post(new Runnable() {  
230 - @Override  
231 - public void run() {  
232 - CToast.makeText(context, "开始连接服务器", 10 * 1000).show();  
233 - }  
234 - });  
235 - Socket so = new Socket(serverHost, serverPort);  
236 - mSocket = new WeakReference<>(so);  
237 - readingMsg.setLength(0);  
238 - mReadThread = new ReadThread();  
239 - mReadThread.start();  
240 - }  
241 241
242 private synchronized void releaseLastSocket() { 242 private synchronized void releaseLastSocket() {
243 try { 243 try {
@@ -285,6 +285,12 @@ public class SocketService1 extends BaseService { @@ -285,6 +285,12 @@ public class SocketService1 extends BaseService {
285 } 285 }
286 return false; 286 return false;
287 } 287 }
  288 +
  289 + private synchronized void clearConnect() {
  290 + mHandler.removeCallbacks(heartBeatRunnable);
  291 + sendRegister.set(false);
  292 + releaseLastSocket();
  293 + }
288 ///end socket operation 294 ///end socket operation
289 295
290 class InitSocketThread extends Thread { 296 class InitSocketThread extends Thread {
@@ -309,170 +315,138 @@ public class SocketService1 extends BaseService { @@ -309,170 +315,138 @@ public class SocketService1 extends BaseService {
309 315
310 // Thread to read content from Socket 316 // Thread to read content from Socket
311 class ReadThread extends Thread { 317 class ReadThread extends Thread {
312 -// private WeakReference<Socket> mWeakSocket;  
313 - private boolean isStart = true;  
314 -  
315 -// public ReadThread() {  
316 -//// mWeakSocket = new WeakReference<Socket>(socket);  
317 -// }  
318 -  
319 - public void release() {  
320 - isStart = false;  
321 - releaseLastSocket();  
322 - }  
323 318
324 @Override 319 @Override
325 public void run() { 320 public void run() {
326 super.run(); 321 super.run();
327 -// Socket socket = mSocket.get();  
328 -// if (null != socket) {  
329 -// try {  
330 - if (!sendRegister) {  
331 - LogUtils.d(TAG, "send register mes");  
332 - SocketSendMsg ssm = new SocketSendMsg().contractRegisterMsg(testRoomSn);  
333 - String msg = gson.toJson(ssm) + END_SYMBOL;  
334 - sendMsg(msg);  
335 - LogUtils.d(TAG, "" + msg);  
336 - sendRegister = true; 322 + if (!sendRegister.get()) {
  323 + LogUtils.d(TAG, "send register mes");
  324 + SocketSendMsg ssm = new SocketSendMsg().contractRegisterMsg(testRoomSn);
  325 + String msg = gson.toJson(ssm) + END_SYMBOL;
  326 + if (!sendMsg(msg)) {
  327 + clearConnect();
  328 + return;
337 } 329 }
338 - LogUtils.d(TAG, "send register mes end");  
339 -// InputStream is = socket.getInputStream();  
340 -// byte[] buffer = new byte[1024 * 4];  
341 -// int length = 0;  
342 -// while (!socket.isClosed() && !socket.isInputShutdown()  
343 -// && isStart && ((length = is.read(buffer)) != -1)) {  
344 -// if (length > 0) {  
345 -// }  
346 -// }  
347 - StringBuilder message = new StringBuilder();  
348 - while (isStart && recvMsg(message)) {  
349 -// if (!message.contains("9997")) {  
350 - LogUtils.d(TAG, "recv msg:" + message);  
351 -// } else {  
352 -// LogUtils.d(TAG, "heat beat success");  
353 -// }  
354 - try {  
355 - SocketResponse socketResponse = gson.fromJson(message.toString(),  
356 - SocketResponse.class);  
357 - switch (socketResponse.getCode()) {  
358 - case SUCCESS_MESSAGE:  
359 - Log.d(TAG, "SUCCESS_MESSAGE");  
360 - break;  
361 - case VERIFY_SUCCESS:  
362 - LogUtils.d(TAG, "VERIFY_SUCCESS");  
363 - mHandler.post(heartBeatRunnable);  
364 - LogUtils.d(TAG, "verify success start heart beat");  
365 - break;  
366 - case HEART_BEAT_SUCCESS:  
367 - //每成功5次心跳判定是否启动main activity  
368 - if (++mainChargeCount == 5) {  
369 - mainChargeCount = 0;  
370 - if (ActivityCollector.getActivity(MainActivity.class) == null) {  
371 - LogUtils.d(TAG, "charge start main activity");  
372 - mHandler.postDelayed(startMainRunnable, 3 * 1000);  
373 - }  
374 - }  
375 - boolean serviceLaunched = ActivityCollector.getActivity(MainActivity.class) != null;  
376 - Log.d(TAG, "HEART_BEAT_SUCCESS,MainActivity Launched:" + serviceLaunched);  
377 - break;  
378 - case HEART_BEAT_ERROR_SYMBOL:  
379 - case ROOM_SN_CONNECTED:  
380 - String msg1 = socketResponse.getCode() == HEART_BEAT_ERROR_SYMBOL ? "HEART_BEAT_ERROR_SYMBOL" : "ROOM_SN_CONNECTED";  
381 - LogUtils.d(TAG, msg1);  
382 - mHandler.removeCallbacks(heartBeatRunnable);  
383 - mReadThread.release();  
384 - sendRegister = false;  
385 - releaseLastSocket();  
386 - LogUtils.d(TAG, msg1 + " before:" + +System.currentTimeMillis());  
387 - try {  
388 - Thread.sleep(10 * 1000);  
389 - } catch (InterruptedException e) {  
390 - e.printStackTrace();  
391 - }  
392 - LogUtils.d(TAG, msg1 + " after:" + System.currentTimeMillis());  
393 - new InitSocketThread().start();  
394 - break;  
395 - case RETURN_VERIFY_CODE:  
396 - LogUtils.d(TAG, "RETURN_VERIFY_CODE");  
397 - if (!TextUtils.isEmpty(socketResponse.getData().getVerify())) {  
398 - String verifyMsg = AuthCode.getDecodeStr(socketResponse.getData().getVerify());  
399 - SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, verifyMsg);  
400 - String msg = gson.toJson(ssm) + END_SYMBOL;  
401 - sendMsg(msg); 330 + LogUtils.d(TAG, "" + msg);
  331 + sendRegister.set(true);
  332 + }
  333 + LogUtils.d(TAG, "send register mes end");
  334 + StringBuilder message = new StringBuilder();
  335 + while (recvMsg(message)) {
  336 + LogUtils.d(TAG, "recv msg:" + message);
  337 + try {
  338 + SocketResponse socketResponse = gson.fromJson(message.toString(),
  339 + SocketResponse.class);
  340 + switch (socketResponse.getCode()) {
  341 + case SUCCESS_MESSAGE:
  342 + Log.d(TAG, "SUCCESS_MESSAGE");
  343 + break;
  344 + case VERIFY_SUCCESS:
  345 + LogUtils.d(TAG, "VERIFY_SUCCESS");
  346 + mHandler.post(heartBeatRunnable);
  347 + LogUtils.d(TAG, "verify success start heart beat");
  348 + break;
  349 + case HEART_BEAT_SUCCESS:
  350 + //每成功5次心跳判定是否启动main activity
  351 + if (++mainChargeCount == 5) {
  352 + mainChargeCount = 0;
  353 + if (ActivityCollector.getActivity(MainActivity.class) == null) {
  354 + LogUtils.d(TAG, "charge start main activity");
  355 + mHandler.postDelayed(startMainRunnable, 3 * 1000);
402 } 356 }
403 - break;  
404 - case CONTAIN_MESSAGE:  
405 - LogUtils.d(TAG, "CONTAIN_MESSAGE");  
406 - if (socketResponse.getData() != null) {  
407 - if (socketResponse.getCmd() == OPEN_DOOR) {  
408 - switch (socketResponse.getData().getUser()) {  
409 - //10用户,20管理员,默认值为0  
410 - case 10:  
411 - new SystemUtils().openFtLed(context.getApplicationContext());  
412 - if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {  
413 - LightOperationUtils.open();  
414 - LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));  
415 - }  
416 - sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when new user come in");  
417 - if (socketResponse.getData().getFirst() == 1) {  
418 - sendMessage(USER_OPEN_DOOR_AND_GET_MOVIE, "user first open the door");  
419 - } else {  
420 - sendMessage(USER_OPEN_DOOR, "user open the door");  
421 - }  
422 - break;  
423 - case 20:  
424 - if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {  
425 - LogUtils.d("LightOperationUtils", "admin open light");  
426 - LightOperationUtils.open();  
427 - LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));  
428 - }  
429 - sendMessage(JUST_OPEN_DOOR, "administrator open the door");  
430 - break;  
431 - case 0:  
432 - sendMessage(JUST_OPEN_DOOR, "get zero none user or administrator open the door");  
433 - break;  
434 - default:  
435 - sendMessage(JUST_OPEN_DOOR, "none user or administrator open the door");  
436 - break;  
437 - }  
438 - } else if (socketResponse.getCmd() == CLEAN_OVER) {  
439 - LogUtils.d("LightOperationUtils", "admin clean over close light");  
440 - LightOperationUtils.setLightValue(5);  
441 - LightOperationUtils.close();  
442 - new SystemUtils().closeFtLed(context.getApplicationContext());  
443 - sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when clean over");  
444 - if (ActivityCollector.isActivityExist(SimpleAdsPlayer2.class)) {  
445 - ActivityCollector.getActivity(SimpleAdsPlayer2.class).finish();  
446 - }  
447 - if (ActivityCollector.isActivityExist(AdsPreVideoPlayerActivity.class)) {  
448 - ActivityCollector.getActivity(AdsPreVideoPlayerActivity.class).finish();  
449 - }  
450 - if (ActivityCollector.isActivityExist(QrCodeShowActivity.class)) {  
451 - ActivityCollector.getActivity(QrCodeShowActivity.class).finish();  
452 - }  
453 - try {  
454 - PollingUtils.stopPollingService(context, CountService.class, CountService.STATUS_ACTION);  
455 - } catch (Exception e) {  
456 - e.printStackTrace();  
457 - } 357 + }
  358 + boolean serviceLaunched = ActivityCollector.getActivity(MainActivity.class) != null;
  359 + Log.d(TAG, "HEART_BEAT_SUCCESS,MainActivity Launched:" + serviceLaunched);
  360 + break;
  361 + case HEART_BEAT_ERROR_SYMBOL:
  362 + case ROOM_SN_CONNECTED:
  363 + String msg1 = socketResponse.getCode() == HEART_BEAT_ERROR_SYMBOL ? "HEART_BEAT_ERROR_SYMBOL" : "ROOM_SN_CONNECTED";
  364 + LogUtils.d(TAG, msg1);
  365 + clearConnect();
  366 + LogUtils.d(TAG, msg1 + " before:" + +System.currentTimeMillis());
  367 + try {
  368 + Thread.sleep(10 * 1000);
  369 + } catch (InterruptedException e) {
  370 + e.printStackTrace();
  371 + }
  372 + LogUtils.d(TAG, msg1 + " after:" + System.currentTimeMillis());
  373 + new InitSocketThread().start();
  374 + break;
  375 + case RETURN_VERIFY_CODE:
  376 + LogUtils.d(TAG, "RETURN_VERIFY_CODE");
  377 + if (!TextUtils.isEmpty(socketResponse.getData().getVerify())) {
  378 + String verifyMsg = AuthCode.getDecodeStr(socketResponse.getData().getVerify());
  379 + SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, verifyMsg);
  380 + String msg = gson.toJson(ssm) + END_SYMBOL;
  381 + sendMsg(msg);
  382 + }
  383 + break;
  384 + case CONTAIN_MESSAGE:
  385 + LogUtils.d(TAG, "CONTAIN_MESSAGE");
  386 + if (socketResponse.getData() != null) {
  387 + if (socketResponse.getCmd() == OPEN_DOOR) {
  388 + switch (socketResponse.getData().getUser()) {
  389 + //10用户,20管理员,默认值为0
  390 + case 10:
  391 + new SystemUtils().openFtLed(context.getApplicationContext());
  392 + if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
  393 + LightOperationUtils.open();
  394 + LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
  395 + }
  396 + sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when new user come in");
  397 + if (socketResponse.getData().getFirst() == 1) {
  398 + sendMessage(USER_OPEN_DOOR_AND_GET_MOVIE, "user first open the door");
  399 + } else {
  400 + sendMessage(USER_OPEN_DOOR, "user open the door");
  401 + }
  402 + break;
  403 + case 20:
  404 + if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
  405 + LogUtils.d("LightOperationUtils", "admin open light");
  406 + LightOperationUtils.open();
  407 + LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
  408 + }
  409 + sendMessage(JUST_OPEN_DOOR, "administrator open the door");
  410 + break;
  411 + case 0:
  412 + sendMessage(JUST_OPEN_DOOR, "get zero none user or administrator open the door");
  413 + break;
  414 + default:
  415 + sendMessage(JUST_OPEN_DOOR, "none user or administrator open the door");
  416 + break;
  417 + }
  418 + } else if (socketResponse.getCmd() == CLEAN_OVER) {
  419 + LogUtils.d("LightOperationUtils", "admin clean over close light");
  420 + LightOperationUtils.setLightValue(5);
  421 + LightOperationUtils.close();
  422 + new SystemUtils().closeFtLed(context.getApplicationContext());
  423 + sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when clean over");
  424 + if (ActivityCollector.isActivityExist(SimpleAdsPlayer2.class)) {
  425 + ActivityCollector.getActivity(SimpleAdsPlayer2.class).finish();
  426 + }
  427 + if (ActivityCollector.isActivityExist(AdsPreVideoPlayerActivity.class)) {
  428 + ActivityCollector.getActivity(AdsPreVideoPlayerActivity.class).finish();
  429 + }
  430 + if (ActivityCollector.isActivityExist(QrCodeShowActivity.class)) {
  431 + ActivityCollector.getActivity(QrCodeShowActivity.class).finish();
  432 + }
  433 + try {
  434 + PollingUtils.stopPollingService(context, CountService.class, CountService.STATUS_ACTION);
  435 + } catch (Exception e) {
  436 + e.printStackTrace();
458 } 437 }
459 } 438 }
460 - break;  
461 - default:  
462 - LogUtils.d(TAG, "default msg:" + socketResponse.toString());  
463 - }  
464 - } catch (JsonSyntaxException e) {  
465 - LogUtils.d(TAG, "error" + message + e.getMessage());  
466 - e.printStackTrace(); 439 + }
  440 + break;
  441 + default:
  442 + LogUtils.d(TAG, "default msg:" + socketResponse.toString());
467 } 443 }
468 - LogUtils.d(TAG, "recv msg:" + message); 444 + } catch (JsonSyntaxException e) {
  445 + LogUtils.d(TAG, "error" + message + e.getMessage());
  446 + e.printStackTrace();
469 } 447 }
470 -// }  
471 -// catch (IOException e) {  
472 -// LogUtils.d(TAG, "error" + e.getCause());  
473 -// e.printStackTrace();  
474 -// }  
475 -// } 448 + LogUtils.d(TAG, "recv msg:" + message);
  449 + }
476 } 450 }
477 } 451 }
478 452
Please register or login to post a comment