|
@@ -11,12 +11,17 @@
|
|
#include <readline/history.h>
|
|
#include <readline/history.h>
|
|
#include <readline/readline.h>
|
|
#include <readline/readline.h>
|
|
|
|
|
|
|
|
+#include <poll.h>
|
|
|
|
+
|
|
using std::string;
|
|
using std::string;
|
|
using std::vector;
|
|
using std::vector;
|
|
|
|
|
|
using boost::asio::buffer;
|
|
using boost::asio::buffer;
|
|
using boost::asio::ip::tcp;
|
|
using boost::asio::ip::tcp;
|
|
|
|
|
|
|
|
+/* this will be provided by main.cpp for the readline callback */
|
|
|
|
+extern IoMan *gIOMAN;
|
|
|
|
+
|
|
IoMan::IoMan(char *ipcstring) : cmdman(fileman) {
|
|
IoMan::IoMan(char *ipcstring) : cmdman(fileman) {
|
|
ipstring = std::string(ipcstring);
|
|
ipstring = std::string(ipcstring);
|
|
port = 1234;
|
|
port = 1234;
|
|
@@ -140,6 +145,7 @@ bool IoMan::init() {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/* loop to fetch data from the network, doing light preprocessing on it to be handled by responseMain */
|
|
void IoMan::networkMain() {
|
|
void IoMan::networkMain() {
|
|
vector<Json::Value> toput;
|
|
vector<Json::Value> toput;
|
|
char *recvjson;
|
|
char *recvjson;
|
|
@@ -226,6 +232,7 @@ void IoMan::networkMain() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/* loop to handle input from the user and responseMain, sending data via network if required */
|
|
void IoMan::inputMain() {
|
|
void IoMan::inputMain() {
|
|
vector<string> toprocess;
|
|
vector<string> toprocess;
|
|
string command;
|
|
string command;
|
|
@@ -359,6 +366,7 @@ void IoMan::inputMain() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/* loop to handle responses that have been fetched by netMain and possibly add new commands to be handled by inputMain */
|
|
void IoMan::responseMain() {
|
|
void IoMan::responseMain() {
|
|
vector<Json::Value> toprocess;
|
|
vector<Json::Value> toprocess;
|
|
vector<string> toput;
|
|
vector<string> toput;
|
|
@@ -469,14 +477,49 @@ void IoMan::responseMain() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/* this is the handler that readlines alternative interface will use to process user input */
|
|
|
|
+void ioman_readlineHandler(char* line) {
|
|
|
|
+ vector<string> tokens;
|
|
|
|
+ if(!line){
|
|
|
|
+ printf("\nNULLBURGER\n");
|
|
|
|
+ gIOMAN->mainmutex.lock();
|
|
|
|
+ gIOMAN->runmain = 0;
|
|
|
|
+ gIOMAN->mainmutex.unlock();
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ // split input line into tokens
|
|
|
|
+ boost::algorithm::split(tokens, std::string(line),
|
|
|
|
+ boost::algorithm::is_any_of(" "),
|
|
|
|
+ boost::algorithm::token_compress_on);
|
|
|
|
+ if (strlen(line) && tokens.size()) {
|
|
|
|
+ add_history(line);
|
|
|
|
+
|
|
|
|
+ gIOMAN->localmutex.lock();
|
|
|
|
+ gIOMAN->printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"),
|
|
|
|
+ gIOMAN->debug);
|
|
|
|
+ gIOMAN->localinput.push_back(line);
|
|
|
|
+ gIOMAN->localmutex.unlock();
|
|
|
|
+ gIOMAN->printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"),
|
|
|
|
+ gIOMAN->debug);
|
|
|
|
+ gIOMAN->localcv.notify_all();
|
|
|
|
+ }
|
|
|
|
+ free(line);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* main user input loop */
|
|
void IoMan::run() {
|
|
void IoMan::run() {
|
|
printMessage("IoMan::run() begin", debug);
|
|
printMessage("IoMan::run() begin", debug);
|
|
- char *line = NULL, *user = NULL, *pass = NULL;
|
|
|
|
|
|
+ //~ char *line = NULL, *user = NULL, *pass = NULL;
|
|
|
|
+ char *user = NULL, *pass = NULL;
|
|
vector<string> tokens;
|
|
vector<string> tokens;
|
|
string work, command;
|
|
string work, command;
|
|
CmdMan::CmdRet cmdret;
|
|
CmdMan::CmdRet cmdret;
|
|
Json::Value root;
|
|
Json::Value root;
|
|
std::unique_lock<std::mutex> ulock;
|
|
std::unique_lock<std::mutex> ulock;
|
|
|
|
+ struct pollfd inPipeStatus;
|
|
|
|
+ inPipeStatus.fd = STDIN_FILENO;
|
|
|
|
+ inPipeStatus.events = POLLIN;
|
|
|
|
|
|
runmain = true;
|
|
runmain = true;
|
|
|
|
|
|
@@ -509,50 +552,25 @@ void IoMan::run() {
|
|
return;
|
|
return;
|
|
initmutex.unlock();
|
|
initmutex.unlock();
|
|
initcv.notify_all();
|
|
initcv.notify_all();
|
|
|
|
+
|
|
|
|
+ rl_callback_handler_install(getCmdPrompt().c_str(), (rl_vcpfunc_t*) &ioman_readlineHandler);
|
|
|
|
|
|
mainmutex.lock();
|
|
mainmutex.lock();
|
|
while (runmain) {
|
|
while (runmain) {
|
|
mainmutex.unlock();
|
|
mainmutex.unlock();
|
|
- free(line);
|
|
|
|
-
|
|
|
|
- line = readline(getCmdPrompt().c_str());
|
|
|
|
-
|
|
|
|
- if (!runmain)
|
|
|
|
- break;
|
|
|
|
|
|
+
|
|
|
|
+ poll(&inPipeStatus, 1, 100);
|
|
|
|
|
|
- // if no input, do not add to history, and go to reading next line
|
|
|
|
- if (strlen(line) == 0) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // split input line into tokens
|
|
|
|
- boost::algorithm::split(tokens, std::string(line),
|
|
|
|
- boost::algorithm::is_any_of(" "),
|
|
|
|
- boost::algorithm::token_compress_on);
|
|
|
|
-
|
|
|
|
- // if input contains only spaces, do not add to history, and go to reading
|
|
|
|
- // next line
|
|
|
|
- if (tokens.size() < 1) {
|
|
|
|
- continue;
|
|
|
|
|
|
+ if (inPipeStatus.revents & POLLIN) {
|
|
|
|
+ rl_callback_read_char();
|
|
}
|
|
}
|
|
-
|
|
|
|
- // add the line to history
|
|
|
|
- add_history(line);
|
|
|
|
-
|
|
|
|
- localmutex.lock();
|
|
|
|
- printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"),
|
|
|
|
- debug);
|
|
|
|
- localinput.push_back(line);
|
|
|
|
- localmutex.unlock();
|
|
|
|
- printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"),
|
|
|
|
- debug);
|
|
|
|
- localcv.notify_all();
|
|
|
|
-
|
|
|
|
if (!connected)
|
|
if (!connected)
|
|
break;
|
|
break;
|
|
mainmutex.lock();
|
|
mainmutex.lock();
|
|
}
|
|
}
|
|
mainmutex.unlock();
|
|
mainmutex.unlock();
|
|
|
|
+
|
|
|
|
+ // Remove the handler
|
|
|
|
+ rl_callback_handler_remove();
|
|
|
|
|
|
- free(line);
|
|
|
|
}
|
|
}
|