clienter.java

来自「用java实现的一个bbs的portal」· Java 代码 · 共 478 行 · 第 1/2 页

JAVA
478
字号
                sendMessage("COLLECTIONINFO",collectionInfo);
                collectionNum = 1;
            }else{
                sendMessage("COLLECTIONEND","");
                collectionNum = 0;
            }
        }
        if (messageType.equals("COLLECTIONNEXT")){
            if (collectionNum<collectionVec.size()){
                String collectionInfo = (String)collectionVec.get(collectionNum);
                System.out.println("COLLECTION INFO : "+collectionInfo+"###");
                sendMessage("COLLECTIONINFO",collectionInfo);
                collectionNum++;
            }else{
                sendMessage("COLLECTIONEND","");
                collectionNum = 0;
            }
        }
        if (messageType.equals("ADDCOLLECTION")){
            String commodity = (String)mesVec.get(1);
            String xmlfile = (String)mesVec.get(2);
            userCollection.insertMyCollection(this.USERID,commodity,xmlfile);
        }
        if (messageType.equals("CLEARCOLLECTION")){
            userCollection.clearCollection(this.USERID);
        }
        if (messageType.equals("DELCOLLECTION")){
            String xmlfile = (String)mesVec.get(1);
            userCollection.removeMyCollection(this.USERID,xmlfile);
        }
        if (messageType.equals("COMMODITY")){
        }
        if (messageType.equals("ONLINE")){
            String bbsName =  (String)mesVec.get(1);
            String bbsId = (String)mesVec.get(2);
            if (BBSTester.getInstance().isOnLine(bbsName,bbsId)){
                sendMessage("ONLINE",message+"%"+"TRUE");
            }else sendMessage("ONLINE",message+"%"+"FALSE");
        }
        if (messageType.equals("MAIL")){
            String from = (String)mesVec.get(1);
            String bbsName = (String)mesVec.get(2);
            String bbsId = (String)mesVec.get(3);
            String title = (String)mesVec.get(4);
            String content = (String)mesVec.get(5);
            content = content.replaceAll("%%","\r\n");

            String to = bbsId+"@"+bbsName;
            MailBean mailBean = new MailBean();
            mailBean.setContent(from,to,title,content);
            if (mailBean.sendMail())
                sendMessage("MAILSUCC","");
            else sendMessage("MAILFAIL","");
        }
        if (messageType.equals("UPDATEUSER")){
            String id =  (String)mesVec.get(1);
            String password = (String)mesVec.get(2);
            String city = (String)mesVec.get(3);
            String address = (String)mesVec.get(4);
            String telephone = (String)mesVec.get(5);
            String mobile = (String)mesVec.get(6);
            String email = (String)mesVec.get(7);
            userInfo.setId(id);
            userInfo.setPassword(password);
            userInfo.setCity(city);
            userInfo.setAddress(address);
            userInfo.setTelephone(telephone);
            userInfo.setMobile(mobile);
            userInfo.setEmail(email);
            if (userInfo.updateUserInfo()) {
                sendMessage("UPDUSERSUCC","");
                sendMessage("USERINFO","");
            } else sendMessage("UPDUSERFAIL","");
        }
        if (messageType.equals("IAMHERE")){
            ServerRecorder.activeClient.add(this.ClientID);
        }
        /////////// wait for modify /////////////////////
        //ATTENTION%TRADETYPE%KIND%COMMODITY%KEYWORD1| KEYWORD2|KEYWORD3%recentTime
        if (messageType.equals("ATTENTION")){
            String tradetype =  (String)mesVec.get(1);
            String kind =  (String)mesVec.get(2);
            String commodity =  (String)mesVec.get(3);
            String keywords =  (String)mesVec.get(4);
            String recentvisit = (String)mesVec.get(5);
            userAttention.addAttention(tradetype,kind,commodity,keywords,recentvisit);
        }
        if (messageType.equals("CANCLEATTENTION")){
            String tradetype = (String)mesVec.get(1);
            String kind = (String)mesVec.get(2);
            String commodity = (String)mesVec.get(3);
            String keywords = (String)mesVec.get(4); 
            userAttention.cancleAttention(tradetype,kind,commodity,keywords);
        }
        if (messageType.equals("CHANGEATTENTION")){
            String refreshtype = (String)mesVec.get(1);
            userAttention.setRefreshtype(Integer.parseInt(refreshtype));
        }
        if (messageType.equals("CLEARATTENTION")){
            userAttention.clearAttention();
        }
    }


    public void connectionClose(){
        if (userAttention != null){
            userAttention.save2DB();
            userAttention.setFlag(false);
        }
        ServerAdapter.onlineUser.remove(USERID+" "+remoteAdd+" "+loginTime);
        ServerAdapter.serverUI.onlineList.setListData(ServerAdapter.onlineUser);
        try{
            out.close();
            in.close();
            socket.close();
            ServerRecorder.removeClienter(ClientID);
            ServerRecorder.activeClient.remove(ClientID);
            ServerAdapter.serverUI.infoLabel.setText("The active connection number is "+ServerRecorder.getClientOnlineNum());
            isRunning = false;
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public synchronized void sendMessage(String messageType,String content){
        System.out.print("Output MessageType ; "+messageType+"|");
        //System.out.println("Output Content ; "+content);
        if (messageType.equals("LOGSUCC")){
            String sendStr = messageType+"%"+ClassifyConfig.getInstance().getXMLLocale()
                    +"%"+ClassifyConfig.getInstance().getBBSName()+ CommodityInfo.getAllKind();
            System.out.println(sendStr);
            out.println(sendStr);
        }
        if (messageType.equals("USERINFO")){
            String userInfoStr = "USERINFO%"+userInfo.getUserInfo(USERID);
            System.out.println(userInfoStr);
            out.println(userInfoStr);
        }
        if (messageType.equals("INITATTENTIONLIST")){
            String attentionList = userAttention.getInitAttentionList();
            System.out.println("INITAttentionList : "+attentionList);
            out.println(attentionList);
        }
        if (messageType.equals("ATTENTIONLIST")){
            String attentionList = userAttention.getAttentionList();
            if (attentionList != null){
                System.out.println("AttentionList : "+attentionList);
                out.println(attentionList);
            }
        }
        /*
        if (messageType.equals("COLLECTIONINDEX")){
        String CollcectionIndex = "COLLECTIONINDEX%"+UserCollection.getCollectionIndex(this.USERID);
        System.out.println(CollcectionIndex);
        out.println(CollcectionIndex);
        }
        */
        if (messageType.equals("LOGFAIL")){
            out.println("LOGFAIL");
        }
        if (messageType.equals("REGSUCC")){
            out.println("REGSUCC");
        }
        if (messageType.equals("REGFAIL")){
            out.println("REGFAIL");
        }
        if (messageType.equals("TRADEINFO")){
            out.println(content);
        }
        if (messageType.equals("INFOEND")){
            out.println("INFOEND");
        }
        if (messageType.equals("COLLECTIONINFO")){
            out.println(content);
        }
        if (messageType.equals("COLLECTIONEND")){
            out.println("COLLECTIONEND");
        }

        if (messageType.equals("ONLINE")){
            out.println(content);
        }
        if (messageType.equals("MAILSUCC")){
            out.println("MAILSUCC");
        }
        if (messageType.equals("MAILFAIL")){
            out.println("MAILFAIL");
        }
        if (messageType.equals("UPDUSERSUCC")){
            out.println("UPDUSERSUCC");
        }
        if (messageType.equals("UPDUSERFAIL")){
            out.println("UPDUSERFAIL");
        }
        if (messageType.equals("AREYOUHERE")){
            out.println("AREYOUHERE");
        }
        if (messageType.equals("SYSTEMINFO")){
            out.println("SYSTEMINFO%"+content);
        }
        if (messageType.equals("EXIT")){
            out.println("EXIT");
        }
    }

    public String getClientID() {
        return ClientID;
    }

    public String getUSERID() {
        return USERID;
    }

    public short getActiveState() {
        return activeState;
    }

    public void setActiveState(short activeState) {
        this.activeState = activeState;
    }

    public short subActiveState() {
        this.activeState--;
        return this.activeState;
    }

    public short addActiveState() {
        if (this.activeState <5)
            this.activeState++;
        return this.activeState;
    }



    public static void main(String args[]){
        System.out.println("%"+ClassifyConfig.getInstance().getBBSName()+ CommodityInfo.getAllKind());
    }
}

⌨️ 快捷键说明

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