Gnutella Forums

Gnutella Forums (
-   General Discussion (
-   -   Using Phex as a library. (

giorgiob July 2nd, 2011 03:12 PM

Using Phex as a library.
Hi all,

I have been experimenting with the following scenario.

I want to write a program that
(1) starts Phex in headless mode,
(2) submits a query,
(3) waits until the query has completed,
(4) gets information about the query hits,
(5) downloads some selected hits
(6) marks the query as completed

I have managed to do step (1). For this, I have taken the Phex main class, and made the main method into a normal method that I can call from my code.
After this method has finished, phex is running and waiting for input.
I can access Phex through the Servent instance.

To perform (2) I wanted to get a QueryManager from the Servent instance and call sendDynamicQuery(). At this point I am pretty lost since I do not know what kinds of parameters I should use here. Also, I do not know exactly how to track the query (3).

Are there any examples for this? Or maybe where are these functionalities implemented in Phex? E.g. what class/method does the program execute when the user submits a query from the GUI? It would be interesting to look at the source code.

Thank you in advance and best greetings

GregorK July 3rd, 2011 02:21 AM

I know Phex is not optimal here and the handling should be improved. I hope this helps you anyway.

For step 2 here ist some pseudo code for the steps to do:

SearchContainer searchContainer = QueryManager.getSearchContainer()
Search newSearch = searchContainer.createSearch( searchString );
newSearch.startSearching( new DefaultSearchProgress(DEFAULT_QUERY_TIMEOUT, DESIRED_RESULTS) );

To get results add a method like this somewhere:
@EventTopicSubscriber(topic=PhexEventTopics.Search _Data)
public void onSearchDataEvent( String topic, SearchDataEvent event )
if ( newSearch != event.getSource() )
if ( event.getType() == SearchDataEvent.SEARCH_HITS_ADDED )
RemoteFile[] newSearchResults = event.getSearchData();

And register the method by calling this in the class constructor:
Phex.getEventService().processAnnotations( this );

There is no real way to do (3), the query results can come in minutes after the search is triggered, there is no official end. The UI uses a combination of timeout and a pseudo progress indicator using the number of received results.
Ask your DefaultSearchProgress instance if it thinks the search is over. But still expect results to come in afterwards.

Can you tell me more about the kind of application you like to embed Phex into?

Look at SearchControllPanel to see how the UI does it.


giorgiob July 3rd, 2011 02:40 AM

Hi Gregor,

thanks for the information.

My idea is to be able to run searches in a batch fashion. For example, if I would like
to look for files matching "Bach", "Mozart", "Haydn", I can write a text file containing
these key words.

Then I launch my program, this starts Phex, reads the text files with the searches, and starts to search (for each search I want to specify the maximum number of hits and a time out after which the search is complete). Based on the hits, it decides what to download. It shuts down when all searches and downloads are completed (it can also have a time out so that a search or download that takes too long is abandoned).

So my first attempt is to write one or more interface classes to "drive" Phex.
As I said, I managed to build a Phex Runner to start Phex as part of a larger
application. For this I just copied the content of the main class into my own method
and did a few adaptations.

Best greetings

giorgiob July 3rd, 2011 01:38 PM

Hi again,

thanks a lot for the hints! I have (2) and (4) working. For (3) I will use a time out.

Now I will look at the code inside the GUI to understand how to download
files. If I have problems I will ask you again.

As we had discussed a few weeks ago, if I can get these functions to work, I can
make a small API and submit it to you. If you find it is OK you can include it
to the code.

Many greetings

GregorK July 3rd, 2011 02:00 PM

Look at inner class SearchResultsPanel.SWQuickDownloadAction to see the code how a RemoteFile object from the search results is either added to an already existing download or to start a new download.

Of course I would be happy if you can help with offering an API to make it easier for others to use this code.

All times are GMT -7. The time now is 08:56 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 ©2011, Crawlability, Inc.

Copyright 2020 Gnutella Forums.
All Rights Reserved.