X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=src%2Fkernel%2Fkernel.cpp;h=ee5e9c753a5630e23e25d28114d35bc478b82258;hb=02fadcf3ea33e60f2d8badbc8d83758c91a5c34d;hp=3e3d41be93e347d862ae373e4732c0d4f19fe6bb;hpb=efb9fd8fce0c1c80c05e2206893ca197b7999ca3;p=vlp.git diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 3e3d41b..ee5e9c7 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -65,14 +65,23 @@ #include #include "kernel.h" -#include "ui/kernelwindow.h" -#include "lock.h" -#include "options.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 /* File resides in top directory (where are Makefiles)*/ #include "../../config.h" +namespace loglan { +namespace vlp { + char CharLine[25] = "________________________"; /** @@ -137,7 +146,7 @@ QKernel::QKernel(int argc, char **argv) q.mkpath(getRemoteDir()); } - info_messages = TRUE; + info_messages = actionInfo_messages->isChecked(); wait_for_info = FALSE; setWindowTitle(PACKAGE_NAME); @@ -148,11 +157,15 @@ QKernel::QKernel(int argc, char **argv) freeINTid = 1; ActiveConnections = 0; strcpy(LockPasswd, ""); + loadConfig(getConfigFilePath()); + RunNetModule(); Net_Notify = new QSocketNotifier(net_sock, QSocketNotifier::Read, this); connect(Net_Notify, SIGNAL(activated(int)), this, SLOT(NetMessage())); + + WriteMessage("\n " PACKAGE_STRING ": READY \n"); } QString QKernel::getConfigFilePath() @@ -190,94 +203,19 @@ void QKernel::loadConfig(const QString & fname) */ void QKernel::loadConfig(const char * fname) { - fprintf(stderr, "2: %s\n", fname); - config_t cfg; - config_setting_t *setting; + loglan::vlp::Config config; + if(config.load(fname)) { + NodeNumber = config.getNodeNumber(); + ConType = config.getConnectionType(); - /* Hack for checking if file exists without using external libs.*/ - - FILE * file = fopen(fname, "rt"); - if (!file) { - fprintf(stderr, "Error: Cannot load configuration file: %s!\n", - fname); - exit(3); - } - /* File exists, so file has been locked. Release it. */ - - config_init(&cfg); - - /* Read the file. If there is an error, report it and exit. */ - if (!config_read(&cfg, file)) { - fprintf(stderr, "%s! In file %s, line %d\n", - config_error_text(&cfg), - config_error_file(&cfg), - config_error_line(&cfg)); - config_destroy(&cfg); - fclose(file); - exit(3); - } - setting = config_lookup(&cfg, "node_number"); - if (setting) { - NodeNumber = config_setting_get_int(setting); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "node_number"); - config_destroy(&cfg); - fclose(file); - exit(3); - } - - setting = config_lookup(&cfg, "type"); - if (setting) { - /* same as strcmp(..) == 0 */ - if (!strcmp(config_setting_get_string(setting), "explicit")) { - ConType = 1; - } else { - ConType = 2; - } - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "type"); - } - - setting = config_lookup(&cfg, "host"); - if (setting) { - switch(config_setting_type(setting)) { - /* TODO: Deprecated. Made for back compatibility. */ - case CONFIG_TYPE_STRING: - ConnectList.append(new ConnectEntry((char*) - config_setting_get_string(setting))); - break; - case CONFIG_TYPE_ARRAY: { - int size = config_setting_length(setting); - for (int i = 0; i < size; i++) { - ConnectList.append(new ConnectEntry((char*) - config_setting_get_string_elem(setting, - i))); - } - break; - } - default: - fprintf(stderr, "%s! In file %s, bad entry type for %s." - " Will not be read.\n", - "Error", fname, "host"); + std::vector hosts = config.getHosts(); + for (unsigned int i = 0; i < hosts.size(); i++) { + ConnectList.append(new ConnectEntry(hosts[i].c_str())); } - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "host"); - } - setting = config_lookup(&cfg, "progdir"); - if (setting){ - strncpy(progdir, config_setting_get_string(setting), 256); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "progdir"); + strncpy(progdir, config.getProgramDir(), 256); } - - config_destroy(&cfg); - fclose(file); } /** @@ -286,11 +224,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); @@ -308,6 +245,7 @@ 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(), @@ -322,6 +260,13 @@ void QKernel::on_actionEditor_triggered() WriteMessage("Executing logedit failed!"); } } + else if (pid < 0) { + WriteMessage("fork(logedit) failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + exit(3); + } } /** @@ -335,6 +280,8 @@ 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(), @@ -350,6 +297,13 @@ void QKernel::on_actionHelp_triggered() WriteMessage("Executing loghelp failed!"); } } + else if (pid < 0) { + WriteMessage("fork(loghelp) failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + exit(3); + } } /** @@ -357,10 +311,11 @@ void QKernel::on_actionHelp_triggered() */ void QKernel::RunGraphModule(char *sk) { - char cmd[255]; - 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(), @@ -394,6 +349,7 @@ void QKernel::RunGraphModule(char *sk) */ void QKernel::RunNetModule() { + struct sockaddr_un svr; int len; int on; @@ -402,6 +358,7 @@ 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(), @@ -461,30 +418,15 @@ void QKernel::RunNetModule() */ void QKernel::on_actionConnect_triggered() { - QDialog d(this, Qt::Dialog); - QLabel lab("IP Address:", &d); - QLineEdit ed("", &d); - QPushButton ob("", &d); - QPushButton cb("", &d); MESSAGE m; - ob.setGeometry(30, 60, 80, 30); - ob.setText("Ok"); - ob.setDefault(TRUE); - lab.setGeometry(10, 10, 60, 30); - lab.setText("Address"); - ed.setGeometry(70, 10, 140, 30); - cb.setGeometry(130, 60, 80, 30); - cb.setText("Cancel"); - d.resize(240, 100); - - connect(&ob, SIGNAL(clicked()), &d, SLOT(accept())); - connect(&cb, SIGNAL(clicked()), &d, SLOT(reject())); - if (d.exec()) { + dialog::ConnectDialog dialog(this); + + if (dialog.exec()) { m.msg_type = MSG_NET; m.param.pword[0] = NET_CONNECT_TO; - strcpy(m.param.pstr, ed.text().toAscii().data()); - write(net_sock, &m, sizeof(MESSAGE)); + strcpy(m.param.pstr, dialog.getAddress().toStdString().c_str()); + write(net_sock, &m, sizeof(MESSAGE)); } } @@ -540,48 +482,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)); } } @@ -593,41 +504,23 @@ void QKernel::on_actionMessage_triggered() */ void QKernel::on_actionKill_triggered() { - QDialog *dlg; - QLineEdit *nodenr; - MESSAGE m; - InterpEntry *pom; - - dlg = new QDialog(this, Qt::Dialog); - dlg->setWindowTitle("Kill interpreter"); - - nodenr = new QLineEdit("", dlg); - nodenr->setGeometry(90, 10, 50, 30); - - QLabel * tmpQLabel = new QLabel("Interp. ID:", dlg); - tmpQLabel->setGeometry(10, 10, 77, 30); - - QPushButton * ob = new QPushButton("Kill", dlg); - ob->setGeometry( 160, 10, 80, 30); - ob->setDefault(TRUE); - - QPushButton * cb = new QPushButton("Cancel", dlg); - cb->setGeometry(260, 10, 80, 30); - dlg->resize(360, 50); - - connect(ob, SIGNAL(clicked()), dlg, SLOT(accept())); - connect(cb, SIGNAL(clicked()), dlg, SLOT(reject())); + dialog::KillInterpreterDialog dialog(this); - if (dlg->exec()) { + if (dialog.exec()) { + MESSAGE m; m.msg_type = MSG_INT; m.param.pword[0] = INT_KILL; - pom = findINTbyID(atoi(nodenr->text().toAscii().data())); - if (pom != NULL) { - if (!(pom->remote)) - write(pom->sock, &m, sizeof(MESSAGE)); - else - WriteMessage("This is a remote instance of " - "a program!"); + + InterpEntry *interpreter; + interpreter = findINTbyID(dialog.getInterpreterId()); + + if (interpreter != NULL) { + if (!(interpreter->remote)) { + write(interpreter->sock, &m, sizeof(MESSAGE)); + } + else { + WriteMessage("This is a remote instance of a program!"); + } } else { WriteMessage("Interpreter not found"); @@ -644,11 +537,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); @@ -666,17 +560,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:"); @@ -761,6 +655,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; @@ -820,12 +715,12 @@ void QKernel::IntMessage(int sock) * Writes message to kernel logger. * @parame msg String with message to log */ -void QKernel::WriteMessage(char *msg) +void QKernel::WriteMessage(const char *msg) { int x; - int y; +// int y; x = desktop->textCursor().blockNumber(); - y = desktop->textCursor().columnNumber(); +// y = desktop->textCursor().columnNumber(); if (x > 100) { desktop->clear(); @@ -853,9 +748,8 @@ void QKernel::WriteMessage(char *msg) void QKernel::on_actionInfo_messages_triggered() { if (toolsMenu != NULL) { - info_messages = !actionInfo_messages->isChecked(); + info_messages = actionInfo_messages->isChecked(); actionInfo_messages->setChecked(info_messages); - fprintf(stderr, "Info is checked? %s\n", info_messages ? "yes" : "no"); } } @@ -866,7 +760,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()); @@ -881,12 +775,12 @@ 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(); if (lockDialog.getPassword().size() > 0) { - strcpy(LockPasswd, password.toAscii().data()); + strcpy(LockPasswd, password.toStdString().c_str()); lockDialog.retype(); if (lockDialog.exec()) { @@ -915,29 +809,11 @@ void QKernel::on_actionLock_console_triggered() */ void QKernel::on_actionUnlock_console_triggered() { - QDialog d(this, Qt::Dialog); - d.setWindowTitle("Enter password"); - - QLabel lab("Password:", &d); - lab.setGeometry(10, 10, 60, 30); - - QLineEdit ed("", &d); - ed.setGeometry(70, 10, 140, 30); - ed.setEchoMode(QLineEdit::Password); + dialog::LockDialog lockDialog(this); - QPushButton ob("Ok", &d); - ob.setGeometry(30, 60, 80, 30); - ob.setDefault(TRUE); - connect(&ob, SIGNAL(clicked()), &d, SLOT(accept())); - - QPushButton cb("Cancel", &d); - cb.setGeometry(130, 60, 80, 30); - connect(&cb, SIGNAL(clicked()), &d, SLOT(reject())); - - d.resize(240, 100); - - if (d.exec()) { - if (strcmp(ed.text().toAscii().data(), LockPasswd) == 0) { + if (lockDialog.exec()) { + QString password = lockDialog.getPassword(); + if (strcmp(password.toStdString().c_str(), LockPasswd) == 0) { setLocked(FALSE); WriteMessage("CONSOLE UNLOCKED"); } else { @@ -949,14 +825,6 @@ void QKernel::on_actionUnlock_console_triggered() } } -/** - * Writes init message in kernel - */ -void QKernel::InitMessage() -{ - WriteMessage("\n " PACKAGE_STRING ": READY \n"); -} - /** * Finds Interpreter by its socket * @param _id ID of the socket @@ -966,9 +834,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; } } @@ -985,9 +853,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; } } @@ -1006,6 +874,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; @@ -1117,7 +986,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; @@ -1172,6 +1040,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"); @@ -1222,6 +1091,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 @@ -1234,9 +1106,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(); - kernel.InitMessage(); return app->exec(); -} +} \ No newline at end of file