www.pudn.com > talk-plugins.rar > LogOutAction.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.MessageDialog; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.ui.IWorkbenchWindow; 
 
public class LogOutAction extends Action { 
 
	private final IWorkbenchWindow window; 
 
	LogOutAction(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_OUT); 
		// Associate the action with a pre-defined command, to allow key 
		// bindings. 
		setActionDefinitionId(ICommandIds.CMD_LOG_OUT); 
		setImageDescriptor(talkClient.TalkClientPlugin 
				.getImageDescriptor("/icons/sample.gif")); 
	} 
 
	public void run() { 
		// LOG OUT 
 
		// GET IP 
		InetAddress ip = null; 
		try { 
			// [NAME]/[ADDRESS] 
			ip = InetAddress.getLocalHost(); 
		} catch (UnknownHostException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
		String ip_str = ip.toString().split("/")[1]; 
 
		// REMOTE LOG OUT 
		switch (logOut(ip_str)) { 
		case 0: 
			MessageDialog.openInformation( 
					Display.getCurrent().getActiveShell(), "Information", 
					"Log out success."); 
			break; 
		case 1: 
			MessageDialog 
					.openError(Display.getCurrent().getActiveShell(), "Error", 
							"You have not registered.\nPlease contact with administrator."); 
			break; 
		case 2: 
			MessageDialog.openInformation( 
					Display.getCurrent().getActiveShell(), "Information", 
					"Already log out."); 
			break; 
		} 
	} 
 
	private int logOut(String ip_str) { 
		// MAKE THE REQUEST PACKAGE 
		// [1]::[IP] 
		String logOut_req = "1::" + ip_str; 
 
		// 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(logOut_req); 
 
		// RECEIVE THE RESPONSE FROM 
		// 0 -- SUCCESS 
		// 1 -- IP NOT EXIST, SO NEED REGISTER 
		// 2 -- ALREADY LOG OUT 
		String logOut_response = ""; 
		BufferedReader br_in = null; 
		try { 
			br_in = new BufferedReader(new InputStreamReader(req_socket 
					.getInputStream())); 
			logOut_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(logOut_response); 
 
		return Integer.parseInt(logOut_response); 
	} 
 
}