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