Difference between revisions of "Using the Engine API"
From RifidiWiki
(New page: This page describes how to create an instance of the Rifidi engine, create readers, and add and remove tags from that engine all programatically via RMI. It is a tutorial for creating a "...) |
(→Write the Hello World Application) |
||
Line 24: | Line 24: | ||
import java.io.PrintWriter; | import java.io.PrintWriter; | ||
import java.net.Socket; | import java.net.Socket; | ||
− | |||
− | |||
− | |||
import java.util.ArrayList; | import java.util.ArrayList; | ||
Line 165: | Line 162: | ||
// print tag list coming back from reader | // print tag list coming back from reader | ||
System.out.println(returnVal); | System.out.println(returnVal); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} catch (Exception e) { | } catch (Exception e) { | ||
− | |||
e.printStackTrace(); | e.printStackTrace(); | ||
} | } | ||
Line 211: | Line 185: | ||
} | } | ||
− | |||
</pre> | </pre> | ||
+ | |||
=Edit the Activator= | =Edit the Activator= | ||
=Run the program= | =Run the program= |
Revision as of 17:35, 19 November 2008
This page describes how to create an instance of the Rifidi engine, create readers, and add and remove tags from that engine all programatically via RMI. It is a tutorial for creating a "hello world" Rifidi application just to show you how the engine works. In production code, however, you shouldn't run the program from the activator.
Contents
Download the source code
In order to use the Engine API, you will first need to download the core rifidi plugins from svn. See Running Rifidi from Source for instructions on how to do this
Create a new plugin
Because Rifidi is OSGi based, all code must be written inside an OSGi bundle. The thing you will need to create a new OSGi bundle for your code to run in. To do this, follow the same steps as in Create a new reader project. You do not need to add the project to svn. In addition, you may need to add dependencies to other plugins, such as org.rifidi.services.tags
Create a program
- Create a new package.
- Right click on the package found in the src directory.
- Select new package
- Give the package a name and select Finish
- Create a new class
- Right Click on the new package
- Select new class
- Give the class a name and click finish
Write the Hello World Application
package testproj.program; import gnu.cajo.utils.extra.TransparentItemProxy; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; import org.rifidi.emulator.reader.module.GeneralReaderPropertyHolder; import org.rifidi.emulator.rmi.server.ReaderModuleManagerInterface; import org.rifidi.emulator.rmi.server.RifidiManager; import org.rifidi.emulator.rmi.server.RifidiManagerInterface; import org.rifidi.services.tags.enums.TagGen; import org.rifidi.services.tags.factory.TagCreationPattern; import org.rifidi.services.tags.factory.TagFactory; import org.rifidi.services.tags.id.TagType; import org.rifidi.services.tags.impl.RifidiTag; /** * This class demonstrates how to use the Rifidi Engine RMI API to create a * virtual reader and add tags to it. * * @author Kyle Neumeier - kyle@pramari.com * */ public class RifidiClass { public void HelloWorld() { try { /** * The IP address of the virtual reader */ String READER_IP = "127.0.0.1"; /** * The port of the virtual reader */ int READER_PORT = 20000; /** * The IP of the RMI server */ String RMI_IP = "127.0.0.1"; /** * The IP of the RMI port */ int RMI_PORT = 1198; // Start up the RMI Server RifidiManager.startManager(RMI_IP, RMI_PORT); // Get the remote object that allows you to create and destroy // virtual readers from the RMI registry RifidiManagerInterface managerInterface = (RifidiManagerInterface) TransparentItemProxy .getItem("//" + RMI_IP + ":" + RMI_PORT + "/" + RifidiManager.URL, new Class[] { RifidiManagerInterface.class }); // The GeneralReaderPropertyHolder is the object that defines a new // virtual reader to create GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder(); readerProperties .setReaderClassName("org.rifidi.emulator.reader.alien.module.AlienReaderModule"); readerProperties.setNumAntennas(2); readerProperties.setReaderName("alien_test"); readerProperties.setProperty("inet_address", READER_IP + ":" + READER_PORT); readerProperties.setProperty("heartbeat_address", READER_IP + ":54321"); readerProperties.setNumGPIs(4); readerProperties.setNumGPOs(8); // create the virtual reader on the remote engine managerInterface.createReader(readerProperties); // get the remote object that allows you to control the virtual // reader ReaderModuleManagerInterface remoteVirtualReader = (ReaderModuleManagerInterface) TransparentItemProxy .getItem("//" + RMI_IP + ":" + RMI_PORT + "/" + readerProperties.getReaderName(), new Class[] { ReaderModuleManagerInterface.class }); // turn the reader on so that you can connect to it from a client // (e.g. telnet) remoteVirtualReader.turnReaderOn(); // The TagCreationPattern is the object that defines how to create a // batch of tags TagCreationPattern tagPattern = new TagCreationPattern(); tagPattern.setNumberOfTags(5); tagPattern.setTagGeneration(TagGen.GEN2); tagPattern.setTagType(TagType.SGTIN96); // uses the TagFactory to generate some new tags ArrayList<RifidiTag> tags = TagFactory.generateTags(tagPattern); // Set the Unique IDs for each of the tags. This is *not* the EPC. // The UniqueID is the ID used internally to differentiate between // tags. long id = 1; for (RifidiTag tag : tags) { tag.setTagEntitiyID(id++); } // add the tags to the virtual reader remoteVirtualReader.addTags(0, tags); // sleep for 500 miliseconds Thread.sleep(500); /* * The next bit of code is "client" code. It is equivalent to * connecting to an alien reader over telnet and seding the 'get * taglist' command. It is only shown here to demonstrate that the * tags that reader has been turned on and tags were added * successfully. */ // create a new socket connection to the reader Socket connection = new Socket(READER_IP, READER_PORT); BufferedReader in = new BufferedReader(new InputStreamReader( connection.getInputStream())); PrintWriter out = new PrintWriter(connection.getOutputStream()); // send username and password System.out.println(readFromReader(in)); out.write("alien\n"); out.flush(); System.out.println(readFromReader(in)); Thread.sleep(500); out.write("password\n"); out.flush(); System.out.println(readFromReader(in)); // send get taglist command out.write("t\n"); out.flush(); String returnVal = readFromReader(in); // print tag list coming back from reader System.out.println(returnVal); } catch (Exception e) { e.printStackTrace(); } } /** * This method reads from the socket. * * @param inBuf * @return The string that was read * @throws IOException */ public String readFromReader(BufferedReader inBuf) throws IOException { StringBuffer buf = new StringBuffer(); int ch = inBuf.read(); while ((char) ch != '\0') { buf.append((char) ch); ch = inBuf.read(); } return buf.toString(); } }