登錄 之 通訊機制(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();
		}
	}
	
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章