View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package org.xnap.plugin.freeway;
20  
21  import java.util.ArrayList;
22  
23  import org.apache.log4j.Logger;
24  import org.gnu.freeway.DaemonSocket;
25  import org.gnu.freeway.protocol.afs.esed2.AFSUtils;
26  import org.gnu.freeway.protocol.afs.esed2.RootNode;
27  import org.gnu.freeway.protocol.afs.esed2.SearchResultCallback;
28  import org.gnu.freeway.protocol.afs.esed2.SendQueriesContext;
29  import org.gnu.freeway.protocol.afs.esed2.TestTerminateThread;
30  import org.gnu.freeway.protocol.afs.swing.SController;
31  import org.gnu.freeway.util.AbstractAction;
32  import org.gnu.freeway.util.ConfigurationService;
33  import org.gnu.freeway.util.CronService;
34  import org.gnu.freeway.util.StateService;
35  import org.gnu.freeway.util.Task;
36  import org.gnu.freeway.util.crypto.HashCode160;
37  import org.xnap.XNap;
38  import org.xnap.search.AbstractSearch;
39  import org.xnap.search.SearchFilter;
40  import org.xnap.search.SearchHandler;
41  
42  /***
43   * 
44   */
45  public class FreewaySearch extends AbstractSearch 
46  	implements SearchResultCallback, TestTerminateThread
47  {
48  
49  	private SearchHandler handler;
50  	private SController controller;
51  	private boolean done = false;
52  	
53  	private DaemonSocket socket = null;
54  	private SendJob sendJob = null;
55  	
56  	private static Logger logger = Logger.getLogger(FreewaySearch.class);
57  
58  	/***
59  	 * @param filter
60  	 */
61  	public FreewaySearch(SearchFilter filter, SController controller) 
62  	{
63  		super(filter);
64  		this.controller = controller;
65  	}
66  
67  	/*
68  	 * @see org.xnap.search.Search#getStatus()
69  	 */
70  	public String getStatus() 
71  	{
72  		if (done) {
73  			return XNap.tr("Done");
74  		}
75  		else {
76  			return XNap.tr("Running");
77  		}
78  	}
79  
80  	/*
81  	 * @see org.xnap.search.Search#isDone()
82  	 */
83  	public boolean isDone() 
84  	{
85  		return done;
86  	}
87  
88  	/*
89  	 * @see org.xnap.search.Search#start(org.xnap.search.SearchHandler)
90  	 */
91  	public void start(SearchHandler handler) 
92  	{
93  		this.handler = handler;
94  		done = false;
95  		socket = DaemonSocket.create
96  		((ConfigurationService)controller.service(ConfigurationService.class),
97  		  controller.getPreferences());
98  		if (socket == null) {
99  			done = true;
100 		}
101 		else {
102 			startSearch();
103 		}
104 	}
105 
106 	/***
107 	 * 
108 	 */
109 	private void startSearch() 
110 	{
111 		HashCode160[]	keys;
112 		final SendQueriesContext	sqc;
113 		Task	receiveThread;
114 		String[] keywords;
115 		ArrayList list = new ArrayList();
116 		int i;
117 		final CronService cron = (CronService)controller.service(CronService.class);
118 		
119 		String[] p = getFilter().getText().split("//s+");
120 		
121 		for (i=0; i<p.length; i++) {
122 			if (p[i].length()>0) {
123 				list.add(p[i]);
124 			}
125 		}
126 
127 		if (list.size() == 0) {
128 			System.out.println("no keywords left");			
129 		}
130 
131 		keywords = (String[]) list.toArray(new String[list.size()]);
132 		
133 		keys = AFSUtils.parseKeywords(keywords);
134 		if (keys.length==0) {
135 			done = true;
136 			return;
137 		}	
138 		
139 		sqc = new SendQueriesContext(keys, socket);
140 		sqc.setTimeOut(0x00FFFFFF);	// "forever" is 6 months...
141 		
142 		sendJob = new SendJob(cron, sqc);
143 		cron.addJob(sendJob, 0, 0);
144 		
145 		Task receiver = new Task("GNUNet SearchResult Receiver", new ReceiveJob(sqc));
146 		receiver.launch();
147 	}
148 
149 	/*
150 	 * @see org.xnap.search.Search#stop()
151 	 */
152 	public void stop() 
153 	{
154 		done = true;
155 		//  socket.closeTemporarily();
156 		((CronService) controller.service(CronService.class)).deleteJob(sendJob, 0);
157 		socket.destroy(); 
158 	}
159 	
160 	private class SendJob extends AbstractAction
161 	{
162 		CronService cron;
163 		SendQueriesContext sqc;
164 	
165 		public SendJob(CronService cron, SendQueriesContext sqc)
166 		{
167 			this.cron = cron;
168 			this.sqc = sqc;
169 		}
170 	
171 		public void perform() throws Throwable 
172 		{
173 			logger.debug("sending query again");
174 			sqc.repeatedlySend(cron, controller.getPolicy());		
175 		}
176 	}
177 	
178 	private class ReceiveJob extends AbstractAction
179 	{
180 		SendQueriesContext sqc;
181 		
182 		ReceiveJob(SendQueriesContext sqc)
183 		{
184 			this.sqc = sqc;
185 		}
186 		
187 		public void perform()
188 		{
189 			logger.debug("Receiving...");
190 			sqc.receive((ConfigurationService) controller.service(ConfigurationService.class),
191 							(StateService) controller.service(StateService.class),
192 							FreewaySearch.this, FreewaySearch.this);
193 			FreewaySearch.this.stop();
194 		}
195 	}
196 
197 	/*
198 	 * @see org.gnu.freeway.protocol.afs.esed2.SearchResultCallback#searchResult(org.gnu.freeway.protocol.afs.esed2.RootNode)
199 	 */
200 	public void searchResult(RootNode node) 
201 	{
202 		handler.resultReceived(new FreewaySearchResult(node));		
203 	}
204 
205 	/*
206 	 * @see org.gnu.freeway.protocol.afs.esed2.SearchResultCallback#newNameFor(org.gnu.freeway.protocol.afs.esed2.RootNode, java.lang.String)
207 	 */
208 	public void newNameFor(RootNode arg0, String arg1) 
209 	{
210 		// TODO Auto-generated method stub
211 	}
212 
213 	/*
214 	 * @see org.gnu.freeway.protocol.afs.esed2.TestTerminateThread#test()
215 	 */
216 	public boolean test()
217 	{
218 		return done;
219 	}
220 }