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

📄 searchsession.java

📁 P2P协议GUNTELLA的java源代码
💻 JAVA
字号:
/*
 * Copyright (C) 2000-2001  Ken McCrary
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Email: jkmccrary@yahoo.com
 */
package com.kenmccrary.jtella;

import java.util.Collections;
import java.util.List;
import java.util.LinkedList;
import java.io.IOException;

import com.kenmccrary.jtella.NodeConnection;
import com.kenmccrary.jtella.util.Log;

/**
 *  A session for initiating seraches on the network
 *
 */
public class SearchSession
{
  private MessageReceiver receiver;
  private GNUTellaConnection connection;
  private Router router;
  private SendThread sendThread;
  private String query;
  private int maxResults;
  private int minSpeed;
  private List searchGUIDList;

  SearchSession(String query,
                int maxResults,
                int minSpeed,
                GNUTellaConnection connection,
                Router router,
                MessageReceiver receiver)
  {
    this.connection = connection;
    this.receiver = receiver;
    this.router = router;
    this.query = query;
    this.maxResults = maxResults;
    this.minSpeed = minSpeed;
    searchGUIDList = Collections.synchronizedList(new LinkedList());
    sendThread = new SendThread();
    sendThread.start();
  }
  
  
  /**
   *   Request a replying servant push a file
   *
   *   @param searchReplyMessage search reply containing file to push
   *   @param pushMessage push message
   *   @return true if the message could be sent
   */
  public boolean sendPushMessage(SearchReplyMessage searchReplyMessage,
                                 PushMessage pushMessage)
  {
    // the push message will be sent on the connection the searchReply
    // arrived on if it is available
    System.out.println("In sendPushmessage");
    Connection connection = searchReplyMessage.getOriginatingConnection();
    
    System.out.println("qr connection status: " + connection.getStatus());
    if ( connection.getStatus() == NodeConnection.STATUS_OK )
    {
      try
      {
        
        System.out.println("Sending push");
        connection.prioritySend(pushMessage);
        return true;
      }
      catch (IOException io)
      {
      }        
    }
    
    return false;
  }                              

  /**
   *  Close the session, ignore future query hits
   *
   */
  public void close()
  {
    // When the session closes, clean out the origination information
    // stored for this session
    router.removeMessageSender(searchGUIDList);
    sendThread.shutdown();
  }

  /**
   *  Continuously monitors for newly formed active connections to send
   *  to
   *
   */
  class SendThread extends Thread
  {
    private LinkedList sentConnections;
    private boolean shutdown;

    SendThread()
    {
      sentConnections = new LinkedList();
      shutdown = false;
    }

    /**
     *  Cease operation
     */
    void shutdown()
    {
       shutdown = true;
       interrupt();
    }

    /**
     *  Working loop
     *
     */
    public void run()
    {
      while (!shutdown)
      {
        try
        {
          // Get all the active connections
          List activeList = connection.getConnections().getActiveConnections();

          // Check if any new connections exists
          activeList.removeAll(sentConnections);

          for (int i = 0; i < activeList.size(); i++)
          {
            NodeConnection nodeConnection = (NodeConnection)activeList.get(i);

            SearchMessage searchMessage = new SearchMessage(query, minSpeed);
            searchGUIDList.add(searchMessage.getGUID());
            nodeConnection.sendAndReceive(searchMessage, receiver);
            sentConnections.add(nodeConnection);
          }

          sleep(5000);
        }
        catch (Exception e)
        {
          // keep running
        }
      }
    }
  }
}

⌨️ 快捷键说明

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