From 61ca41e1d26575c25f6799ff72f3a012e1486fc9 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 19 Apr 2016 21:15:55 +0300 Subject: [PATCH] [no commit message] --- numass-storage/numass-client/build.gradle | 2 +- .../inr/numass/scripts/TestClient.groovy | 2 +- .../src/main/java/inr/numass/client/Cli.java | 145 ++++++++++++++++++ .../java/inr/numass/client/NumassClient.java | 118 +------------- 4 files changed, 150 insertions(+), 117 deletions(-) create mode 100644 numass-storage/numass-client/src/main/java/inr/numass/client/Cli.java diff --git a/numass-storage/numass-client/build.gradle b/numass-storage/numass-client/build.gradle index a4ff06da..e4c413ee 100644 --- a/numass-storage/numass-client/build.gradle +++ b/numass-storage/numass-client/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'application' if (!hasProperty('mainClass')) { - ext.mainClass = 'inr.numass.client.NumassClient' + ext.mainClass = 'inr.numass.client.Cli' } mainClassName = mainClass diff --git a/numass-storage/numass-client/src/main/groovy/inr/numass/scripts/TestClient.groovy b/numass-storage/numass-client/src/main/groovy/inr/numass/scripts/TestClient.groovy index 599e5842..36c68b89 100644 --- a/numass-storage/numass-client/src/main/groovy/inr/numass/scripts/TestClient.groovy +++ b/numass-storage/numass-client/src/main/groovy/inr/numass/scripts/TestClient.groovy @@ -37,7 +37,7 @@ while(line == null || !line.startsWith("exit")){ // print ">" line = br.readLine(); if(line!= null && !line.startsWith("exit")){ - NumassClient.runComand("127.0.0.1", 8335, line.split(" ")); + Cli.runComand("127.0.0.1", 8335, line.split(" ")); } } diff --git a/numass-storage/numass-client/src/main/java/inr/numass/client/Cli.java b/numass-storage/numass-client/src/main/java/inr/numass/client/Cli.java new file mode 100644 index 00000000..79c3d3d6 --- /dev/null +++ b/numass-storage/numass-client/src/main/java/inr/numass/client/Cli.java @@ -0,0 +1,145 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package inr.numass.client; + +import hep.dataforge.meta.Meta; +import hep.dataforge.storage.commons.StorageManager; +import hep.dataforge.values.Value; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Map; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.slf4j.LoggerFactory; + +/** + * + * @author Alexander Nozik + */ +public class Cli { + + public static void main(String[] args) { + new StorageManager().startGlobal(); + + Options options = buildOptions(); + + CommandLineParser parser = new DefaultParser(); + + CommandLine cli; + try { + cli = parser.parse(options, args, false); + } catch (ParseException ex) { + System.out.println("Error: command line"); + LoggerFactory.getLogger("NumassClient").error("Error while parsing command line", ex); + System.exit(1); + return; + } + + runComand(cli.getOptionValue("a", "192.168.11.1"), Integer.valueOf(cli.getOptionValue("p", "8335")), cli.getArgs()); + + } + + public static void runComand(String ip, int port, String... args) { + checkArgLength(1, args); + try (NumassClient client = new NumassClient(ip, port)) { + switch (args[0]) { + case "getRun": + Meta getRun = client.getCurrentRun(); + if (getRun.getBoolean("success", true)) { + System.out.println(getRun.getString("run.path")); + } else { + System.out.println("Error: operaton failed"); + } + return; + case "setRun": + checkArgLength(2, args); + Meta setRun = client.startRun(args[1]); + if (setRun.getBoolean("success", true)) { + System.out.println(setRun.getString("run.path")); + } else { + System.out.println("Error: operaton failed"); + } + return; + case "getState": + checkArgLength(2, args); + String stateName = args[1]; + Map states = client.getStates(stateName); + if (states != null) { + System.out.println(states.get(stateName).stringValue()); + } else { + System.out.println("Error: operaton failed"); + } + return; + case "setState": + checkArgLength(3, args); + String setStateName = args[1]; + String setStateValue = args[2]; + Meta setStateMeta = client.setState(setStateName, setStateValue); + if (setStateMeta.getBoolean("success", true)) { + System.out.println("OK"); + } else { + System.out.println("Error: operaton failed"); + } + return; + case "pushPoint": + checkArgLength(2, args); + String path; + String fileName; + if (args.length == 2) { + path = ""; + fileName = args[1]; + } else { + path = args[1]; + fileName = args[2]; + } + + Meta pushPoint = client.sendNumassData(path, fileName); +// LoggerFactory.getLogger("Numass-client").debug(pushPoint.toString()); + if (pushPoint.getBoolean("success", true)) { + System.out.println("OK"); + } else { + System.out.println("Error: operaton failed"); + } + return; + case "addNote": +// checkArgLength(2, args); +// String note = args[1]; + String note = new BufferedReader(new InputStreamReader(System.in)).readLine(); + Meta addNote = client.addNote(note, null); + if (addNote.getBoolean("success", true)) { + System.out.println("OK"); + } else { + System.out.println("Error: operaton failed"); + } + } + + } catch (IOException ex) { + System.out.println("Error: connection failed"); + LoggerFactory.getLogger("NumassClient").error("Error while initializing connection", ex); + System.exit(1); + } + } + + private static void checkArgLength(int length, String... args) { + if (args.length < length) { + LoggerFactory.getLogger("NumassClient").error("Command line to short"); + System.exit(1); + } + } + + private static Options buildOptions() { + Options options = new Options(); + + options.addOption("a", true, "IP address of the server. Default: 192.168.111.1"); + options.addOption("p", true, "Server port. Default: 8335"); + + return options; + } +} diff --git a/numass-storage/numass-client/src/main/java/inr/numass/client/NumassClient.java b/numass-storage/numass-client/src/main/java/inr/numass/client/NumassClient.java index 5d91c7dd..c2ff5cd8 100644 --- a/numass-storage/numass-client/src/main/java/inr/numass/client/NumassClient.java +++ b/numass-storage/numass-client/src/main/java/inr/numass/client/NumassClient.java @@ -26,11 +26,14 @@ import hep.dataforge.storage.commons.StorageManager; import hep.dataforge.storage.commons.StorageUtils; import hep.dataforge.values.Value; import inr.numass.storage.NumassStorage; +import java.io.BufferedInputStream; +import java.io.BufferedReader; import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.nio.ByteBuffer; @@ -221,120 +224,5 @@ public class NumassClient extends MessageFactory implements Closeable { return sendAndRecieve(env.build()).meta(); } - public static void main(String[] args) { - new StorageManager().startGlobal(); - Options options = buildOptions(); - - CommandLineParser parser = new DefaultParser(); - - CommandLine cli; - try { - cli = parser.parse(options, args, false); - } catch (ParseException ex) { - System.out.println("Error: command line"); - LoggerFactory.getLogger("NumassClient").error("Error while parsing command line", ex); - System.exit(1); - return; - } - - runComand(cli.getOptionValue("a", "192.168.11.1"), Integer.valueOf(cli.getOptionValue("p", "8335")), cli.getArgs()); - - } - - public static void runComand(String ip, int port, String... args) { - checkArgLength(1, args); - try (NumassClient client = new NumassClient(ip, port)) { - switch (args[0]) { - case "getRun": - Meta getRun = client.getCurrentRun(); - if (getRun.getBoolean("success", true)) { - System.out.println(getRun.getString("run.path")); - } else { - System.out.println("Error: operaton failed"); - } - return; - case "setRun": - checkArgLength(2, args); - Meta setRun = client.startRun(args[1]); - if (setRun.getBoolean("success", true)) { - System.out.println(setRun.getString("run.path")); - } else { - System.out.println("Error: operaton failed"); - } - return; - case "getState": - checkArgLength(2, args); - String stateName = args[1]; - Map states = client.getStates(stateName); - if (states != null) { - System.out.println(states.get(stateName).stringValue()); - } else { - System.out.println("Error: operaton failed"); - } - return; - case "setState": - checkArgLength(3, args); - String setStateName = args[1]; - String setStateValue = args[2]; - Meta setStateMeta = client.setState(setStateName, setStateValue); - if (setStateMeta.getBoolean("success", true)) { - System.out.println("OK"); - } else { - System.out.println("Error: operaton failed"); - } - return; - case "pushPoint": - checkArgLength(2, args); - String path; - String fileName; - if (args.length == 2) { - path = ""; - fileName = args[1]; - } else { - path = args[1]; - fileName = args[2]; - } - - Meta pushPoint = client.sendNumassData(path, fileName); -// LoggerFactory.getLogger("Numass-client").debug(pushPoint.toString()); - if (pushPoint.getBoolean("success", true)) { - System.out.println("OK"); - } else { - System.out.println("Error: operaton failed"); - } - return; - case "addNote": - checkArgLength(2, args); - String note = args[1]; - Meta addNote = client.addNote(note, null); - if (addNote.getBoolean("success", true)) { - System.out.println("OK"); - } else { - System.out.println("Error: operaton failed"); - } - } - - } catch (IOException ex) { - System.out.println("Error: connection failed"); - LoggerFactory.getLogger("NumassClient").error("Error while initializing connection", ex); - System.exit(1); - } - } - - private static void checkArgLength(int length, String... args) { - if (args.length < length) { - LoggerFactory.getLogger("NumassClient").error("Command line to short"); - System.exit(1); - } - } - - private static Options buildOptions() { - Options options = new Options(); - - options.addOption("a", true, "IP address of the server. Default: 192.168.111.1"); - options.addOption("p", true, "Server port. Default: 8335"); - - return options; - } }