📄 peer.java
字号:
System.exit(1); } } private void downloadAllBookmarked() { for(int i=0;i<this.bcats.getCatalogsNumber();i++) { this.received=false; BookmarkedCatalogItem bci=this.bcats.getItem(i); this.clabel=bci.getCatalogLabel(); while(!this.received) { this.sendMessage(bci.getCatalogName(),bci.getCatalogUrl()); try{Thread.sleep(5000);}catch(InterruptedException ie){} } w.addedToBC(new AddedBCEvt(this,bci.getCatalogLabel(),bci.getCatalogName(),"In Local Cache")); w.insertLogEntry(bci.getCatalogLabel()+" "+bci.getCatalogName()+" "+bci.getCatalogUrl().toString()+": In Local Cache"); //w.addCheckBox(); } System.out.println("all bookmarked catalogs downloaded"); this.w.insertLogEntry("all bookmarked catalogs downloaded"); } public void init () { qID=0; qIDS=0; bcats=new BookmarkedCatalogs(); lcm=new local_catalog_manager.LocalCatalogManager(); //h= new catalog_search.Handler(newGroup.getResolverService(),"csearch"); try{ModuleClassID classID = (ModuleClassID) IDFactory.fromURL( new URL((refModuleClassID))); //ModuleClassID classID=IDFactory.newModuleClassID(); ceserv = (catalog_exchange.CatalogExchangeService) newGroup.lookupService(classID); //csserv=(catalog_search.CatalogSearchService) newGroup.lookupService(cID); //ceserv=new catalog_exchange.ScambioCataloghiServiceImpl(); ceserv.addListener(this); //csserv.addListener(this); //ciserv=new catalog_exchange.ScambioCataloghiServiceImpl(); //ciserv.addListener(this); } catch(MalformedURLException mue){System.out.println("mue");mue.printStackTrace();} catch(UnknownServiceException use){System.out.println("use");use.printStackTrace();} catch(ServiceNotFoundException snf){ System.out.println("snf"); snf.printStackTrace(); } try{ModuleClassID cID = (ModuleClassID) IDFactory.fromURL( new URL((rMCID))); //System.out.println(cID); //System.out.println(newGroup2.getPeerGroupAdvertisement()); //System.out.println(newGroup2.getPeerGroupID()); //System.out.println(newGroup2.getPeerGroupName()); // newGroup2.lookupService(arg0, arg1) csserv = (catalog_search.CatalogSearchService) newGroup2.lookupService(cID); //csiserv = new catalog_search.CatalogSearchServiceImpl(); //try{csiserv.init(newGroup2, newGroup2.getPeerID(), newGroup2.getImplAdvertisement());} //catch(PeerGroupException pge){System.out.println("pge");pge.printStackTrace();} //csiserv.search(t, d, c, qid, mw); // this.csserv.addListener(h); } //finally{} catch(ServiceNotFoundException snf){ System.out.println("snf"); snf.printStackTrace(); } catch(UnknownServiceException use){System.out.println("use");use.printStackTrace();} catch(MalformedURLException mue){System.out.println("mue");mue.printStackTrace();} } private void sendMessage(String cname, URL u) { try { // Find the service on the peer group. //URL u=new URL("http://localhost/catalog.xml"); ceserv.getCatalog(qID,cname,u); } catch (Exception e2) { // Warn the user. e2.printStackTrace(); System.out.println("Error finding service!"); } } private void refreshCatalogs() { ceserv.catRefresh(); } public void processAnswer(catalog_exchange.CatWebEvt event) { // Extract the response message from the event object. boolean er = event.getResponse(); // Print out the answer given in the response. System.out.println("response from web: "+er); w.insertLogEntry("response from web: "+er); // Print out the answer given in the response. if(er) { received=true; try{ String cl=new String(clabel); String cn=null; if(cname!=null) { cn=new String(cname); URL cu=new URL(curl.toString()); BookmarkedCatalogItem bcifw=new BookmarkedCatalogItem(cl,cn,cu); bcats.insert(bcifw); } } catch(Exception e) { e.printStackTrace(); } } } public void addToBC(AddBCEvt abce) { String cl=new String(abce.getLabel()); URL cu=null; String cn=null; boolean error=false; try{ cu=new URL(abce.getUrl().toString()); if(cu!=null) { w.addedToBC(new AddedBCEvt(this,cl,"URL Parsed")); w.insertLogEntry(cl+" "+cu+": URL Parsed"); cu.openConnection(); w.addedToBC(new AddedBCEvt(this,cl,"URL Reachable")); w.insertLogEntry(cl+" "+cu+": URL Reachable"); } } catch(Exception e){ error=true; w.addedToBC(new AddedBCEvt(this,cl,": Parsing or Connection Error",error)); w.insertLogEntry(cl+" "+cu+": Parsing or Connection Error"); } cn=Integer.valueOf(cu.hashCode()).toString(); w.addedToBC(new AddedBCEvt(this,cl,cn,"Name Calculated")); w.insertLogEntry(cl+" "+cu+" "+cn+": Name Calculated"); bcats.insert(new BookmarkedCatalogItem(cl,cn,cu)); w.addedToBC(new AddedBCEvt(this,cl,cn,"Catalog Bookmarked")); w.insertLogEntry(cl+" "+cu+" "+cn+": Catalog Bookmarked"); w.addedToBC(new AddedBCEvt(this,cl,cn,"Waiting")); w.insertLogEntry(cl+" "+cu+" "+cn+": Waiting"); bcats= new BookmarkedCatalogs(); downloadAllBookmarked(); } public ID getPeerId() { return newGroup.getPeerID(); } public boolean delFromBC(DelBCEvt dbce) { String cn=dbce.getCatalogName(); String cl=dbce.getCatalogLabel(); boolean r=bcats.delete(cn); DeletedBCEvt debce=new DeletedBCEvt(this,dbce.getCatalogName(),cl); w.deletedFromBC(debce); r=(r)&&(ceserv.catDelete(cn)); return r; } public void search(SearchEvt se) { String t=se.getTitle(); String d=se.getDescription(); String []c=se.getCatalogs(); boolean ow=se.isOthweb(); boolean op=se.isOthpeer(); boolean lc=se.isLoccat(); String [] cl=null; String [] tLc=null; String [] dLc=null; String [] uLc=null; if(lc){ Document doc=lcm.getDoc(); boolean match=false; String temp=null; int j=0; NodeList nl=doc.getElementsByTagName("title"); int i=0; cl=new String[1]; cl[0]="Local Peer Catalog"; for (i=0;i<nl.getLength();i++) { if (i>0) { String []b=new String [i+1]; for (int f=0;f<cl.length;f++) b[f]=cl[f]; b[i]=b[i-1]; cl=b; } String par=null; String par2=null; String comodo2=null; NodeList nl2=null; Element el=null; Element el2=null; NodeList n=null; NodeList n2=null; if (t.equals("")) { if(d.equals("")) { } else { par=d; } } else { par=t; if (d!=null)par2=d; } el=(Element)nl.item(i); n=el.getChildNodes(); temp=n.item(0).getNodeValue(); nl2=doc.getElementsByTagName("description"); el2=(Element)nl2.item(i); n2=el2.getChildNodes(); comodo2=n2.item(0).getNodeValue(); Pattern p = null; Matcher m = null; Pattern p2 = null; Matcher m2 = null; if(par!=null && par2!=null) { p = Pattern.compile("(?ui)"+par); m = p.matcher(temp); match=m.find(); p2=Pattern.compile("(?ui)"+par2); m2=p2.matcher(comodo2); match=match && m2.find(); } else if (par!=null && par.equals(t)){ p = Pattern.compile("(?ui)"+par); m = p.matcher(temp); match=m.find(); } else if (par!=null && par.equals(d)){ p = Pattern.compile("(?ui)"+par); m = p.matcher(comodo2); match=m.find(); } if(match) { if (par==t) { if (tLc==null) { tLc=new String [1]; dLc=new String [1]; uLc=new String [1]; tLc[j]=temp; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("description")) { NodeList no=nd.item(k).getChildNodes(); dLc[j]=no.item(0).getNodeValue(); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); uLc[j]=no.item(0).getNodeValue(); } } } else { String [] t1=new String[j+1]; String [] d1=new String[j+1]; String [] u1=new String[j+1]; for (int k=0;k<tLc.length;k++) { t1[k]=tLc[k]; d1[k]=dLc[k]; u1[k]=uLc[k]; } t1[j]=temp; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("description")) { NodeList no=nd.item(k).getChildNodes(); d1[j]=no.item(0).getNodeValue(); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); u1[j]=no.item(0).getNodeValue(); } } tLc=t1; dLc=d1; uLc=u1; } j++; } else { if (tLc==null) { tLc=new String [1]; dLc=new String [1]; uLc=new String [1]; dLc[j]=comodo2; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("title")) { NodeList no=nd.item(k).getChildNodes(); tLc[j]=no.item(0).getNodeValue(); //System.out.println("t[j] "+t[j]); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); uLc[j]=no.item(0).getNodeValue(); } } } else { String [] t1=new String[j+1]; String [] d1=new String[j+1]; String [] u1=new String[j+1]; for (int k=0;k<tLc.length;k++) { t1[k]=tLc[k]; d1[k]=dLc[k]; u1[k]=uLc[k]; } d1[j]=comodo2; Node root=el.getParentNode(); NodeList nd=root.getChildNodes(); for(int k=0;k<nd.getLength();k++) { if(nd.item(k).getNodeName().equals("title")) { NodeList no=nd.item(k).getChildNodes(); t1[j]=no.item(0).getNodeValue(); //System.out.println("t[j] "+t[j]); } if(nd.item(k).getNodeName().equals("url")) { NodeList no=nd.item(k).getChildNodes(); u1[j]=no.item(0).getNodeValue(); } } tLc=t1; dLc=d1; uLc=u1; } } j++; } } if (tLc!=null)w.printResults(tLc,dLc,uLc,cl); } // c=new String[1]; //c[0]="*"; if((ow)||(op)||(c.length>0)) { csserv.search(t, d, c, qIDS, w, ow, op); qIDS++; } } public void insertInLocCat(String t,String d,URL u,boolean dw) { lcm.insert(t, d, u,dw); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -