|
@@ -1,170 +1,79 @@
|
|
|
+#include "../include/commands.hpp"
|
|
|
+#include "../include/machineiomanager.hpp"
|
|
|
+#include "../include/useriomanager.hpp"
|
|
|
+
|
|
|
#include <boost/asio.hpp>
|
|
|
#include <boost/program_options.hpp>
|
|
|
-#include <cctype>
|
|
|
#include <iostream>
|
|
|
-
|
|
|
-#define COMMANDLEN 10
|
|
|
-#define sizeofarr(a) (sizeof(a) / sizeof(a[0]))
|
|
|
+#include <string>
|
|
|
|
|
|
namespace bpo = boost::program_options;
|
|
|
-namespace bai = boost::asio;
|
|
|
-using bai::ip::tcp;
|
|
|
-
|
|
|
-typedef enum {
|
|
|
- CMD_HELP,
|
|
|
- CMD_CONNECT,
|
|
|
- CMD_DISCONNECT,
|
|
|
- CMD_PUT,
|
|
|
- CMD_REMOVE,
|
|
|
- CMD_GET,
|
|
|
- CMD_QUERY,
|
|
|
- CMD_SETUP,
|
|
|
- CMD_LOG,
|
|
|
- CMD_UNKNOWN
|
|
|
-} COMMANDTYPES;
|
|
|
-
|
|
|
-typedef struct {
|
|
|
- COMMANDTYPES cmd;
|
|
|
- const char *name;
|
|
|
- const char *desc;
|
|
|
-} CMD;
|
|
|
-
|
|
|
-CMD commands[]{{CMD_HELP, "help", "show help"},
|
|
|
- {CMD_CONNECT, "connect", "connect to IP"},
|
|
|
- {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
|
|
|
- {CMD_PUT, "put", "upload file to IP and add to queue"},
|
|
|
- {CMD_REMOVE, "remove",
|
|
|
- "remove file from IP and queue (stops xfer if required)"},
|
|
|
- {CMD_GET, "get", "retrieve file from IP"},
|
|
|
- {CMD_QUERY, "query", "query status of IP"},
|
|
|
- {CMD_SETUP, "setup", "configure server at IP"},
|
|
|
- {CMD_LOG, "log", "show log from IP"}};
|
|
|
-
|
|
|
-/* takes a string in str and checks if it is a valid command
|
|
|
- returns the matching COMMANDTYPES or CMD_UNKNOWN
|
|
|
- if isshort is nonzero, only the first character of str will be checked
|
|
|
-*/
|
|
|
-COMMANDTYPES getCommand(char *str, unsigned int isshort) {
|
|
|
- COMMANDTYPES ret = CMD_UNKNOWN;
|
|
|
- char temp[COMMANDLEN + 1] = {0};
|
|
|
- if (strlen(str) > COMMANDLEN)
|
|
|
- return ret;
|
|
|
- if (isshort)
|
|
|
- temp[0] = std::tolower(str[0]);
|
|
|
- else
|
|
|
- for (int i = 0; i < COMMANDLEN; i++)
|
|
|
- temp[i] = std::tolower(str[i]);
|
|
|
-
|
|
|
- for (int i = 0; i < sizeofarr(commands); i++) {
|
|
|
- if (isshort) {
|
|
|
- if (tolower(str[0]) == commands[i].name[0])
|
|
|
- ret = commands[i].cmd;
|
|
|
- } else {
|
|
|
- if (!strncmp(temp, commands[i].name, COMMANDLEN))
|
|
|
- ret = commands[i].cmd;
|
|
|
- }
|
|
|
- }
|
|
|
- return ret;
|
|
|
-}
|
|
|
|
|
|
void show_help(char *exec) {
|
|
|
std::printf("ccats command line interface\n");
|
|
|
- std::printf("usage: %s COMMAND IP [Options]\n", exec);
|
|
|
- std::printf("available COMMANDs are:\n");
|
|
|
- for (int i = 0; i < sizeofarr(commands); i++) {
|
|
|
- std::printf("%10s - %s\n", commands[i].name, commands[i].desc);
|
|
|
- }
|
|
|
+ std::printf("usage: %s IP [Options]\n", exec);
|
|
|
std::printf("IP should be in the format \"xxx.xxx.xxx.xxx\"\n");
|
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
bpo::options_description desc{"Options"};
|
|
|
- desc.add_options()("cooloption", "Cooloption to use with command FOON");
|
|
|
+ desc.add_options()("help", "show this help")(
|
|
|
+ "machine",
|
|
|
+ "switch to machine mode for I/O (not designed for user interaction)")(
|
|
|
+ "batch", bpo::value<std::string>(),
|
|
|
+ "run operations from arg as batch file");
|
|
|
+
|
|
|
bpo::variables_map vm;
|
|
|
- bai::io_service ios;
|
|
|
- tcp::socket socket(ios);
|
|
|
- boost::system::error_code err;
|
|
|
- bai::streambuf recvbuf;
|
|
|
+ unsigned int machine = 0;
|
|
|
+ const char *file = NULL;
|
|
|
+ IoManager *ioman;
|
|
|
|
|
|
- // handle no command
|
|
|
- if (argc < 2) {
|
|
|
+ if (argc < 2 || !std::strncmp(argv[1], "--help", 6)) {
|
|
|
show_help(argv[0]);
|
|
|
- exit(1);
|
|
|
+ std::cout << desc;
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
- // identify command, print error message if unknown
|
|
|
- COMMANDTYPES cmd = getCommand(argv[1], (strlen(argv[1]) == 1));
|
|
|
- switch (cmd) {
|
|
|
- case CMD_UNKNOWN:
|
|
|
- std::printf("unknown command\n");
|
|
|
- case CMD_HELP:
|
|
|
+ if (!isdigit(argv[1][0])) {
|
|
|
+ std::printf("invalid ip\n");
|
|
|
show_help(argv[0]);
|
|
|
std::cout << desc;
|
|
|
- exit(1);
|
|
|
- }
|
|
|
- if (argc < 3) {
|
|
|
- std::printf("not enough arguments\n");
|
|
|
- show_help(argv[0]);
|
|
|
- exit(1);
|
|
|
+ return 1;
|
|
|
}
|
|
|
|
|
|
- // parse additional arguments, possibly drop this in the future for full
|
|
|
- // interactivity
|
|
|
try {
|
|
|
- store(parse_command_line(argc - 2, argv + 2, desc), vm);
|
|
|
+ store(parse_command_line(argc - 1, argv + 1, desc), vm);
|
|
|
notify(vm);
|
|
|
|
|
|
if (vm.count("help")) {
|
|
|
+ show_help(argv[0]);
|
|
|
std::cout << desc;
|
|
|
- } else {
|
|
|
- std::printf("no additional options\n");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (vm.count("machine")) {
|
|
|
+ // enable machine/gui mode
|
|
|
+ machine = 1;
|
|
|
+ }
|
|
|
+ if (vm.count("batch")) {
|
|
|
+ // handle batch file mode
|
|
|
+ file = vm["batch"].as<std::string>().c_str();
|
|
|
}
|
|
|
} catch (const bpo::error &ex) {
|
|
|
std::fprintf(stderr, "%s\n", ex.what());
|
|
|
}
|
|
|
+ std::printf("ip %s machine mode is %d file is %s\n", argv[1], machine,
|
|
|
+ file ? file : "");
|
|
|
|
|
|
- // have enough valid arguments, work through them
|
|
|
- switch (cmd) {
|
|
|
- case CMD_CONNECT:
|
|
|
- std::printf("connecting to %s\n", argv[2]);
|
|
|
- socket.connect(tcp::endpoint(bai::ip::address::from_string(argv[2]), 1234));
|
|
|
- std::printf("connected to %s - sending hello\n", argv[2]);
|
|
|
- bai::write(socket, bai::buffer("Client CONNECT test\n"), err);
|
|
|
- if (!err) {
|
|
|
- std::printf("OK: sent message\n");
|
|
|
- } else {
|
|
|
- std::printf("ERR: couldnt send message: %s\n", err.message().c_str());
|
|
|
- }
|
|
|
- bai::read(socket, recvbuf, bai::transfer_all(), err);
|
|
|
- if (err && err != bai::error::eof) {
|
|
|
- std::printf("ERR: couldnt recieve message: %s\n", err.message().c_str());
|
|
|
- } else {
|
|
|
- std::printf("OK: recieved message %s\n",
|
|
|
- bai::buffer_cast<const char *>(recvbuf.data()));
|
|
|
- }
|
|
|
- break;
|
|
|
- case CMD_DISCONNECT:
|
|
|
- std::printf("disconnecting from %s\n", argv[2]);
|
|
|
- socket.connect(tcp::endpoint(bai::ip::address::from_string(argv[2]), 1234));
|
|
|
- std::printf("connected to %s - sending goodbye\n", argv[2]);
|
|
|
- bai::write(socket, bai::buffer("Client DISCONNECT test\n"), err);
|
|
|
- if (!err) {
|
|
|
- std::printf("OK: sent message\n");
|
|
|
- } else {
|
|
|
- std::printf("ERR: couldnt send message: %s\n", err.message().c_str());
|
|
|
- }
|
|
|
- bai::read(socket, recvbuf, bai::transfer_all(), err);
|
|
|
- if (err && err != bai::error::eof) {
|
|
|
- std::printf("ERR: couldnt recieve message: %s\n", err.message().c_str());
|
|
|
- } else {
|
|
|
- std::printf("OK: recieved message %s\n",
|
|
|
- bai::buffer_cast<const char *>(recvbuf.data()));
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- std::printf("command %s not implemented\n", argv[1]);
|
|
|
- break;
|
|
|
+ if (machine) {
|
|
|
+ ioman = new MachineIoManager(argv[1]);
|
|
|
+ } else {
|
|
|
+ ioman = new UserIoManager(argv[1]);
|
|
|
}
|
|
|
|
|
|
+ // ‘MachineIoManager::MachineIoManager(char*&)’
|
|
|
+ if (ioman->connect()) {
|
|
|
+ ioman->run();
|
|
|
+ }
|
|
|
+ delete ioman;
|
|
|
std::printf("done\n");
|
|
|
}
|