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.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.jinLiquor.Jin20005ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.jinLiquor.Jin20005ResBean;
import cn.net.cpzslibs.prot.handset.jinLiquor.Jin20005Login;
import android.os.Handler;
import android.os.Message;


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

	public TestJinThread(Handler mHandler) {
		this.mHandler = mHandler;
		//
	}

	@Override
	public void run() {
		String ip = "zs-wms.jingpai.com";
		SocketCreate create = new SocketCreate(ip, 35251, "");
		try {
//			LogUtil.d(TConfig.Echo, "登录"+deviceId);
			create.createJustCreateSocket();
			Jin20005Login l = new Jin20005Login();
//			l.login(create, new Jin20005ReqBean("101001", "e10adc3949ba59abbe56e057f20f883e"));
			l.login(create, new Jin20005ReqBean("1", "1"),"1");
			Jin20005ResBean  resBean = l.getResBean();
			
			if (resBean == null) {
				Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS);
				msg.obj = "jinTest 服务器无响应";
				mHandler.sendMessage(msg);
				return;
			} 
			
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS);
			msg.obj = "Jin连接成功";
			mHandler.sendMessage(msg);
			
		} catch (BindException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765 连接失败";
			mHandler.sendMessage(msg);
		} catch (ConnectException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765 连接失败";
			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 = "108 - 8765 请求超时";
			mHandler.sendMessage(msg);
		} catch (UnknownHostException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765连接失败";
			mHandler.sendMessage(msg);
		} catch (IOException e) {
			e.printStackTrace();
//			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "108 - 8765 连接失败";
			mHandler.sendMessage(msg);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
	}



}
