00001
00002
00003
00004
package org.nees.rbnb;
00005
00006
import java.io.IOException;
00007
00008
import com.rbnb.sapi.*;
00009
00010
import com.rbnb.utility.ArgHandler;
00011
00012
00013
import com.rbnb.utility.RBNBProcess;
00014
00015
00019 public class StringSink {
00020
00021
private static final String SERVER =
"localhost:3333";
00022
private static final String SINK_NAME =
"GetSting";
00023
private static final String SOURCE_NAME =
"Command";
00024
private static final String CHANNEL_NAME =
"CommandData";
00025
00026
private String server = SERVER;
00027
private String sinkName = SINK_NAME;
00028
private String sourceName = SOURCE_NAME;
00029
private String channelName = CHANNEL_NAME;
00030
private String requestPath = sourceName +
"/" + channelName;
00031
00032 Sink sink = null;
00033 ChannelMap sMap;
00034
int index;
00035
boolean connected =
false;
00036
00037 Thread stringDataThread;
00038
boolean runit =
false;
00039
00040
public static void main(String[] args) {
00041
StringSink w =
new StringSink(args);
00042 w.
exec();
00043 w.
startThread();
00044 }
00045
00046
private void printUsage() {
00047 System.out.println(
"StringSink: usage is...");
00048 System.out.println(
"StringSink ");
00049 System.out.println(
"[-s server_hostname *" + SERVER +
"] ");
00050 System.out.println(
"[-k Sink Name *" + SINK_NAME +
" ]");
00051 System.out.println(
"[-n source_name *" + SOURCE_NAME +
"] ");
00052 System.out.println(
"[-c channel_name *" + CHANNEL_NAME +
"] ");
00053 }
00054
00055
public StringSink(String[] args) {
00056
00057
try {
00058 ArgHandler ah=
new ArgHandler(args);
00059
if (ah.checkFlag(
'h')) {
00060 printUsage();
00061 RBNBProcess.exit(0);
00062 }
00063
if (ah.checkFlag(
's')) {
00064 String a=ah.getOption(
's');
00065
if (a!=null) server=a;
00066 }
00067
if (ah.checkFlag(
'n')) {
00068 String a=ah.getOption(
'n');
00069
if (a!=null) sourceName=a;
00070 }
00071
if (ah.checkFlag(
'c')) {
00072 String a=ah.getOption(
'c');
00073
if (a!=null) channelName=a;
00074 }
00075
if (ah.checkFlag(
'k')) {
00076 String a=ah.getOption(
'k');
00077
if (a!=null) sinkName=a;
00078 }
00079 }
catch (Exception e) {
00080 System.err.println(
"StringSink argument exception "+e.getMessage());
00081 e.printStackTrace();
00082 RBNBProcess.exit(0);
00083 }
00084
00085 requestPath = sourceName +
"/" + channelName;
00086
00087 System.out.println(
"Starting StringSink on " + server +
" as " + sinkName);
00088 System.out.println(
" Requesting " + requestPath);
00089 System.out.println(
" Use StringSink -h to see optional parameters");
00090 }
00091
00092
public void exec()
00093 {
00094
try {
00095
00096 sink=
new Sink();
00097 sink.OpenRBNBConnection(server,sinkName);
00098 sMap =
new ChannelMap();
00099 index = sMap.Add(requestPath);
00100 sink.Subscribe(sMap,
"newest");
00101 connected =
true;
00102 System.out.println(
"StringSink: Connection made to server = "
00103 + server +
" as " + sinkName
00104 +
" requesting " + requestPath +
".");
00105 }
catch (SAPIException se) { se.printStackTrace(); }
00106 }
00107
00108
public void startThread()
00109 {
00110
00111
if (!connected)
return;
00112
00113
00114 Runnable r =
new Runnable() {
00115
public void run() {
00116 runWork();
00117 }
00118 };
00119 runit =
true;
00120 stringDataThread =
new Thread(r,
"StringData");
00121 stringDataThread.start();
00122 System.out.println(
"StringSink: Started thread.");
00123 }
00124
00125
public void stopThread()
00126 {
00127 runit =
false;
00128 stringDataThread.interrupt();
00129 System.out.println(
"StringSink: Stopped thread.");
00130 }
00131
00132
private void runWork ()
00133 {
00134
try {
00135
while(isRunning())
00136 {
00137 ChannelMap m = sink.Fetch(-1);
00138
if (m == null)
00139 {
00140 System.out.println(
"Data fetch failed.");
00141
continue;
00142 }
00143 String[] st = m.GetDataAsString(index);
00144 System.out.println(
"Command(s) Received: ");
00145
for (
int i = 0; i < st.length; i++)
00146 {
00147 System.out.println(st[i]);
00148 }
00149 }
00150 }
catch (SAPIException se) {
00151 se.printStackTrace();
00152 }
00153 stringDataThread = null;
00154 }
00155
00156
public boolean isRunning()
00157 {
00158
return (connected && runit);
00159 }
00160
00161
00162 }