View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.capturingapp.util;
23  
24  import java.io.IOException;
25  import java.util.concurrent.Future;
26  
27  import org.fosstrak.capturingapp.CaptureApp;
28  import org.fosstrak.capturingapp.CaptureAppPortTypeImpl;
29  
30  /**
31   * tiny helper to execute a capture application and at the same time maintain 
32   * a handle to it.
33   * @author sawielan
34   *
35   */
36  public class CaptureAppWorker {
37  	// the capture application itself.
38  	private CaptureApp captureApp;
39  	
40  	// an identifier for this worker/capture application.
41  	private final String identifier;
42  	
43  	// a handle to the executor service running the capture application.
44  	@SuppressWarnings("unchecked")
45  	private Future handle;
46  	
47  	/**
48  	 * creates a new worker.
49  	 * @param identifier an identifier for this worker/capture application.
50  	 * @param cap a handle to the executor service running the capture application.
51  	 */
52  	public CaptureAppWorker(String identifier, 
53  			CaptureApp cap) {
54  		
55  		this.captureApp = cap;
56  		this.identifier = identifier;
57  	}
58  	
59  	/**
60  	 * start the execution of the worker.
61  	 */
62  	public void start() {
63  		handle = CaptureAppPortTypeImpl.submitToThreadPool(captureApp);
64  	}
65  	
66  	/**
67  	 * stop the execution of the worker.
68  	 */
69  	public void stop() {
70  		try {
71  			captureApp.stopCaptureApp();
72  		} catch (IOException e) {
73  			e.printStackTrace();
74  		}
75  		handle.cancel(true);
76  	}
77  	
78  	/**
79  	 * @return a handle to the capture application.
80  	 */
81  	public org.fosstrak.capturingapp.CaptureApp getCaptureApp() {
82  		return captureApp;
83  	}
84  	
85  	/**
86  	 * @return the identifier for this worker.
87  	 */
88  	public final String getIdentifier() {
89  		return identifier;
90  	}
91  }