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.StubNotFoundException;
26  import java.rmi.server.UnicastRemoteObject;
27  
28  import org.fosstrak.llrp.adaptor.exception.LLRPRuntimeException;
29  import org.fosstrak.llrp.client.LLRPExceptionHandlerTypeMap;
30  import org.llrp.ltk.exceptions.InvalidLLRPMessageException;
31  import org.llrp.ltk.generated.LLRPMessageFactory;
32  import org.llrp.ltk.types.LLRPMessage;
33  
34  /**
35   * creates a callback instance that retrieves asynchronous messages.
36   * @author sawielan
37   *
38   */
39  public class AdaptorCallback extends UnicastRemoteObject implements AsynchronousNotifiable {
40  	
41  	/**
42  	 * serial id.
43  	 */
44  	private static final long serialVersionUID = 1L;
45  	
46  	/** the worker that holds this callback. */
47  	private AdaptorWorker worker = null;
48  	
49  	/**
50  	 * creates a callback instance that retrieves asynchronous messages.
51  	 * @param remote true if run remotely.
52  	 * @throws RemoteException when there is an rmi exception.
53  	 */
54  	public AdaptorCallback(boolean remote) throws RemoteException {
55  		if (remote) {
56  			try {
57  				UnicastRemoteObject.exportObject(this);
58  			} catch (StubNotFoundException e) {
59  				// this exception is normal as exportObject is backwards compatible to 
60  				// java 1.4. since java 1.5 the stub gets auto-generated and so 
61  				// there is no stub available -> exception. we can safely 
62  				// ignore this exception.
63  				System.out.println("ignoring exception as with java > 5 ok.");
64  			} catch (RemoteException e) {
65  				e.printStackTrace();
66  			}
67  		}
68  	}
69  	
70  
71  	/* (non-Javadoc)
72  	 * @see org.fosstrak.llrp.adaptor.AsynchronousNotifiable#notify(byte[], java.lang.String)
73  	 */
74  	public void notify(byte[] message, String readerName) throws RemoteException {
75  		try {
76  			// create the llrp message 
77  			LLRPMessage llrpMessage = LLRPMessageFactory.createLLRPMessage(message);
78  			
79  			// dispatch the message to the simplified handlers
80  			AdaptorManagement.getInstance().dispatchHandlers(
81  					worker.getAdaptor().getAdaptorName(), readerName, llrpMessage);
82  			
83  		} catch (InvalidLLRPMessageException e) {
84  			AdaptorManagement.getInstance().postException(new LLRPRuntimeException(e.getMessage()), 
85  					LLRPExceptionHandlerTypeMap.EXCEPTION_MSG_SENDING_ERROR, 
86  					worker.getAdaptor().getAdaptorName(), readerName);
87  		}
88  	}
89  	
90  	/**
91  	 * sets the worker that holds this callback.
92  	 * @param worker the worker that holds this callback.
93  	 */
94  	public void setWorker(AdaptorWorker worker) {
95  		this.worker = worker;
96  	}
97  
98  	public void notifyError(LLRPRuntimeException e, String readerName) throws RemoteException {
99  		AdaptorManagement.getInstance().postException(e, e.getExceptionType(),
100 				worker.getAdaptor().getAdaptorName(),
101 				readerName);
102 	}
103 }