package cn.net.communal.cpzshandset.service.thread.impl.checkAvaiable;

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import cn.net.communal.cpzshandset.MyHandsetApp;
import cn.net.communal.cpzshandset.common.config.Config;
import cn.net.communal.cpzshandset.common.config.RequestConstant;
import cn.net.communal.cpzshandset.common.config.TConfig;
import cn.net.communal.cpzshandset.common.util.LogUtil;
import cn.net.communal.cpzshandset.service.bll.ProductBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;



/**
 * 链接服务器测试线程
 * @author zwj
 *
 * 2015-5-28
 */
public class TestThread implements Runnable{
	private long deviceId;
	private ProductBll bll;
	private Handler mHandler;

	public TestThread(Handler mHandler,long deviceId) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		//
	}

	@Override
	public void run() {
		int currentProt = Config.port % 100;
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录"+deviceId);
			
//			Log.e(TConfig.Echo, MyHandsetApp.getProt() + "port:" + Config.port);
			create.createLoginCpzsHandset(deviceId);
			
			if (create.isResult()) {
				Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS);
				msg.obj = currentProt+" 连接成功";
				mHandler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				msg.arg1 = create.getiErrCode();
				msg.obj = currentProt+"连接成功:"+create.getErrMsg();
				mHandler.sendMessage(msg);
			}
			
		} catch (BindException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = currentProt+"连接失败";
			mHandler.sendMessage(msg);
		} catch (ConnectException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = currentProt+" 连接失败";
			mHandler.sendMessage(msg);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
			msg.obj = currentProt+" 请求超时";
			mHandler.sendMessage(msg);
		} catch (UnknownHostException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = currentProt+" 连接失败";
			mHandler.sendMessage(msg);
		} catch (IOException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = currentProt+" 连接失败";
			mHandler.sendMessage(msg);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
	}



}
