X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=src%2Fkernel%2Fkernel.cpp;h=6e9beec81fa6aa43de734df0c83b51bbad0055fd;hb=f43129215d9cc2dcd6bf5f2a4c6b1bebec34de16;hp=2f45322ef2cd18c9d162b4814f49ad272bcc236c;hpb=6d33434099abfed5a9946f1ede31be2f42e9a17b;p=vlp.git diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 2f45322..6e9beec 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -29,7 +29,6 @@ ************************************************************/ - #include #include #include @@ -50,7 +49,8 @@ #include #include #include -#include +#include +#include #include #include @@ -62,20 +62,25 @@ #include #include +#include #include "kernel.h" -#include "kernelwindow.h" +#include "ui/kernelwindow.h" #include "lock.h" #include "options.h" +#include "kill_interpreter_dialog.h" +#include "connect_dialog.h" + +#include "vlp/config.h" /* File resides in top directory (where are Makefiles)*/ #include "../../config.h" -char CharLine[25] = "________________________"; -char myargs[5][255]; +namespace loglan { +namespace vlp { -//QApplication *app; +char CharLine[25] = "________________________"; /** * Event invoked on program close. @@ -115,19 +120,31 @@ void QKernel::setLocked(bool locked) * Kernel program constructor. * Prepares everything to work. */ -QKernel::QKernel() +QKernel::QKernel(int argc, char **argv) { setupUi(this); - QDir q(REMOTE_PATH); - char ss[255]; + QString arg0(argv[0]); + arg0 += "/"; + homeDir = QDir(arg0); + homeDir.cdUp(); + + + int i; + for(i = 0; (i < 5) && (i < argc-1); i++) { + strcpy(myargs[i], ""); + if (i < argc) { + strcpy(myargs[i], argv[i+1]); + } + } + + QDir q(getRemoteDir()); if (!q.exists()) { - sprintf(ss, "mkdir %s", REMOTE_PATH); - system(ss); - } + q.mkpath(getRemoteDir()); + } - info_messages = TRUE; + info_messages = actionInfo_messages->isChecked(); wait_for_info = FALSE; setWindowTitle(PACKAGE_NAME); @@ -138,123 +155,65 @@ QKernel::QKernel() freeINTid = 1; ActiveConnections = 0; strcpy(LockPasswd, ""); - LoadConfig("vlp.cfg"); + + 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"); } -/** - * Displays window with information about not implemented functionality. - */ -void QKernel::n_impl() +QString QKernel::getConfigFilePath() +{ + return homeDir.absoluteFilePath("vlp.cfg"); +} + +const char * QKernel::getHomeDir() +{ + return homeDir.absolutePath().toStdString().c_str(); +} + +const char * QKernel::getRemoteDir() { - QMessageBox::information(this, "Function info", "This function is not " - "implemented yet...", "Ok"); + return homeDir.absoluteFilePath(REMOTE_PATH).toStdString().c_str(); } +const char * QKernel::getNetModuleSocket() +{ + return homeDir.absoluteFilePath(NPATH).toStdString().c_str(); +} + +const char * QKernel::getGraphModuleSocket() +{ + return homeDir.absoluteFilePath(GPATH).toStdString().c_str(); +} + +void QKernel::loadConfig(const QString & fname) +{ + loadConfig(fname.toStdString().c_str()); +} /** * Loads configuration from the given file. * @param fname Filename of the configuration file. */ -void QKernel::LoadConfig(char * fname) +void QKernel::loadConfig(const char * fname) { - config_t cfg; - config_setting_t *setting; - const char *str; - - /* 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); - } + loglan::vlp::Config config; + if(config.load(fname)) { + NodeNumber = config.getNodeNumber(); + ConType = config.getConnectionType(); - 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); } - - setting = config_lookup(&cfg, "homedir"); - if (setting) { - strncpy(HomeDir, config_setting_get_string(setting), 255); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "homedir"); - } - - config_destroy(&cfg); - fclose(file); } /** @@ -263,15 +222,15 @@ void QKernel::LoadConfig(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); + // @TODO: if no interpreter is running will result in killing app RunIntModule((char*)s.toAscii().data(), 0); } } @@ -281,10 +240,30 @@ void QKernel::on_actionExecute_triggered() */ void QKernel::on_actionEditor_triggered() { - char cmd[255]; - sprintf(cmd, "%s/modules/logedit %s %s %s %s %s %s &", HomeDir, HomeDir, - myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]); - system(cmd); + QString program = getHomeDir(); + program += "/modules/logedit"; + + pid_t pid = fork(); + if (pid == 0) { + if (execl(program.toStdString().c_str(), + getHomeDir(), + myargs[0], + myargs[1], + myargs[2], + myargs[3], + myargs[4], + NULL + ) == -1) { + WriteMessage("Executing logedit failed!"); + } + } + else if (pid < 0) { + WriteMessage("fork(logedit) failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + exit(3); + } } /** @@ -292,10 +271,34 @@ void QKernel::on_actionEditor_triggered() */ void QKernel::on_actionHelp_triggered() { - char cmd[255]; - sprintf(cmd, "%s/modules/loghelp %s/doc %s %s %s %s %s &", HomeDir, - HomeDir, myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]); - system(cmd); + QString program = getHomeDir(); + program += "/modules/loghelp"; + + QString docDir = getHomeDir(); + docDir += "/doc"; + + pid_t pid = fork(); + if (pid == 0) { + if (execl(program.toStdString().c_str(), + docDir.toStdString().c_str(), + myargs[0], + myargs[1], + myargs[2], + myargs[3], + myargs[4], + NULL + ) == -1) { + + WriteMessage("Executing loghelp failed!"); + } + } + else if (pid < 0) { + WriteMessage("fork(loghelp) failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + exit(3); + } } /** @@ -303,13 +306,35 @@ void QKernel::on_actionHelp_triggered() */ void QKernel::RunGraphModule(char *sk) { - char cmd[255]; - - sprintf(cmd, "%s/modules/loggraph %s %s %s %s %s %s &", HomeDir, - sk, myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]); - - if (system(cmd) != 0) - WriteMessage("Cannot connect GRAPH resources"); + QString program = getHomeDir(); + program += "/modules/loggraph"; + + pid_t pid = fork(); + if (pid == 0) { + if (execl(program.toStdString().c_str(), + program.toStdString().c_str(), + sk, + myargs[0], + myargs[1], + myargs[2], + myargs[3], + myargs[4], + NULL + ) == -1) { + + WriteMessage("Executing loggraph failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + } + } + else if (pid < 0) { + WriteMessage("fork(loggraph) failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + exit(3); + } } /** @@ -318,39 +343,58 @@ void QKernel::RunGraphModule(char *sk) void QKernel::RunNetModule() { struct sockaddr_un svr; - int len, on; + int len; + int on; int sock; - char cmd[255]; - sprintf(cmd, "%s/modules/lognet %s %s %s %s %s %s &", HomeDir, - NPATH, myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]); + + QString program = getHomeDir(); + program += "/modules/lognet"; + + pid_t pid = fork(); + if (pid == 0) { + if (execl(program.toStdString().c_str(), + program.toStdString().c_str(), + getNetModuleSocket(), + getConfigFilePath().toStdString().c_str(), + myargs[0], + myargs[1], + myargs[2], + myargs[3], + myargs[4], + NULL + ) == -1) { + + WriteMessage("Executing lognet failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + } + } + else if (pid < 0) { + WriteMessage("fork(lognet) failed!"); + WriteMessage("Exiting..."); + sleep(2); + on_actionQuit_triggered(); + exit(3); + } /* -------- socket for NET module -------- */ - unlink(NPATH); + unlink(getNetModuleSocket()); sock = socket(AF_UNIX, SOCK_STREAM, 0); bzero(&svr, sizeof(svr)); svr.sun_family = AF_UNIX; - strcpy(svr.sun_path, NPATH); + strcpy(svr.sun_path, getNetModuleSocket()); len = strlen(svr.sun_path) + sizeof(svr.sun_family); - bind(sock, (struct sockaddr*)&svr, len); + bind(sock, (struct sockaddr*)&svr, len); listen(sock, 5); - if (system(cmd) == 0) { - net_sock = accept(sock, (struct sockaddr*)0, (unsigned int*)0); - // close(sock); - if (net_sock != 0) { - WriteMessage("NETWORK successfully connected"); - fcntl(net_sock, F_SETFL, O_NONBLOCK|fcntl(net_sock, - F_GETFL, 0)); - on=1; - setsockopt(net_sock, IPPROTO_TCP, TCP_NODELAY, - (char*)&on, sizeof(on)); - } else { - WriteMessage("Cannot connect NETWORK resources"); - WriteMessage("Exiting..."); - sleep(2); - on_actionQuit_triggered(); - } - /* system OK */ + net_sock = accept(sock, (struct sockaddr*)0, (unsigned int*)0); + // close(sock); + if (net_sock != 0) { + WriteMessage("NETWORK successfully connected"); + fcntl(net_sock, F_SETFL, O_NONBLOCK|fcntl(net_sock, F_GETFL, 0)); + on = 1; + setsockopt(net_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)); } else { WriteMessage("Cannot connect NETWORK resources"); WriteMessage("Exiting..."); @@ -365,30 +409,16 @@ 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); + dialog.setWindowTitle("IP Address:"); + + 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)); } } @@ -497,41 +527,24 @@ 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"); + dialog::KillInterpreterDialog dialog(this); + dialog.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())); - - 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"); @@ -548,11 +561,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); @@ -579,7 +593,7 @@ void QKernel::NetMessage() WriteMessage(CharLine); break; case VLP_REMOTE_INSTANCE: - sprintf(ss, "%s/%s", REMOTE_PATH, + sprintf(ss, "%s/%s", getRemoteDir(), msg.param.pstr); if (info_messages) { @@ -724,12 +738,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(); @@ -757,9 +771,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"); } } @@ -770,10 +783,11 @@ void QKernel::on_actionInfo_messages_triggered() */ void QKernel::on_actionOptions_triggered() { - OptionsDialog optionsDialog(this); + dialog::OptionsDialog optionsDialog(getConfigFilePath(), this); if (optionsDialog.exec()) { - optionsDialog.saveConfig("vlp.cfg"); - LoadConfig("vlp.cfg"); + optionsDialog.saveConfig(getConfigFilePath()); + + loadConfig(getConfigFilePath()); } } @@ -784,12 +798,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()) { @@ -818,29 +832,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); - - 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())); + dialog::LockDialog lockDialog(this); - 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 { @@ -852,14 +848,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 @@ -869,9 +857,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; } } @@ -888,9 +876,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; } } @@ -956,11 +944,15 @@ InterpEntry *QKernel::RunIntModule(char *ss, int r) strcpy(newINT->fullname, ss); sprintf(a, "%s%d", IPATH, newint); - sprintf(cmd, "%s/modules/logint %s %s", HomeDir, a, ss); - if (r) + sprintf(cmd, "%s/modules/logint %s %s", + getHomeDir(), + a, + ss); + if (r) { strcat(cmd, " r"); - sprintf(b, " %s %s %s %s %s", myargs[0], myargs[1], myargs[2], - myargs[3], myargs[4]); + } + sprintf(b, " %s %s %s %s %s", + myargs[0], myargs[1], myargs[2], myargs[3], myargs[4]); strcat(cmd, b); strcat(cmd, " &"); @@ -1016,7 +1008,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; @@ -1071,6 +1062,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"); @@ -1121,6 +1113,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 @@ -1130,18 +1125,11 @@ void QKernel::on_actionInfo_triggered() */ int main(int argc, char **argv) { - int i; - for(i = 0; i < 5; i++) { - strcpy(myargs[i], ""); - } - for(i = 1; i < argc; i++) { - strcpy(myargs[i - 1], argv[i]); - } + XInitThreads(); QApplication * app = new QApplication(argc, argv); - QKernel kernel; + loglan::vlp::QKernel kernel(argc, argv); kernel.show(); - kernel.InitMessage(); return app->exec(); -} +} \ No newline at end of file