ebusctl: interactive mode added.

This commit is contained in:
Roland Jax
2014-11-25 11:35:53 +01:00
parent 73eeef99f6
commit 68c15ce66e
7 changed files with 173 additions and 37 deletions
+1 -1
View File
@@ -117,7 +117,7 @@ void* Connection::run()
break;
// removed closed socket
if (datalen <= 0 || strcasecmp(data, "QUIT") == 0)
if (datalen <= 0 || strncasecmp(data, "QUIT", 4) == 0)
break;
// send data
+1 -1
View File
@@ -195,7 +195,6 @@ public:
bool missingCommand() const { return (m_command.size() == 0 ? true : false); }
private:
/** private constructor - singleton pattern */
/**
* @brief private construtor.
* @param command is true if an command is needed.
@@ -213,6 +212,7 @@ private:
/**
* @brief private = operator.
* @param reference to an instance.
* @return reference to instance.
*/
Appl& operator=(const Appl&);
+14 -1
View File
@@ -51,9 +51,22 @@ public:
bool status() { return m_status; }
private:
/** private constructor - singleton pattern */
/**
* @brief private construtor.
*/
Daemon() {}
/**
* @brief private copy construtor.
* @param reference to an instance.
*/
Daemon(const Daemon&);
/**
* @brief private = operator.
* @param reference to an instance.
* @return reference to instance.
*/
Daemon& operator=(const Daemon&);
/** status of process; true if we are a daemon */
+16 -1
View File
@@ -304,13 +304,28 @@ public:
void stop();
private:
/** private constructor - singleton pattern */
/**
* @brief private construtor.
*/
Logger() {}
/**
* @brief private copy construtor.
* @param reference to an instance.
*/
Logger(const Logger&);
/**
* @brief private = operator.
* @param reference to an instance.
* @return reference to instance.
*/
Logger& operator=(const Logger&);
/** typedefs for a vector of type LogSink* */
typedef vector<LogSink*> sink_t;
/** typedefs for a vector of type LogSink* iterator */
typedef vector<LogSink*>::iterator sinkCI_t;
/** vector of available logging sinks */
+3 -1
View File
@@ -32,8 +32,10 @@ class TCPSocket
{
public:
/** grant access for friend classes */
/** grant access for friend class TCPClient */
friend class TCPClient;
/** grant access for friend class TCPServer */
friend class TCPServer;
/**
+130 -29
View File
@@ -25,6 +25,7 @@
#include "tcpsocket.h"
#include <iostream>
#include <cstdlib>
#include <sstream>
using namespace std;
@@ -46,6 +47,75 @@ void define_args()
}
enum CommandType {
ct_open,
ct_exit,
ct_help,
ct_invalid
};
CommandType getCase(const string& item)
{
if (strcasecmp(item.c_str(), "OPEN") == 0) return ct_open;
if (strcasecmp(item.c_str(), "EXIT") == 0) return ct_exit;
if (strcasecmp(item.c_str(), "HELP") == 0) return ct_help;
return ct_invalid;
}
bool connect(const char* host, int port, bool once=true)
{
TCPClient* client = new TCPClient();
TCPSocket* socket = client->connect(host, port);
if (socket != NULL) {
do {
string message;
if (once == false) {
cout << "online: ";
getline(cin, message);
}
else {
message = A.getCommand();
for (int i = 0; i < A.numArgs(); i++) {
message += " ";
message += A.getArg(i);
}
}
socket->send(message.c_str(), message.size());
if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) {
char data[1024];
size_t datalen;
datalen = socket->recv(data, sizeof(data)-1);
data[datalen] = '\0';
cout << data;
}
break;
} while (once == false);
delete socket;
}
else
cout << "error connecting to " << host << ":" << port << endl;
delete client;
if (once == false)
return false;
return true;
}
int main(int argc, char* argv[])
{
// define arguments and application variables
@@ -56,37 +126,68 @@ int main(int argc, char* argv[])
if (A.missingCommand() == true) {
cout << "interactive mode started." << endl;
exit(EXIT_FAILURE);
bool running = true;
do {
string input, token;
vector<string> cmd;
cout << "$: ";
getline(cin, input);
// prepare input
istringstream stream(input);
while (getline(stream, token, ' ') != 0)
cmd.push_back(token);
if (cmd.size() == 0)
cout << "command missing" << endl;
switch (getCase(cmd[0])) {
case ct_invalid:
cout << "command not found" << endl;
break;
case ct_open:
{
bool ret = true;
cout << "connect to..." << endl;
if (cmd.size() == 1)
ret = connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"), false);
else if (cmd.size() == 2)
ret = connect(cmd[1].c_str(), A.getOptVal<int>("port"), false);
else if (cmd.size() == 3)
ret = connect(cmd[1].c_str(), atoi(cmd[2].c_str()), false);
else
cout << "open [host [port]]" << endl;
running = ret;
}
break;
case ct_exit:
running = false;
break;
case ct_help:
cout << "commands:" << endl
<< " open - open connection to ebusd 'open [host [port]]'" << endl
<< " exit - exit ebusctl" << endl
<< " help - print this page" << endl;
break;
default:
break;
}
} while (running == true);
exit(EXIT_SUCCESS);
}
TCPClient* client = new TCPClient();
TCPSocket* socket = client->connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"));
if (socket != NULL) {
// build message
string message(A.getCommand());
for (int i = 0; i < A.numArgs(); i++) {
message += " ";
message += A.getArg(i);
}
socket->send(message.c_str(), message.size());
char data[1024];
size_t datalen;
datalen = socket->recv(data, sizeof(data)-1);
data[datalen] = '\0';
cout << data;
delete socket;
}
else
cout << "error connecting to " << A.getOptVal<const char*>("server")
<< ":" << A.getOptVal<int>("port") << endl;
delete client;
connect(A.getOptVal<const char*>("server"), A.getOptVal<int>("port"));
exit(EXIT_SUCCESS);
}
+8 -3
View File
@@ -36,14 +36,14 @@ void define_args()
{
A.setVersion("ebusfeed is part of """PACKAGE_STRING"");
A.addText(" 'ebusfeed' sends a raw dump file to a local serial device (pts)\n\n"
A.addText(" 'ebusfeed' sends a dump file to a local serial device (pts)\n\n"
" Usage: 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n"
" 2. create symbol links to appropriate devices\n"
" for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n"
" 'ln -s /dev/pts/3 /dev/ttyUSB20'\n"
" 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n"
" 4. start ebusfeed: 'ebusfeed /path/to/raw_file'\n\n"
"Command: '/path/to/raw_file'\n\n"
" 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n\n"
"Command: '/path/to/ebus_dump.bin'\n\n"
"Options:\n");
A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory,
@@ -62,6 +62,11 @@ int main(int argc, char* argv[])
// parse arguments
A.parseArgs(argc, argv);
if (A.missingCommand() == true) {
cout << "ebus dump file is required." << endl;
exit(EXIT_FAILURE);
}
string dev(A.getOptVal<const char*>("device"));
Port port(dev, true);