登录 之 通讯机制(socket)续 --- 代码优化

因为代码的重用性,所以采用单实例的模式:

抽象接口(父类)ActionInterface

package com.net.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.net.convert.Security;
import com.net.ui.LandWindow;

public abstract class ActionInterface implements ActionListener {
	protected LandWindow landUI=null;
	
	protected String name;
	protected String pswd;
	
	protected String IP=null;
	protected int SERVERPORT=-1;
	protected String SPLITSTR=null;
	
	protected OutputStream os=null;
	protected InputStream is=null;
	protected String respondStyle="style";
	protected final String ISACK = "ACK";
	protected final String ISNAK = "NAK!";
	
	public ActionInterface(LandWindow landUI) {
		this.landUI=landUI;
	}
	
	public void actionPerformed(ActionEvent arg0) {
		landUI.getUserNameLabel().setText(null);
		landUI.getPasswdLabel().setText(null);
		landUI.getStatLabel().setText(null);
	}
	
	public void setIP(String ip){
		this.IP=ip;
	}
	
	public void setSplitStr(String splitStr){
		this.SPLITSTR=splitStr;
	}
	
	public void setServerPort(int port){
		this.SERVERPORT=port;
	}
	
	protected void start() { // 建立联网线程
		new Thread(new Runnable() {
			public void run() {
				Socket s = null;
				try {
					s = new Socket(IP, SERVERPORT);
					os = s.getOutputStream();
					os.write((name + SPLITSTR + pswd + SPLITSTR + respondStyle).getBytes());

					os.flush();
					// 读内容
					Thread.sleep(1000);
					is = s.getInputStream();
					int len = is.available();
					byte[] bytes = new byte[len];
					is.read(bytes);
					String result = new String(bytes);
					// TODO 这里通过返回结果处理
					String[] strs=result.split(SPLITSTR);
					String resultStatus=strs[0];
					String resultInfo=strs[1];
					if (ISACK.equals(resultStatus)){
						landUI.getPasswdField().setText(null);
						landUI.getStatLabel().setText("INFO:" + resultInfo + respondStyle + " successfully");
					} else if(ISNAK.equals(resultStatus)){ //验证成功后输出对应用户名
						landUI.getPasswdField().setText(null);
						landUI.getStatLabel().setText("ERROR:" + resultInfo);
					} else {
						landUI.getStatLabel().setText("ERROR:can not get respond from server");
					}
				} catch(ConnectException e){
					landUI.getStatLabel().setText("ERROR:Can not connect server");
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					try {
						if (os != null) os.close();
						if (is != null) is.close();
						if (s != null)	s.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	
	protected String getSecurityString(String str){
		Security security = new Security(str);
		security.securityAlgorithm("MD5");
		return security.getSecurityResult();
	}
}
// 响应之“重置”
package com.net.action;

import java.awt.event.ActionEvent;
import com.net.ui.LandWindow;

public class Cancle extends ActionInterface {
	
	public Cancle(LandWindow landUI) {
		super(landUI);
	}
	
	private void clearPanel() {
		this.landUI.getUserNameField().setText(null);
		this.landUI.getPasswdField().setText(null);
		this.landUI.getCertTextField().setText(null);
	}
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		super.actionPerformed(ae);
		this.clearPanel();
		this.landUI.refreshCertPicLabel();
	}

}

//响应之“登录”
package com.net.action;

import java.awt.event.ActionEvent;
import com.net.ui.LandWindow;

public class Submit extends ActionInterface{
	
	public Submit(LandWindow landUI) {
		super(landUI);
		this.respondStyle="land";
		this.landUI.refreshCertPicLabel();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		super.actionPerformed(arg0);
		
		String name = landUI.getUserNameField().getText();
		String pswd = new String(landUI.getPasswdField().getPassword());
		String certStr = landUI.getCertTextField().getText();
		boolean passFlag=true;
		if (name.length() == 0){
			landUI.getUserNameLabel().setText("用户名不能为空");
			passFlag=false;
		}
		if (pswd.length() == 0){
			landUI.getPasswdLabel().setText("密码不能为空");
			passFlag=false;
		}
		if (certStr.length() == 0){
			landUI.getStatLabel().setText("验证码不能为空");
			passFlag=false;
		}
		if(passFlag){
			if (certStr.equalsIgnoreCase(landUI.getCertPicStrValue())){
				this.name=name;
				this.pswd=getSecurityString(pswd);
				landUI.getStatLabel().setText("正在登录...");
				this.start();
			} else {
				landUI.getStatLabel().setText("验证码错误");
			}
		} else {
			landUI.refreshCertPicLabel();
		}
	}
}

//响应之“注册”
package com.net.action;

import java.awt.event.ActionEvent;
import com.net.ui.LandWindow;

public class Register extends ActionInterface{

	public Register(LandWindow landUI) {
		super(landUI);
		this.respondStyle="register";
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		super.actionPerformed(arg0);
		
		String name = landUI.getUserNameField().getText();
		String pswd = new String(landUI.getPasswdField().getPassword());
		String certStr = landUI.getCertTextField().getText();
		boolean passFlag=true;
		if (name.length() == 0){
			landUI.getUserNameLabel().setText("用户名不能为空");
			passFlag=false;
		}
		if (pswd.length() == 0){
			landUI.getPasswdLabel().setText("密码不能为空");
			passFlag=false;
		}
		if (certStr.length() == 0){
			landUI.getStatLabel().setText("验证码不能为空");
			passFlag=false;
		}
		if(passFlag){
			if (certStr.equalsIgnoreCase(landUI.getCertPicStrValue())){
				this.name=name;
				this.pswd=getSecurityString(pswd);
				landUI.getStatLabel().setText("提交信息中...");
				this.start();
			} else {
				landUI.getStatLabel().setText("验证码错误");
			}
		} else {
			landUI.refreshCertPicLabel();
		}
	}
	
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值