package cn.net.caoyao.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.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.service.bll.ProductBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import android.os.Handler;
import android.os.Message;


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

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

	@Override
	public void run() {
		SocketCreate create = new SocketCreate("218.245.0.109", 8890, "");
		try {
			LogUtil.d(TConfig.Echo, "登录"+deviceId);
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS);
				msg.obj = "109 连接成功";
				mHandler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				msg.arg1 = create.getiErrCode();
				msg.obj = "109连接成功:"+create.getErrMsg();
				mHandler.sendMessage(msg);
			}
			
		} catch (BindException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "109 连接失败";
			mHandler.sendMessage(msg);
		} catch (ConnectException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "109 连接失败";
			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 = "109 请求超时";
			mHandler.sendMessage(msg);
		} catch (UnknownHostException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "109 连接失败";
			mHandler.sendMessage(msg);
		} catch (IOException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "109 连接失败";
			mHandler.sendMessage(msg);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
	}



}
