Init commit with echo and a kind of log

This commit is contained in:
Christos Houtouridis
2018-11-27 01:06:41 +02:00
commit e2a6319055
82 changed files with 21992 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
package net.hoo2.auth.vmodem;
import ithakimodem.*;
class Com {
static final int SPEED_DEFAULT = 9600;
static final int TIMEOUT_DEFAULT = 2000;
static final String URL_DEFAULT = "ithaki";
Com () {
modem_ = new Modem();
speed_ = SPEED_DEFAULT;
timeout_ = TIMEOUT_DEFAULT;
modem_.setSpeed(speed_);
modem_.setTimeout(timeout_);
}
Com (int speed, int timeout) {
modem_ = new Modem();
speed_ = speed;
timeout_ = timeout;
modem_.setSpeed(speed_);
modem_.setTimeout(timeout_);
}
// get/set
int timeout () { return timeout_; }
void timeout (int t) {
timeout_ = t;
modem_.setTimeout(timeout_);
}
int speed () { return speed_; }
void speed (int speed) {
speed_ = speed;
modem_.setSpeed(speed_);
}
boolean open (String url) { return modem_.open(url); }
boolean open () { return modem_.open(URL_DEFAULT); }
boolean close() { return modem_.close(); }
Transaction request (Transaction data, boolean ask, byte[] delimiter) {
int ch =0;
int i =0;
_clear (data.response);
if (ask) {
modem_.write(data.code);
modem_.write((int)'\r');
data.departure = System.currentTimeMillis() - (long)((8*(data.code.length+1))*(1000.0/speed_));
}
do {
try {
ch = modem_.read();
}
catch (Exception e) {
System.out.println (e.getMessage());
}
if (i == 0)
data.arrival = System.currentTimeMillis() - (long)(8*(1000.0/speed_));
data.response [i++] = (byte)ch;
} while (!_detect (data.response, "\r\n\n\n".getBytes())
&& !_detect (data.response, "NO CARRIER".getBytes())
&& !_detect (data.response, delimiter)
&& ch != -1);
return data;
}
private boolean _detect (byte[] response, byte[] pattern) {
if (pattern != null) {
for (int i =0 ; i<response.length - pattern.length ; ++i) {
boolean detected = true;
for (int j=0 ; j<pattern.length ; ++j) {
if (response[i+j] != pattern[j]) {
detected = false;
break;
}
}
if (detected)
return true;
}
}
return false;
}
private void _clear (byte[] buffer) {
for (int i=0 ; i<buffer.length ; ++i)
buffer[i] = 0;
}
private Modem modem_;
private int speed_;
private int timeout_;
}
class Transaction {
byte[] code;
byte[] response;
long departure;
long arrival;
Transaction (byte[] code, byte[] response) {
this.code = code;
this.response = response;
departure = arrival = 0;
}
}
+78
View File
@@ -0,0 +1,78 @@
package net.hoo2.auth.vmodem;
import java.io.IOException;
import java.io.PrintWriter;
class Echo {
static final int ECHO_DURATION_DEFAULT = 60;
static final int ECHO_CODE_SIZE = 5;
static final int ECHO_BUFFER_SIZE = 256;
static final int ECHO_RESPONSE_SIZE = 35;
static final String ECHO_DELIMITER = "PSTOP";
private Com com_;
private Transaction transaction_;
private int duration_;
private String logfile_;
Echo (Com com, byte[] code, int duration, String logfile) {
com_ = com;
duration_ = duration;
transaction_= new Transaction(new byte[ECHO_CODE_SIZE],
new byte[ECHO_BUFFER_SIZE]);
transaction_.code = code.clone();
logfile_ = logfile;
}
void run (boolean verbose) {
boolean init;
long start;
long now;
PrintWriter writer = null;
String line;
line = "Running echo with: " + new String(transaction_.code);
System.out.println(line);
if (logfile_ != null) {
try {
writer = new PrintWriter(logfile_);
writer.println(line);
}
catch (IOException exp) {
System.err.println( "Open log file failed: " + exp.getMessage() );
return;
}
}
init = true;
start = System.currentTimeMillis();
do {
if (init == true) {
transaction_ = com_.request (transaction_, false, null);
init = false;
line = new String(transaction_.response);
if (verbose) {
System.out.println(line);
}
}
else {
transaction_ = com_.request(transaction_, true, ECHO_DELIMITER.getBytes());
line = new String(transaction_.code)
+ ": "
+ new String(transaction_.response).substring(0, 35)
+ " Resp.time= "
+ (transaction_.arrival - transaction_.departure)
+ " [msec]";
if (logfile_ != null) writer.println(line);
if (verbose) System.out.println(line);
}
now = System.currentTimeMillis();
} while (now - start < duration_*1000);
try {
if (writer != null)
writer.close();
} catch (Exception ex) {/*ignore*/}
}
}
+148
View File
@@ -0,0 +1,148 @@
/**
* @file VirtualModem.java
* @brief
* Contain the Main class for the project VirtualModem
*
* @author Christos Choutouridis AEM:8997
* @email cchoutou@ece.auth.gr
*/
package net.hoo2.auth.vmodem;
/** @name imports */
/** @{ */
import org.apache.commons.cli.*;
/** @} */
/**
* @class VirtualModem
*
* @brief This is the main control class of the program.
*
* This class includes the main function.Using this class's api
* the user can ...
*/
public class VirtualModem {
/** @name Data */
/** @{ */
CommandLine line;
CommandLineParser parser;
Options options;
HelpFormatter formatter;
Com com;
String logfile;
boolean verbose;
/** @} */
/** @name constructors */
/** @{ */
public VirtualModem () {
parser = new DefaultParser();
options = new Options();
formatter = new HelpFormatter();
com = new Com();
logfile = null;
verbose = false;
// line is initialized in getCmdOptions()
Option verbose = new Option ("v", "verbose", false, "Be more verbose");
Option help = new Option ("h", "help", false, "Print this message");
Option timeout = Option.builder("t")
.longOpt("timeout")
.hasArg()
.valueSeparator('=')
.desc("Select timeout in [sec]")
.build();
Option speed = Option.builder("s")
.longOpt("speed")
.hasArg()
.valueSeparator('=')
.desc("Select speed in [bps]")
.build();
Option log = Option.builder("l")
.longOpt("log")
.hasArg()
.desc("Log file name")
.build();
Option echo = Option.builder("e")
.longOpt("echo")
.numberOfArgs(2)
.desc ("Request echo sequence")
.build();
options.addOption(verbose);
options.addOption(help);
options.addOption(timeout);
options.addOption(speed);
options.addOption(log);
options.addOption(echo);
}
/** @} */
private boolean getCmdOptions (String[] args) {
try {
// parse the command line arguments
line = parser.parse (options, args);
}
catch( ParseException exp ) {
// oops, something went wrong
System.err.println( "Parsing command line failed: " + exp.getMessage() );
return false;
}
return true;
}
private boolean commandDispatcher () {
// Get boolean options first
if (line.hasOption("verbose")) {
verbose = true;
}
// get options
if (line.hasOption("timeout")) {
com.timeout(Integer.parseInt(line.getOptionValue("timeout")));
}
if (line.hasOption("speed")) {
com.speed(Integer.parseInt(line.getOptionValue("speed")));
}
if (line.hasOption("log")) {
logfile = line.getOptionValue("log");
}
// Execution dispatcher
if (line.hasOption("help")) {
formatter.printHelp( "virtualModem", options );
return true;
}
if (line.hasOption("echo")) {
Echo e = new Echo(com,
line.getOptionValues("echo")[0].getBytes(),
Integer.valueOf(line.getOptionValues("echo")[1]),
logfile);
if (com.open() == true) {
e.run(verbose);
com.close();
}
}
else {
System.err.println ("Error: Unrecognized option");
return false;
}
return true;
}
/**
* @brief Main
*
*/
public static void main(String[] args) {
// allocate the main object
VirtualModem vmodem = new VirtualModem();
// prepare command line input
if (vmodem.getCmdOptions (args) != true)
return;
if (vmodem.commandDispatcher() != true)
return;
}
}