Sirit Example Client
From RifidiWiki
UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY
This class shows how to create a program to talk to the Sirit INfinity 510 reader.
<pre> /*
* SimpleSiritClient.java * * Created: 27.05.2009 * Project: RiFidi SiritClient * http://www.rifidi.org * http://rifidi.sourceforge.net * Copyright: Pramari LLC and the Rifidi Project * License: Lesser GNU Public License (LGPL) * http://www.opensource.org/licenses/lgpl-license.html * Author: Stefan Fahrnbauer - stefan@pramari.com */
package sandbox;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;
/**
* @author Stefan Fahrnbauer - stefan@pramari.com * */
public class SimpleSiritClient {
String ipAddress = "192.168.11.130"; Socket sock; int port = 50007; BufferedReader reader; PrintWriter writer;
Future<String> future;
/** * @param args */ public static void main(String[] args) { SimpleSiritClient client = new SimpleSiritClient(); client.go(); }
/** * executes the application */ public void go() {
setUpNetworking();
String command = "";
// Login to reader command = "reader.login(login=admin,pwd=readeradmin)"; processCommand(command);
// let the reader scan for tags for 3 seconds command = "tag.db.scan_tags(3000)"; processCommand(command);
tearDown(); }
/** * initializes the connection */ public void setUpNetworking() { try { // connection for submitting commands sock = new Socket(this.ipAddress, this.port); reader = new BufferedReader(new InputStreamReader(sock .getInputStream())); writer = new PrintWriter(sock.getOutputStream()); } catch (IOException ex) { ex.printStackTrace(); } }
/** * closes the connection */ public void tearDown() { try { sock.close(); } catch (IOException ex) { ex.printStackTrace(); } }
/** * process command, get response and write to log */ public void processCommand(String command) {
// create callable to read response ExecutorService pool = Executors.newFixedThreadPool(2); // Callable for Commandchannel Callable<String> callable = new IncomingCallable(); future = pool.submit(callable);
// send command to reader sendCommand(command);
// write to log window System.out.println(">>" + command + "\r\n");
// receive and write response System.out.println(getLastResponse()); }
/** * sends command to reader */ public void sendCommand(String command) { // send command to reader writer.print(command + "\r\n"); writer.flush(); }
/** * waits for and returns the reader's response */ public String getLastResponse() { String response = ""; try { response = future.get(); } catch (Exception e) { e.printStackTrace(); } return response; }
public class IncomingCallable implements Callable<String> { PrintWriter writer; String command;
public String call() { // get response String message = ""; String lastResponse = ""; try { while (lastResponse.indexOf("\r\n\r\n") == -1) { message = reader.readLine(); message = message.replace("\r", "").replace("\n", "") + "\r\n"; lastResponse = lastResponse.concat(message); } } catch (Exception ex) { ex.printStackTrace(); } return lastResponse; } }
} </pre>