www.pudn.com > talk-plugins.rar > LogOnAction.java


package talkClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;

public class LogOnAction extends Action {

	private final IWorkbenchWindow window;

	LogOnAction(String text, IWorkbenchWindow window) {
		super(text);
		this.window = window;
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_LOG_ON);
		// Associate the action with a pre-defined command, to allow key
		// bindings.
		setActionDefinitionId(ICommandIds.CMD_LOG_ON);
		setImageDescriptor(talkClient.TalkClientPlugin
				.getImageDescriptor("/icons/sample3.gif"));
	}

	public void run() {
		// LOG ON

		String ip_str = "";
		String password = "";

		// GET IP
		InetAddress ip = null;
		try {
			// [NAME]/[ADDRESS]
			ip = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ip_str = ip.toString().split("/")[1];

		// IP INPUT
		InputDialog dlg = new InputDialog(
				Display.getCurrent().getActiveShell(), "Log On", "IP Address",
				ip_str, null);
		if (dlg.open() == Window.OK) {
			ip_str = dlg.getValue().trim();

			// PASSWORD INPUT
			InputDialog dlg1 = new InputDialog(Display.getCurrent()
					.getActiveShell(), "Log On", "Password", "", null);
			if (dlg1.open() == Window.OK) {
				password = dlg1.getValue().trim();

				System.out.println(ip_str);
				System.out.println(password);

				// REMOTE LOG ON
				switch (logOn(ip_str, password)) {
				case 0:
					MessageDialog.openInformation(Display.getCurrent()
							.getActiveShell(), "Information", "Log on sucess.");
					break;
				case 1:
					MessageDialog
							.openError(Display.getCurrent().getActiveShell(),
									"Error",
									"You have not registered.\nPlease contact with administrator.");
					break;
				case 2:
					MessageDialog.openError(Display.getCurrent()
							.getActiveShell(), "Error", "Wrong password.");
					break;
				case 3:
					MessageDialog.openInformation(Display.getCurrent()
							.getActiveShell(), "Information", "Already log on.");
					break;
				}
			}
		}

	}

	private int logOn(String ip_str, String password) {
		// MAKE THE REQUEST PACKAGE
		// [0]::[IP]::[PASSWORD]
		String logOn_req = "0::" + ip_str + "::" + password;

		// SOCKET CONNECTION
		Socket req_socket = null;
		try {
			req_socket = new Socket("172.18.80.34", 1860);
		} catch (UnknownHostException e) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(),
					"Error", "Connection failed!");
		} catch (IOException e) {
			MessageDialog.openError(Display.getCurrent().getActiveShell(),
					"Error", "Connection failed!");
		}
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(req_socket.getOutputStream(), true);
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		// SENT THE PACKAGE
		pw.println(logOn_req);

		// RECEIVE THE RESPONSE FROM
		// 0 -- SUCCESS
		// 1 -- IP NOT EXIST, SO NEED REGISTER
		// 2 -- PASSWORD WRONG
		// 3 -- ALREADY LOG ON
		String logOn_response = "";
		BufferedReader br_in = null;
		try {
			br_in = new BufferedReader(new InputStreamReader(req_socket
					.getInputStream()));
			logOn_response = br_in.readLine();
			
			br_in.close();
			pw.close();
			req_socket.close();
			
		} catch (IOException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}

		System.out.println(logOn_response);

		
		return Integer.parseInt(logOn_response);
	}
}