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

📄 updatecms.java

📁 Java p2p程序设计2002年版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            cms = new CMS();
            
            try {
                cms.init(group, null,group.getPeerGroupAdvertisement());
            } catch (PeerGroupException e) {
                e.printStackTrace();
            }
            
            cms.startApp(new File(getGroupDir(group), CMS.DEFAULT_DIR));
            if (null != cms) {
                cmsSessions.put(getShortName(group), cms);
            }else{
                System.out.println("Error creating CMS object.");
                throw new RuntimeException("Error creating CMS object.");
            }
        }else{
            System.out.println("Using the old CMS.");
        }
        return cms;
    }
    private static File getGroupDir(PeerGroup pg) {
        File dir = null;
        
        // make sure the root directory exists
        File rootDir = new File(CMS_DATA_DIRNAME);
        if (!rootDir.exists()) {
            rootDir.mkdir();
        }
        
        dir = new File(rootDir, getShortName(pg));
        if (!dir.exists()) {
            dir.mkdir();
        }
        
        return dir;
    }
    
    public static String getShortName(PeerGroup peerGroup) {
        URL url = null;
        String groupName = peerGroup.getPeerGroupName();
        groupName = groupName.replace(' ','_');
        return groupName;
    }
    
    InnerListListener listListener = new InnerListListener();
    
    
    protected class InnerGetContentListener implements ContentListener {
        /** Called when a Conent Handler has an event */
        public void contentEvent(ContentEvent event){
            System.out.println(event);
            switch(event.getId()){
                case ContentEvent.LOAD_FAIL:
                    break;
                case ContentEvent.LOAD_DONE:
                    ContentRequestHandler handler = (ContentRequestHandler)event.getSource();
                    File file = handler.getFile();
                    PeerGroupAdvertisement ad = (PeerGroupAdvertisement)group.getPeerGroupAdvertisement();
                    String groupName = ad.getName();
                    DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode(file.getName()+"("+groupName+")");
                    
                    JTree tree = mirrorGUI.getLocalContentTree();
                    DefaultTreeModel model = mirrorGUI.getLocalContentTreeModel();
                    model.insertNodeInto(fileNode, remoteFiles,remoteFiles.getChildCount());
                    
                    tree.treeDidChange();
                    ((DefaultTreeModel)tree.getModel()).reload() ;
                    break;
                case ContentEvent.LOAD_PROGRESS:
                    // We could add a progress bar here
                    // Note also that we could stop the software
                    // from exiting until all files transfer
                    break;
            }
        }
    }
    protected class InnerListListener implements ContentListener {
        public InnerListListener(){
        }
        /** Called when a Conent Handler has an event */
        public void contentEvent(ContentEvent event){
            System.out.println(event);
            switch(event.getId()){
                case ContentEvent.LOAD_FAIL:
                    break;
                case ContentEvent.LOAD_DONE:
                    
                    ListContentHandler handler = (ListContentHandler) event.getSource();
                    
                    ContentAdvertisement[] content = handler.getResults();
                    File dir = new File(inputDir);
                    if (!dir.isDirectory()&& !dir.isFile()){
                        if(!dir.mkdir()){
                            System.out.println("Unable to create the directory");
                        }
                    }
                    for(int i = 0;i < content.length;i++){
                        File saveFile = new File(inputDir+content[i].getName());
                        getRemoteFile(group,content[i], saveFile);
                    }
                    break;
            }
        }
        InnerGetContentListener contentListener = new InnerGetContentListener();
        
        public void getRemoteFileInner(PeerGroup group, ContentAdvertisement cAdv,File file){
            getRemoteFile(group,cAdv,file);
            
        }// end of getRemoteFileInner()
    }// end of inner class InnerListListener
    public void getRemoteFile(PeerGroup group, ContentAdvertisement cAdv,File file){
        try {
            System.out.println("requesting content");
            ContentRequestHandler contentRequestHandler = new ContentRequestHandler(group, cAdv, file);
        } catch (InvocationTargetException e) {
            // Future: Should display error dialog here
            e.printStackTrace();
        }
        
    }// end of getRemoteFile()
    
    class PeriodicDocSearch implements Runnable{
        ListRequestor request = null;
        boolean running = true;
        int sleepTimeMilliseconds;
        public PeriodicDocSearch(int sleepTimeMilliseconds){
            this.sleepTimeMilliseconds = sleepTimeMilliseconds;
            request = new ListRequestor(group, null);
            request.activateRequest();
        }
        public void stopThread(){
            running = false;
            if (request != null){
                request.cancel();
                
            }
        }
        public void run(){
            while (running){
                System.out.println("************* Looking for documents");
                if (!request.isDone()){
                    System.out.println("Starting a newSearch");
                    request.cancel();
                    request = new ListRequestor(group, "");
                    request.activateRequest();
                }else{
                    System.out.println("Still searching");
                }
                
                // searchCMS(group, "");
                
                try{
                    Thread.sleep(sleepTimeMilliseconds);
                }catch(InterruptedException ie){
                    // just shut down the thread
                    stopThread();
                }
            }// while running
        }
    }// end of class PeriodicDocSearch
    
    class ListRequestor extends CachedListContentRequest {
        
        boolean done = false;
        /**
         *  Create a new instance of ListRequester
         *
         *@param  group                          the peergroup for which
         *      to do the searching
         *@param  inRequest                      the pipe to the peer
         *      where to do the searching
         *@param  inSubStr                       the string for which to
         *      search
         *@exception  InvocationTargetException  Description of
         *      Exception
         *@since
         */
        public ListRequestor( PeerGroup group, String inSubStr ) {
            super( group, inSubStr );
        }// End of constructor
        
        /**
         *  Called if the connection over the indicated pipe has been
         *  made and if the result was retrieved. The vector <a
         *  href="#results">results</a> is updated accordingly
         *
         *@since
         */
        public void notifyMoreResults() {
            System.out.println("Found some results");
            
            if (mirrorGUI != null) {
                System.out.println("get results");
                ContentAdvertisement[] searchResults = getResults();
                System.out.println("adding to GUI");
                mirrorGUI.displayDocumentsFound(searchResults);
                // Get the files
                
                for(int i = 0;i < searchResults.length;i++){
                    File saveFile = new File(inputDir+searchResults[i].getName());
                    getRemoteFile(group,searchResults[i], saveFile);
                }
            }
        }// End of notifyMoreResults()
        public void notifyDone() {
            done = true;
            System.out.println("Requestor done");
            
        }// End of  notifyDone()
        
        public boolean isDone(){
            return done;
        }// End of isDone()
    }// end of inner class
    
}// End of class UpdateCMS

⌨️ 快捷键说明

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