Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "../common/consts.h"
00022 #include "../msg/msg.h"
00023 #include "../utils/utils.h"
00024 #include <dashel/dashel.h>
00025 #include <time.h>
00026 #include <iostream>
00027 #include <cstring>
00028
00029 namespace Aseba
00030 {
00031 using namespace Dashel;
00032 using namespace std;
00033
00038
00040 class Exec : public Hub
00041 {
00042 private:
00043 const unsigned messageId;
00044 const char *programName;
00045
00046 public:
00047 Exec(const unsigned messageId, const char *programName) :
00048 messageId(messageId),
00049 programName(programName)
00050 {
00051 }
00052
00053 protected:
00054
00055 void incomingData(Stream *stream)
00056 {
00057 Message *message = Message::receive(stream);
00058
00059 if (message->type == messageId)
00060 {
00061 const int ret(system(programName));
00062 if (ret == -1)
00063 cerr << "Executable " << programName << " cannot be run" << endl;
00064 else if (ret > 0)
00065 cerr << "Warning, execution of " << programName << " returned value " << ret << endl;
00066 }
00067 }
00068 };
00069
00071 }
00072
00073
00075 void DumpHelp(std::ostream &stream, const char *programName)
00076 {
00077 stream << "Aseba Exec, execute an external command upon a given message, usage:\n";
00078 stream << programName << " MSG_ID PROG_NAME [options] [targets]*\n";
00079 stream << "Options:\n";
00080 stream << "-h, --help : shows this help\n";
00081 stream << "-V, --version : shows the version number\n";
00082 stream << "Targets are any valid Dashel targets." << std::endl << std::endl;
00083 stream << "Report bugs to: aseba-dev@gna.org" << std::endl;
00084 }
00085
00087 void DumpVersion(std::ostream &stream)
00088 {
00089 stream << "Aseba Exec " << ASEBA_VERSION << std::endl;
00090 stream << "Aseba protocol " << ASEBA_PROTOCOL_VERSION << std::endl;
00091 stream << "Licence LGPLv3: GNU LGPL version 3 <http://www.gnu.org/licenses/lgpl.html>\n";
00092 }
00093
00094 int main(int argc, char *argv[])
00095 {
00096 std::vector<std::string> targets;
00097
00098 if (argc < 3)
00099 {
00100 DumpHelp(std::cerr, argv[0]);
00101 return 1;
00102 }
00103
00104 const unsigned msgId(atoi(argv[1]));
00105 const char *programName(argv[2]);
00106 int argCounter = 3;
00107 while (argCounter < argc)
00108 {
00109 const char *arg = argv[argCounter];
00110
00111 if ((strcmp(arg, "-h") == 0) || (strcmp(arg, "--help") == 0))
00112 {
00113 DumpHelp(std::cout, argv[0]);
00114 return 0;
00115 }
00116 else if ((strcmp(arg, "-V") == 0) || (strcmp(arg, "--version") == 0))
00117 {
00118 DumpVersion(std::cout);
00119 return 0;
00120 }
00121 else
00122 {
00123 targets.push_back(argv[argCounter]);
00124 }
00125 argCounter++;
00126 }
00127
00128 if (targets.empty())
00129 targets.push_back(ASEBA_DEFAULT_TARGET);
00130
00131 try
00132 {
00133 Aseba::Exec Exec(msgId, programName);
00134 for (size_t i = 0; i < targets.size(); i++)
00135 Exec.connect(targets[i]);
00136 Exec.run();
00137 }
00138 catch(Dashel::DashelException e)
00139 {
00140 std::cerr << e.what() << std::endl;
00141 }
00142
00143 return 0;
00144 }