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

📄 jxtasearch.java

📁 p2p 是现在java社区最火的一个话题, 看看jxta能给P2p带来什么吧
💻 JAVA
字号:
import java.util.Enumeration;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.OutputPipeEvent;

import net.jxta.protocol.PipeAdvertisement;
import net.jxta.peergroup.PeerGroup;
import net.jxta.discovery.DiscoveryService;


public class JXTASearch implements OutputPipeListener
{
    private PeerGroup peerGroup;
    private OutputPipe outputPipe;
    
    public JXTASearch ( PeerGroup peerGroup ) {
        this.peerGroup = peerGroup;
    }//endConstructor


    public PipeAdvertisement getPipeAdvertisement ( String targetPipeName ) {
        Enumeration enum = null;
        PipeAdvertisement pipeAdvert = null;
       
        DiscoveryService discoveryService = peerGroup.getDiscoveryService ();

        try
        {
            while( true ){
                discoveryService.getRemoteAdvertisements (
                                                     null, 
                                                     DiscoveryService.ADV, 
                                                     "Name", 
                                                     targetPipeName, 
                                                     5
                                                     );

                enum = discoveryService.getLocalAdvertisements (
                                                           DiscoveryService.ADV, 
                                                           "Name", 
                                                           targetPipeName
                                                           );

                
                int i = 0;
              
                if ( enum != null)
                {
                    while (enum.hasMoreElements()) 
                    {
                        i++;
                        pipeAdvert = ( PipeAdvertisement ) enum.nextElement ();
                    }
                }//if(enum != null)
                
                if ( pipeAdvert != null ) 
                {
                    System.out.println ( "JXTASearch: ["+i+"] Target (" + targetPipeName + ") found" );
                    break;
                }//if (pipeAdvert != null)
        
            }//while( true )
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
        
        return pipeAdvert;
        
    }//getPipeAdvertisement() 
    

    public void outputPipeEvent ( OutputPipeEvent e ) {
        outputPipe = e.getOutputPipe ();
    }//outputPipeEvent()


    public OutputPipe getOutputPipe (PipeAdvertisement pipeAdvert){
        try
        {
            PipeService pipeSvc = peerGroup.getPipeService ();
            pipeSvc.createOutputPipe ( pipeAdvert, this );

            while ( true )
            {
                if ( outputPipe != null )
                    break;
            }//while (true)
         
         return outputPipe;        
        
        }//try
        catch ( Exception e )
        {
             e.printStackTrace();
        }//catch
         return null;
    }//getOutputPipe()

}//end JXTASearch class

⌨️ 快捷键说明

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