Using the Engine API

From RifidiWiki

Jump to: navigation, search

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.

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 OSGi bundle

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.

For this example, you will want to declare dependencies on the following plugins:

  • org.rifidi.emulator
  • org.rifidi.tags
  • org.rifidi.emulator.reader.alien

Create a program

  1. Create a new package.
    1. Right click on the package found in the src directory.
    2. Select new package
    3. Give the package a name and select Finish
  2. Create a new class
    1. Right Click on the new package
    2. Select new class
    3. Give the class a name and click finish

Write the Hello World Application

package testproj;

import java.util.ArrayList;
import java.util.HashSet;

import org.rifidi.emulator.manager.ReaderManager;
import org.rifidi.emulator.reader.module.GeneralReaderPropertyHolder;
import org.rifidi.tags.enums.TagGen;
import org.rifidi.tags.factory.TagCreationPattern;
import org.rifidi.tags.factory.TagFactory;
import org.rifidi.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 -
public class RifidiClass {

	 * The reader manager - the service that controls the virtual RFID readers
	private ReaderManager readerManager;

	public RifidiClass() {
		// tell the service registry to inject dependencies into this object

	private void startReader() {
		try {

			/** The IP address of the virtual reader */
			String READER_IP = "";
			/** The port of the virtual reader */
			int READER_PORT = 20000;
			String READER_NAME = "alien_test";

			// The GeneralReaderPropertyHolder is the object that defines a new
			// virtual reader to create
			GeneralReaderPropertyHolder readerProperties = new GeneralReaderPropertyHolder();
			readerProperties.setProperty("inet_address", READER_IP + ":"
					+ READER_PORT);
			readerProperties.setProperty("heartbeat_address", READER_IP
					+ ":54321");

			// create the virtual reader on the remote engine

			// turn the reader on so that you can connect to it from a client
			// (e.g. telnet)

			// The TagCreationPattern is the object that defines how to create a
			// batch of tags
			TagCreationPattern tagPattern = new TagCreationPattern();

			// 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) {

			// add the tags to the virtual reader
			readerManager.addTags(READER_NAME, 0, new HashSet<RifidiTag>(tags));

			// sleep for 500 miliseconds

			 * 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(
			PrintWriter out = new PrintWriter(connection.getOutputStream());

			// send username and password

			// send get taglist command
			String returnVal = readFromReader(in);

			// print tag list coming back from reader
		} catch (Exception e) {

	 * 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 =;
		while ((char) ch != '\0') {
			buf.append((char) ch);
			ch =;
		return buf.toString();

	 * This method is called by the service registry to inject the ReaderManager
	 * @param readerManager
	public void setReaderManager(ReaderManager readerManager) {
		this.readerManager = readerManager;
		// start the program now that we know the readerManager has been
		// injected.


Edit the Activator

The Activator is the class that the OSGi framework uses to start and stop the bundle that you wrote. The start() method is called when your bundle is activated, and the stop() method is called when the bundle is deactivated. For an introduction to OSGi see this article.

Although this is a bad idea for production code, for the sake of simplicity, the start() method in the activator for this project starts the hello world class. This is the activator for the bundle:

package testproj;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import testproj.RifidiClass;

public class Activator implements BundleActivator {

	 * (non-Javadoc)
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	public void start(BundleContext context) throws Exception {
		Thread t = new Thread(new TestRunner(), "Rifidi Hello World Thread");

	 * (non-Javadoc)
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	public void stop(BundleContext context) throws Exception {

	 * Run the program in a separate thread so we don't occupy the OSGi starter
	 * thread.
	 * @author Kyle Neumeier -
	private class TestRunner implements Runnable {

		public void run() {
			new RifidiClass();

Run the program

To run this program from Eclipse, you need to create a new Run Configuration.

  1. Click Run->Run Configurations
  2. Double click on OSGi Framework on the tree on the left
  3. Select the required bundles. The 'bundles' list displays a list of the bundles that will be activated when you run the program. You only want the bundles selected that are required.
    1. Click 'Deselct All'
    2. Check the bundle that you just created.
    3. Check all the bundles that start with 'org.springframework...'
    4. Select 'Add Required Bundles'
  4. Click 'Run'

You should see alot of output in the console, and at the bottom of the output, you should see the tags that you "client" got from the reader:

Tag:304A 37DA 04A4 4927 D1C8 0775, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2
Tag:3008 26FD CCAC 1E53 612D 56EB, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2
Tag:302E 5243 7D73 9CEF 1E8A 08FA, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2
Tag:307A 11F3 960A B58C 7454 B248, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2
Tag:308C 5BD0 BA14 CB02 55D4 89D5, Disc:2008/11/19 12:09:20, Last:2008/11/19 12:09:21, Count:1, Ant:0, Proto:2
Personal tools