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.llrp.adaptor;
23  
24  import java.rmi.RemoteException;
25  import java.rmi.server.UnicastRemoteObject;
26  import java.util.HashMap;
27  import java.util.LinkedList;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.fosstrak.llrp.adaptor.exception.LLRPDuplicateNameException;
32  import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
33  import org.fosstrak.llrp.adaptor.util.AsynchronousNotifiableList;
34  
35  /**
36   * This adaptor implements the Adaptor interface.  
37   * @author sawielan
38   *
39   */
40  public class AdaptorImpl extends UnicastRemoteObject implements Adaptor {
41  	
42  	/**
43  	 * default serial for serialization.
44  	 */
45  	private static final long serialVersionUID = -5896254195502117705L;
46  
47  	/** a map holding all the readers contained in this adaptor. */
48  	protected Map<String, ReaderImpl> readers = new HashMap<String, ReaderImpl> ();
49  	
50  	/** a list with all the receivers of asynchronous messages. */
51  	private AsynchronousNotifiableList toNotify = new AsynchronousNotifiableList();
52  	
53  	/** the name of this adaptor. */
54  	protected String adaptorName = null;
55  	
56  	private AdaptorManagement adaptorManagement = null;
57  	
58  	/**
59  	 * Constructor for a adaptor. 
60  	 * @param adaptorName the name of this adaptor.
61  	 * @throws RemoteException whenever there is an rmi exception.
62  	 */
63  	public AdaptorImpl(String adaptorName) throws RemoteException {
64  		super();
65  		this.adaptorName = adaptorName;
66  	}
67  	
68  	public boolean containsReader(String readerName) throws RemoteException {
69  		return readers.containsKey(readerName);
70  	}
71  
72  	public void define(String readerName, 
73  			String readerAddress, 
74  			boolean clientInitiatedConnection,
75  			boolean connectImmediately)
76  			throws RemoteException, LLRPRuntimeException {
77  		
78  		if (containsReader(readerName)) {
79  			throw new LLRPDuplicateNameException(readerName, "Reader '" + readerName + "' already exists.");
80  		}
81  		
82  		ReaderImpl reader = new ReaderImpl(this, readerName, readerAddress);
83  		reader.setClientInitiated(clientInitiatedConnection);
84  		reader.setConnectImmediate(connectImmediately);
85  		
86  		// run the connection setup only when requested.
87  		if (connectImmediately) {
88  			reader.connect(clientInitiatedConnection);
89  		}
90  		readers.put(readerName, reader);
91  		commit();
92  	}
93  
94  	public void define(String readerName, 
95  			String readerAddress,
96  			int port, 
97  			boolean clientInitiatedConnection,
98  			boolean connectImmediately) 
99  		throws RemoteException, LLRPRuntimeException {
100 		
101 		if (containsReader(readerName)) {
102 			throw new LLRPDuplicateNameException(readerName, "Reader '" + readerName + "' already exists.");
103 		}
104 		
105 		ReaderImpl reader = new ReaderImpl(this, readerName, readerAddress, port);	
106 		reader.setClientInitiated(clientInitiatedConnection);
107 		reader.setConnectImmediate(connectImmediately);
108 		
109 		// run the connection setup only when requested.
110 		if (connectImmediately) {
111 			reader.connect(clientInitiatedConnection);
112 		}
113 		readers.put(readerName, reader);
114 		commit();
115 	}
116 
117 	public String getAdaptorName() throws RemoteException {
118 		return adaptorName;
119 	}
120 
121 	public List<String> getReaderNames() throws RemoteException {
122 		// we create a copy, no leakage!
123 		List<String> readerNames = new LinkedList<String> ();
124 		
125 		for (String name : readers.keySet()) {
126 			readerNames.add(name);
127 		}
128 		return readerNames;
129 	}
130 
131 	public void undefine(String readerName) throws RemoteException,
132 			LLRPRuntimeException {
133 		
134 		if (!containsReader(readerName)) {
135 			throw new LLRPRuntimeException("Reader '" + readerName + "' does not exist.");
136 		}
137 		Reader reader = readers.remove(readerName);
138 		reader.disconnect();
139 		commit();
140 	}
141 		
142 	public void undefineAll() throws RemoteException, LLRPRuntimeException {
143 		for (String readerName : getReaderNames()) {
144 			try {
145 				undefine(readerName);
146 			} catch (LLRPRuntimeException e) {
147 				// remove the reader from the list nevertheless
148 				readers.remove(readerName);
149 				
150 				// notify the error
151 				errorCallback(e, readerName);
152 			}
153 		}
154 		commit();
155 	}
156 	
157 	
158 	public void disconnectAll() throws RemoteException, LLRPRuntimeException {
159 		for (String readerName : getReaderNames()) {
160 			readers.get(readerName).disconnect();
161 		}
162 	}
163 	
164 	public void sendLLRPMessage(String readerName, byte[] message)
165 			throws RemoteException, LLRPRuntimeException {
166 		
167 		if (!containsReader(readerName)) {
168 			throw new LLRPRuntimeException("Reader '" + readerName + "' does not exist.");
169 		}
170 		
171 		readers.get(readerName).send(message);
172 	}
173 
174 	public void sendLLRPMessageToAllReaders(byte[] message)
175 			throws RemoteException, LLRPRuntimeException {
176 		
177 		for (Reader reader : readers.values()) {
178 			reader.send(message);
179 		}
180 		
181 	}
182 
183 	
184 	public void registerForAsynchronous(AsynchronousNotifiable receiver)
185 			throws RemoteException {
186 		
187 		toNotify.add(receiver);
188 	}
189 
190 	
191 	public void messageReceivedCallback(byte[] message, String readerName)
192 			throws RemoteException {
193 		
194 		toNotify.notify(message, readerName);
195 	}
196 
197 	
198 	public void deregisterFromAsynchronous(AsynchronousNotifiable receiver)
199 			throws RemoteException {
200 		
201 		toNotify.remove(receiver);
202 	}
203 
204 	
205 	public void errorCallback(LLRPRuntimeException e, String readerName)
206 		throws RemoteException {
207 		
208 		toNotify.notifyError(e, readerName);	
209 	}
210 
211 	
212 	public Reader getReader(String readerName) throws RemoteException {
213 		return readers.get(readerName);
214 	}
215 
216 	
217 	public void setAdaptorName(String adaptorName) throws RemoteException {
218 		this.adaptorName = adaptorName;
219 	}
220 
221 	private void commit() {
222 		if (adaptorManagement != null) {
223 			adaptorManagement.commit();
224 		}
225 	}
226 	
227 	public void setAdaptorManagement(AdaptorManagement management) {
228 		this.adaptorManagement = management;
229 	}
230 
231 }