Options options = new Options();
addOptions(options);
CommandLineParser parser = new PosixParser();
CommandLine commandLine = null;
IServiceStatistics default_statistics = new ServiceStatistics(false, false, "<N/A>");
try {
commandLine = parser.parse(options, args);
} catch (ParseException e) {
print("Cannot parse command line:", e);
return;
}
String arguments = commandLine.getOptionValue(ServicePing.Arguments.decode());
String pingClass = commandLine.getOptionValue(ServicePing.Class.decode());
String endpoint = commandLine.getOptionValue(ServicePing.Endpoint.decode());
String port = commandLine.getOptionValue(ServicePing.Port.decode());
Socket sock = null;
try {
sock = new Socket("localhost", Integer.parseInt(port));
} catch (NumberFormatException e2) {
e2.printStackTrace();
return;
} catch (UnknownHostException e2) {
e2.printStackTrace();
return;
} catch (IOException e2) {
e2.printStackTrace();
return;
}
print ("ServicePingMain listens on port", sock.getLocalPort());
InputStream sock_in = null;
OutputStream sock_out = null;
try {
sock_in = sock.getInputStream();
sock_out = sock.getOutputStream();
} catch (IOException e2) {
e2.printStackTrace();
return;
}
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(sock_out);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AServicePing custom = resolve(pingClass, arguments, endpoint);
if ( custom == null ) {
print("bad_pinger:", pingClass, endpoint);
return;
}
while ( true ) {
if ( debug ) print("ServicePingMeta starts ping.");
byte[] cmd = new byte[1];
cmd[0] = 0;
int eof = 0;
try {
eof = sock_in.read(cmd);
} catch (IOException e) {
handleError(e);
}
if ( debug ) print("Read cmd", new String(cmd), "eof", eof);
if ( eof == -1 ) {
print("EOF on input pipe. Exiting");
custom.stop();
return;
}
try {
if ( cmd[0] == 'P' ) {
IServiceStatistics ss = custom.getStatistics();
if ( ss == null ) {
ss = default_statistics;
}
// print("Is alive: " + ss.isAlive());
oos.writeObject(ss);