Java非阻塞聊天室源碼 Client

//client
public class NBChatClient {
    private static final String CMD_QUIT = "/quit";
    
    private Selector sel;
    private SocketChannel socket;
    private boolean feedback = false;
    private boolean loginSeccess = false;
    private boolean active = true;
    
    private Object oLogin = new Object();
    private ByteBuffer buf = ByteBuffer.allocate(128);
    private static Properties props = new Properties();
    NBChatClient(String fName) {
        initConfig(fName);
        initClient();
        start();
    }
    
    private static void initConfig(String fName) {
        try {
            InputStream in = NBChatServer.class.getClassLoader().getResourceAsStream(fName);
            props.load(in);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
    
    private void initClient() {
        String ipStr = props.getProperty(NBChatServer.key_ip);
        String portStr = props.getProperty(NBChatServer.key_port);
        
        try {
            sel = Selector.open();
            this.socket = SocketChannel.open();
            InetAddress ip = InetAddress.getByName(ipStr);
            InetSocketAddress remote = new InetSocketAddress(ip, Integer.parseInt(portStr));
            this.socket.connect(remote);
            this.socket.configureBlocking(NBChatServer.NON_BLOCKING);
            socket.register(sel, SelectionKey.OP_READ);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    
    private void start() {
        //create a new thread to read message from server.
        new Thread() {
            public void run() {
               
                int readyCount = 0;
                while (active) {
                    try {
                        readyCount = sel.select();
                    } catch (IOException e) {
                        if (sel.isOpen())
                            continue;
                        else
                            e.printStackTrace();
                    }
                    if (readyCount == 0)
                        continue;
                    Set readyKeys = sel.selectedKeys();
                    Iterator keys = readyKeys.iterator();
                    while (keys.hasNext()) {
                        SelectionKey key = (SelectionKey) keys.next();
                        if (!key.isValid())
                            continue;
                        keys.remove();
                        try {
                            if (key.isReadable()) {
                                SocketChannel socket = (SocketChannel) key.channel();
                                buf.clear();
                                socket.read(buf);
                                String input = ChatUtil.decode(buf);
                                
                                //如果已經授權,則直接輸出信息。
                                if (loginSeccess) {
                                    System.out.println(input);
                                } 
                                //如果沒有登錄, 且返回信息爲授權登錄, 則將登錄旗標置爲true, 並notify主線程。
                                else if (NBChatServer.LOGIN_OK.equals(input)) {
                                    System.out.println("-------------------------------------------- Welcome ------------------------------------ ");
                                    System.out.println("---------------------------------Non Blocking Chat Program------------------------------- ");
                                    System.out.println("----------------------------------------Author: ChenLinping ----------------------------- ");
                                    System.out.println("------------------------------------------------------------------------------------------");
                                    feedback = loginSeccess = true;
                                    
                                    
//                                    //用sleep保證此線程的notify在main線程的wait之後.
//                                    //如果沒有這個sleep,則可能發生先notify再wait的情況,則mail線程將一直等待oLogin的鎖而阻塞。
//                                    try {
//                                        Thread.sleep(100);
//                                    } catch (InterruptedException e) {
//                                        e.printStackTrace();
//                                    }
                                    
                                    synchronized (oLogin) {
                                        oLogin.notifyAll();
                                    }
                                } 
                                //否則輸出permission denied信息。
                                else {
                                    System.out.println("Permission denied~");
                                    feedback = true;
//                                    try {
//                                        Thread.sleep(100);
//                                    } catch (InterruptedException e) {
//                                        e.printStackTrace();
//                                    }
                                    
                                    synchronized (oLogin) {
                                        oLogin.notifyAll();
                                    }
                                }
                            }
                        } catch (IOException e) {
                            System.out.println("You have disconnected!");
                            key.cancel();
                            try {
                                key.channel().close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                }
            }
        }.start();
    }
    private void handleMsg(String s) throws IOException {
        if (CMD_QUIT.equals(s)) {
            this.active = false;
            socket.close();
            System.exit(-1);
        } else
        socket.write(ByteBuffer.wrap(s.getBytes()));
    }
    private void doLogin(NBChatClient client, BufferedReader input) throws IOException {
        while (!client.loginSeccess) {
            System.out.print("user:");
            String user = input.readLine();
            System.out.print("pass:");
            String pass = input.readLine();
            
            client.handleMsg("/login:"+user+"/"+pass);
            
            //可能存在的一種情況是:程序運行到這裏,mail線程還沒有block,而server已經響應了,告知client登錄成功或失敗。這種情況下,則不需要block mail線程。
            //如果是登錄成功,則不需要block線程,而應該直接進入聊天。
            //如果是登錄失敗,也不需要block線程,而應該直接讓用戶重新登錄。
            while (!client.feedback) {
                //輸入登錄信息後,阻塞main線程,等待系統驗證,如果驗證成功,則可以開始聊天。
                synchronized (client.oLogin) {
                    try {
                        client.oLogin.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        NBChatClient client = new NBChatClient(args[0]);
        try {
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            
            client.doLogin(client, input);
            
            String s;
            while ((s = input.readLine()) != null)
                client.handleMsg(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 

 

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