⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatservicemanager.java

📁 JStock是一个免费股市软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            //}
          
            ChatService.this.notifyStateObserver(state);
            
            doneSignal = new CountDownLatch(1);

            try {
                doneSignal.await();
            } catch (InterruptedException ex) {
                state = State.END;
            }

            state = State.END;
            
            return state;
        }

        private State account_created()
        {
            State state = State.ACCOUNT_CREATED;

            ChatService.this.notifyStateObserver(state);

            state = State.CONNECTING;

            return state;
        }

        private State account_creating()
        {
            State state = State.ACCOUNT_CREATING;

            ChatService.this.notifyStateObserver(state);

            AccountManager accountManager = connection.getAccountManager();

            if (accountManager == null)
            {
                state = State.CONNECTING;

                return state;
            }

            try {
                if (use_login_retry)
                {
                    accountManager.createAccount(username + this.login_retry, password);
                }
                else
                {
                    accountManager.createAccount(username, password);
                }

                state = State.ACCOUNT_CREATED;

            } catch (XMPPException ex) {
                log.error(null, ex);

                final XMPPError error = ex.getXMPPError();
                if (error != null)
                {
                    if (error.getCode() == 409)
                    {
                        // XMPPError : conflict(409) - account already there.
                        
                        // Try to pollute the username.
                        use_login_retry = true;
                        login_retry++;                       
                    }

                    state = State.CONNECTING;
                }
                else
                {
                    state = State.CONNECTING;
                }
            }

            return state;
        }

        private void sendMessage(String msg) {
            try {
                readerLock.lock();
                if (muc != null)
                {
                    muc.sendMessage(msg);
                }
            } catch (XMPPException ex) {
                log.error(null, ex);
            }
            finally {
                readerLock.unlock();
            }
        }

        private State connecting()
        {
            State state = State.CONNECTING;

            ChatService.this.notifyStateObserver(state);

            if (connection.isAuthenticated() && connection.isConnected())
            {
                state = State.CONNECTED;
                return state;
            }
            
            try {
                connection.connect();

                // You have to put this code before you login
                SASLAuthentication.supportSASLMechanism("PLAIN", 0);

                if (use_login_retry)
                {
                    connection.login(username + this.login_retry, password);
                }
                else
                {
                    connection.login(username, password);
               }

                if (connection.isAuthenticated() && connection.isConnected())
                {
                    if (use_login_retry)
                    {
                        use_login_retry = !use_login_retry;
                        username = username + this.login_retry;
                        this.login_retry = -1;
                        MainFrame.getJStockOptions().setChatUsername(username);
                    }

                    this.connection.removeConnectionListener(connectionListener);
                    this.connection.addConnectionListener(connectionListener);

                    state = State.CONNECTED;
                }
            } catch (XMPPException ex) {
                log.error(null, ex);

                final XMPPError error = ex.getXMPPError();
                if (error != null)
                {
                    if (error.getCode() == 504)
                    {
                        // XMPPError : remote-server-timeout(504) Could not connect to jabber.org:5222. - network down
                        state = State.CONNECTING;
                    }
                }
                else
                {
                    state = State.ACCOUNT_CREATING;
                }
            }            

            return state;
        }

        public void stop()
        {
            runnableFlag = false;
            if (me != null)
                me.interrupt();

            if (doneSignal != null)
                doneSignal.countDown();
        }

        public boolean changePassword(String newPassword) {
            final XMPPConnection c = this.connection;

            if (c == null) {
                return false;
            }

            try {
                if ((c.isAuthenticated() == false) || (c.isConnected() == false)) {
                    return false;
                }

                final AccountManager accountManager = c.getAccountManager();

                if (accountManager == null) {
                    return false;
                }

                accountManager.changePassword(newPassword);
            }
            catch (Exception ex) {
                log.error(null, ex);
                return false;
            }

            return true;
        }

        public boolean isLogin() {
            final XMPPConnection c = this.connection;

            if (c == null) {
                return false;
            }

            return c.isAuthenticated() && c.isConnected();
        }

        private final ConnectionListener connectionListener = this.getConnectionListener();

        // It is strange that we cannot share addMessageListener and addParticipantListener on a
        // same single object.
        private final PacketListener messageListener = this.getPacketListener();
        private final PacketListener participantListener = this.getPacketListener();
        
        private MultiUserChat muc = null;
        private volatile boolean runnableFlag = true;
        private String username = null;
        private final String password;
        private XMPPConnection connection = null;
        private boolean use_login_retry = false;
        private int login_retry = -1;
        private volatile CountDownLatch doneSignal = null;
        private Thread me = null;

        private final java.util.concurrent.locks.ReadWriteLock readWriteLock;
        private final java.util.concurrent.locks.Lock readerLock;
        private final java.util.concurrent.locks.Lock writerLock;
    }

    public synchronized void start()
    {
        String username = MainFrame.getJStockOptions().getChatUsername();
        String password = org.yccheok.jstock.gui.Utils.decrypt(MainFrame.getJStockOptions().getChatPassword());

        stop();

        chatService = new ChatService(username, password);
        chatService.start();
    }

    public synchronized void stop()
    {
        if (chatService == null) {
            return;
        }

        chatService.stop();
    }

    public void sendMessage(String msg) {
        this.chatService.sendMessage(msg);
    }

    private static class SubjectEx<S, A> extends Subject<S, A> {
        @Override
        protected void notify(S subject, A arg) {
            super.notify(subject, arg);
        }
    }

    private SubjectEx<ChatServiceManager, Packet> getPacketSubject() {
        return new SubjectEx<ChatServiceManager, Packet>();
    }

    private SubjectEx<ChatServiceManager, ChatServiceManager.State> getStateSubject() {
        return new SubjectEx<ChatServiceManager, ChatServiceManager.State>();
    }

    private void notifyPacketObserver(Packet packet) {
        this.packetSubject.notify(this, packet);
    }

    private void notifyStateObserver(State state) {
        this.stateSubject.notify(this, state);
    }

    public void attachPacketObserver(org.yccheok.jstock.engine.Observer<ChatServiceManager, Packet> observer) {
        packetSubject.attach(observer);
    }

    public void attachStateObserver(org.yccheok.jstock.engine.Observer<ChatServiceManager, ChatServiceManager.State> observer) {
        stateSubject.attach(observer);
    }

    public void dettachAllPacketObserver() {
        packetSubject.dettachAll();
    }

    public void dettachAllStateObserver() {
        stateSubject.dettachAll();
    }

    public boolean isLogin() {
        final ChatService c = this.chatService;

        if (c == null) {
            return false;
        }

        return c.isLogin();
    }

    public boolean changePassword(String newPassword) {
        final ChatService c = this.chatService;
        
        if (c == null) {
            return false;
        }

        return c.changePassword(newPassword);
    }

    private final SubjectEx<ChatServiceManager, ChatServiceManager.State> stateSubject = this.getStateSubject();
    private final SubjectEx<ChatServiceManager, Packet> packetSubject = this.getPacketSubject();

    private static final Log log = LogFactory.getLog(ChatServiceManager.class);
    private ChatService chatService = null;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -