Handling search of config file in multiple directories, additional debug messages
[vlp.git] / src / kernel / kernel.cpp
index c6a9d1795d252fd286c8ca2266cb406fa141bf01..1c0b7921b96806dd5accaecddd8398e0973090b9 100644 (file)
 #include <X11/Xlib.h>
 
 #include "kernel.h"
-#include "ui/kernelwindow.h"
-#include "lock.h"
-#include "options.h"
-#include "kill_interpreter_dialog.h"
-#include "connect_dialog.h"
+#include "ui/KernelWindow.h"
+#include "ConnectDialog.h"
+#include "KillDialog.h"
+#include "LockDialog.h"
+#include "OptionsDialog.h"
+#include "MessageDialog.h"
 
 #include "vlp/config.h"
+#include "vlp/QtConfigurationFinder.h"
+#include "vlp/exception/ConfigFileNotFound.h"
 
+#include <sys/prctl.h>
 /* File resides in top directory (where are Makefiles)*/
 #include "../../config.h"
 
 
+namespace loglan {
+namespace vlp {
+
 char CharLine[25] = "________________________";
 
 /**
@@ -121,12 +128,6 @@ QKernel::QKernel(int argc, char **argv)
 {
        setupUi(this);
 
-       QString arg0(argv[0]);
-       arg0 += "/";
-       homeDir = QDir(arg0);
-       homeDir.cdUp();
-
-       
        int i;
        for(i = 0; (i < 5) && (i < argc-1); i++) {
                strcpy(myargs[i], "");
@@ -135,6 +136,9 @@ QKernel::QKernel(int argc, char **argv)
                }
        }
 
+       loglan::vlp::QtConfigurationFinder configFinder;
+       configFinder.initSearchDirs();
+
        QDir q(getRemoteDir());
 
        if (!q.exists()) {
@@ -153,7 +157,7 @@ QKernel::QKernel(int argc, char **argv)
        ActiveConnections = 0;
        strcpy(LockPasswd, "");
 
-       loadConfig(getConfigFilePath());
+       loadConfig(configFinder.findConfig().c_str());
 
        RunNetModule();
 
@@ -210,6 +214,8 @@ void QKernel::loadConfig(const char * fname)
                }
 
                strncpy(progdir, config.getProgramDir(), 256);
+
+               homeDir = QDir(QCoreApplication::applicationDirPath());
        }
 }
 
@@ -219,11 +225,10 @@ void QKernel::loadConfig(const char * fname)
  */
 void QKernel::on_actionExecute_triggered()
 {
-       int i;
        QString s = QFileDialog::getOpenFileName(this, "Execute", progdir, "*.log");
 
        if (!s.isNull()) {
-               i = s.indexOf(".log");
+               int i = s.indexOf(".log");
 
                if (i > 0)
                        s.remove(i, 4);
@@ -241,9 +246,11 @@ void QKernel::on_actionEditor_triggered()
        QString program = getHomeDir();
        program += "/modules/logedit";
 
+       fprintf(stderr, "Run EDIT Module: %s, %s\n", program.toStdString().c_str(), getHomeDir());
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
+                       "logedit",
                        getHomeDir(),
                        myargs[0],
                        myargs[1],
@@ -275,9 +282,12 @@ void QKernel::on_actionHelp_triggered()
        QString docDir = getHomeDir();
        docDir += "/doc";
 
+       fprintf(stderr, "Run HELP Module: %s, %s\n", program.toStdString().c_str(), docDir.toStdString().c_str());
+
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
+                       "loghelp",
                        docDir.toStdString().c_str(),
                        myargs[0],
                        myargs[1],
@@ -307,9 +317,12 @@ void QKernel::RunGraphModule(char *sk)
        QString program = getHomeDir();
        program += "/modules/loggraph";
 
+       fprintf(stderr, "Run GRAPH Module: %s, %s\n", program.toStdString().c_str(), sk);
+
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
+                       "loggraph",
                        program.toStdString().c_str(),
                        sk,
                        myargs[0],
@@ -340,6 +353,7 @@ void QKernel::RunGraphModule(char *sk)
  */
 void QKernel::RunNetModule()
 {
+
        struct sockaddr_un svr;
        int len;
        int on;
@@ -348,10 +362,11 @@ void QKernel::RunNetModule()
        QString program = getHomeDir();
        program += "/modules/lognet";
 
+       fprintf(stderr, "Run NET Module: %s, %s %s\n", program.toStdString().c_str(), getNetModuleSocket(), getConfigFilePath().toStdString().c_str());
        pid_t pid = fork();
        if (pid == 0) {
                if (execl(program.toStdString().c_str(),
-                       program.toStdString().c_str(),
+                       "lognet",
                        getNetModuleSocket(),
                        getConfigFilePath().toStdString().c_str(),
                        myargs[0],
@@ -409,8 +424,7 @@ void QKernel::on_actionConnect_triggered()
 {
        MESSAGE m;
 
-       ConnectDialog dialog(this);
-       dialog.setWindowTitle("IP Address:");
+       dialog::ConnectDialog dialog(this);
 
        if (dialog.exec()) {
                m.msg_type = MSG_NET;
@@ -472,48 +486,17 @@ void QKernel::on_actionQuit_triggered()
  */
 void QKernel::on_actionMessage_triggered()
 {
-       QDialog *dlg;
-       QLineEdit *nodenr;
+       dialog::MessageDialog dialog(this);
        MESSAGE m;
 
-       dlg = new QDialog(this, Qt::Dialog);
-       dlg->setWindowTitle("Send message to node");
-
-       nodenr = new QLineEdit("number", dlg);
-       nodenr->setGeometry(90, 10, 50, 30);
-       nodenr->setText("");
-
-       QLabel *tmpQLabel;
-       tmpQLabel = new QLabel("Node number:", dlg);
-       tmpQLabel->setGeometry(10, 10, 77, 30);
-
-       tmpQLabel = new QLabel("Message:", dlg);
-       tmpQLabel->setGeometry(10, 50, 70, 30);
-
-       QLineEdit *msg;
-       msg = new QLineEdit("", dlg);
-       msg->setGeometry(80, 60, 330, 30);
-
-       QPushButton *ob;
-       ob = new QPushButton("Send", dlg);
-       ob->setGeometry(230, 10, 80, 30);
-       ob->setDefault(TRUE);
-       
-       QPushButton *cb;
-       cb = new QPushButton("Cancel", dlg);
-       cb->setGeometry(330, 10, 80, 30);
-       dlg->resize(430, 110);
-       connect(ob, SIGNAL(clicked()), dlg, SLOT(accept()));
-       connect(cb, SIGNAL(clicked()), dlg, SLOT(reject()));
-
-       if (dlg->exec()) {
+       if (dialog.exec()) {
                m.msg_type = MSG_NET;
                m.param.pword[0] = NET_PROPAGATE;
                m.param.pword[1] = MSG_VLP;
                m.param.pword[2] = NodeNumber;
-               m.param.pword[4] = atoi(nodenr->text().toAscii().data());
+               m.param.pword[4] = dialog.getNodeNumber();
                m.param.pword[6] = VLP_WRITE;
-               strcpy(m.param.pstr, msg->text().toAscii().data());
+               strcpy(m.param.pstr, dialog.getMessage().toStdString().c_str());
                write(net_sock, &m, sizeof(MESSAGE));
        }
 }
@@ -525,21 +508,23 @@ void QKernel::on_actionMessage_triggered()
  */
 void QKernel::on_actionKill_triggered()
 {
-       KillInterpreterDialog dialog(this);
-       dialog.setWindowTitle("Kill interpreter");
-
-       MESSAGE m;
-       InterpEntry *interpreter;
+       dialog::KillInterpreterDialog dialog(this);
 
        if (dialog.exec()) {
+               MESSAGE m;
                m.msg_type = MSG_INT;
                m.param.pword[0] = INT_KILL;
+               
+               InterpEntry *interpreter;
                interpreter = findINTbyID(dialog.getInterpreterId());
+
                if (interpreter != NULL) {
-                       if (!(interpreter->remote))
+                       if (!(interpreter->remote)) {
                                write(interpreter->sock, &m, sizeof(MESSAGE));
-                       else
+                       }
+                       else {
                                WriteMessage("This is a remote instance of a program!");
+                       }
                }
                else {
                        WriteMessage("Interpreter not found");
@@ -549,6 +534,8 @@ void QKernel::on_actionKill_triggered()
 
 /**
  * Sends message to the net module.
+ *
+ * \todo method needs to be refactored
  */
 void QKernel::NetMessage()
 {
@@ -556,11 +543,12 @@ void QKernel::NetMessage()
        /* TODO: It has to be rewritten */
        MESSAGE msg;
        int cnt;
-       char ss[255];
        InterpEntry *pom;
 
        cnt = read(net_sock, &msg, sizeof(MESSAGE));
        if ((cnt > 0) && (msg.msg_type == MSG_NET)) {
+               char ss[255];
+
                switch(msg.param.pword[0]) {
                case NET_CSWRITELN:
                        WriteMessage(msg.param.pstr);
@@ -578,17 +566,17 @@ void QKernel::NetMessage()
                                case VLP_WRITE:
                                        QApplication::beep();
                                        WriteMessage(CharLine);
-                                       WriteMessage(
-                                               "### Incoming Messsage ###");
-                                       sprintf(ss, "Mesg from Node %d: %s",
-                                                       msg.param.pword[2],
-                                                       msg.param.pstr);
+                                       WriteMessage("### Incoming Messsage ###");
+                                       sprintf(ss,
+                                               "Mesg from Node %d: %s",
+                                               msg.param.pword[2],
+                                               msg.param.pstr
+                                       );
                                        WriteMessage(ss);
                                        WriteMessage(CharLine);
                                        break;
                                case VLP_REMOTE_INSTANCE:
-                                       sprintf(ss, "%s/%s", getRemoteDir(),
-                                                               msg.param.pstr);
+                                       sprintf(ss, "%s/%s", getRemoteDir(), msg.param.pstr);
 
                                        if (info_messages) { 
                                                WriteMessage("Running program:");
@@ -673,6 +661,7 @@ void QKernel::IntMessage(int sock)
                switch (msg.msg_type) { 
                case MSG_GRAPH:
                        if (msg.param.pword[0] == GRAPH_ALLOCATE) {
+                               fprintf(stderr, "Running graph module with arg: %s\n", msg.param.pstr);
                                RunGraphModule(msg.param.pstr);
                        }
                        break;
@@ -689,8 +678,6 @@ void QKernel::IntMessage(int sock)
                case MSG_INT:
                        switch(msg.param.pword[0]) {
                        case INT_EXITING:
-                               char ss[255];
-
                                MESSAGE m;
                                m.msg_type = MSG_VLP;
                                m.param.pword[0] = VLP_INTERPRETER_DOWN;
@@ -705,6 +692,7 @@ void QKernel::IntMessage(int sock)
                                delete e;
 
                                if (info_messages) {
+                                       char ss[255];
                                        sprintf(ss, "%s : End of program "
                                                "execution", msg.param.pstr);
                                        WriteMessage(ss);
@@ -777,7 +765,7 @@ void QKernel::on_actionInfo_messages_triggered()
  */
 void QKernel::on_actionOptions_triggered()
 {
-       OptionsDialog optionsDialog(getConfigFilePath(), this);
+       dialog::OptionsDialog optionsDialog(getConfigFilePath(), this);
        if (optionsDialog.exec()) {
                optionsDialog.saveConfig(getConfigFilePath());
 
@@ -792,7 +780,7 @@ void QKernel::on_actionOptions_triggered()
  */
 void QKernel::on_actionLock_console_triggered()
 {
-       LockDialog lockDialog(this);
+       dialog::LockDialog lockDialog(this);
 
        if (lockDialog.exec()) {
                QString password = lockDialog.getPassword();
@@ -826,7 +814,7 @@ void QKernel::on_actionLock_console_triggered()
  */
 void QKernel::on_actionUnlock_console_triggered()
 {
-       LockDialog lockDialog(this);
+       dialog::LockDialog lockDialog(this);
 
        if (lockDialog.exec()) {
                QString password = lockDialog.getPassword();
@@ -851,9 +839,9 @@ InterpEntry *QKernel::findINTbySocket(int _id)
 {
        InterpEntry *pom = NULL;
        
-       for (int i = 0; i < Interpreters.size(); i++) {
-               if (Interpreters.at(i)->sock == _id) {
-                       pom = Interpreters.at(i);
+       for (auto interpreter : Interpreters) {
+               if (interpreter->sock == _id) {
+                       pom = interpreter;
                        break;
                }
        }
@@ -870,9 +858,9 @@ InterpEntry *QKernel::findINTbyID(int _id)
 {
        InterpEntry *pom = NULL;
        
-       for (int i = 0; i < Interpreters.size(); i++) {
-               if (Interpreters.at(i)->ID == _id) {
-                       pom = Interpreters.at(i);
+       for (auto interpreter : Interpreters) {
+               if (interpreter->ID == _id) {
+                       pom = interpreter;
                        break;
                }
        }
@@ -891,6 +879,7 @@ InterpEntry *QKernel::findINTbyID(int _id)
  */
 InterpEntry *QKernel::RunIntModule(char *ss, int r)
 {
+       fprintf(stderr, "Run INT Module: %s, %d\n", ss, r);
        char a[256], b[255];
        struct sockaddr_un svr;
        int len, sock, on;
@@ -937,7 +926,7 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r)
        strcpy(newINT->shortname, b);
        strcpy(newINT->fullname, ss);
 
-       sprintf(a, "%s%d", IPATH, newint);
+       sprintf(a, "%s%d", homeDir.absoluteFilePath(IPATH).toStdString().c_str(), newint);
        sprintf(cmd, "%s/modules/logint %s %s",
                getHomeDir(),
                a,
@@ -945,9 +934,11 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r)
        if (r) {
                strcat(cmd, " r");
        }
-       sprintf(b, " %s %s %s %s %s",
-               myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
-       strcat(cmd, b);
+//     sprintf(b, " %s %s %s %s %s",
+//             myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]);
+//     sprintf(cmd, "%s %s", cmd, b);
+//     strcat(cmd, b);
+       fprintf(stderr, "%s\n", cmd);
        strcat(cmd, " &");
 
        sock = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -1002,7 +993,6 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r)
 void QKernel::RemoteInstance(InterpEntry *interp, int on)
 {
        MESSAGE m;
-       char s[255];
 
        m.msg_type = MSG_NET;
        m.param.pword[0] = NET_NODE_EXIST;
@@ -1057,6 +1047,7 @@ void QKernel::RemoteInstance(InterpEntry *interp, int on)
                m.param.pword[1] = interp->RInstances[on];
                write(interp->sock, &m, sizeof(MESSAGE));
        } else { /* There is no such a node! */
+               char s[255];
                sprintf(s, "Warning: Node number %d not found!", on); 
                WriteMessage(s);
                WriteMessage("Allocating O-process on the local node");
@@ -1107,6 +1098,9 @@ void QKernel::on_actionInfo_triggered()
        wait_for_info = TRUE;
 }
 
+}
+}
+
 /**
  * Program main function
  * All program arguments but the first one (argv[0]: program name) are saved and
@@ -1119,8 +1113,8 @@ int main(int argc, char **argv)
        XInitThreads();
 
        QApplication * app = new QApplication(argc, argv);
-       QKernel kernel(argc, argv);
+       loglan::vlp::QKernel kernel(argc, argv);
        kernel.show();
        
        return app->exec();
-}
+}
\ No newline at end of file