X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=src%2Fnet%2Flognet.cpp;h=088ef9fc24f012f8f8e73b8a05a65fa309ccccd5;hb=24268bd1db118a7ee6a291900dd771a01f2f660b;hp=decd822abfb2ddcb652ccafe82a6509035e35f06;hpb=7a277153cc72eed9f152af09f22e680db3ec6563;p=vlp.git diff --git a/src/net/lognet.cpp b/src/net/lognet.cpp index decd822..088ef9f 100644 --- a/src/net/lognet.cpp +++ b/src/net/lognet.cpp @@ -1,5 +1,5 @@ -#include "../head/genint1.h" -#include "../head/comm.h" +#include "genint1.h" +#include "comm.h" #include #include @@ -22,6 +22,8 @@ #include #include +#include + #define REMOTE_PATH "REMOTE" #define MAXLINKS 30 #define LOGPORT 3600 @@ -183,44 +185,93 @@ NETMOD::NETMOD(char *kernel_name) void NETMOD::load_config(char *fname) { - QFile f(fname); - QString line; - QString val; - int br=0,on,k=0; - NETlink *pomlink; + config_t cfg; + config_setting_t *setting; + int on,k=0; + NETlink *pomlink; - if (!f.exists()) - { - write_at_console("Cannot load configuration file!");sleep(2);exit(3); - } - f.open(IO_ReadOnly); - br = f.readLine(line,256); - while (br>0) - { - QStringList l = QStringList::split("=",line,FALSE); - QStringList::Iterator it = l.begin(); - line = *it; - ++it; - val = *it; - val = val.stripWhiteSpace(); - if (line == "node_number") {MyNode = val.toInt();}; - if (line == "host" ) { - k++; - pomlink = new NETlink; - strcpy(pomlink->addr,val.ascii()); - pomlink->connected = FALSE; - pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); - fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0)); - on=1; - setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on)); - Links.append(pomlink); - to_connect++; - }; - br = f.readLine(line,256); - } - f.close(); - if (k==0) all_connected=TRUE; - if (MyNode==-1) {write_at_console("Node number must be specified");exit(1);}; + config_init(&cfg); + + + /* 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); + write_at_console("Cannot load configuration file!"); + fclose(file); + exit(3); + } + + /* 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);/* from original code. */ + } + + setting = config_lookup(&cfg, "node_number"); + if(setting) + { + MyNode = config_setting_get_int(setting); + } + /* else */ + if (!setting || MyNode==-1) + { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", + "Error", + fname, + "node_number"); + write_at_console("Node number must be specified"); + config_destroy(&cfg); + fclose(file); + exit(1); + } + + setting = config_lookup(&cfg, "host"); + if(setting) { + k++; + pomlink = new NETlink; + + switch(config_setting_type(setting)) { + case CONFIG_TYPE_STRING:/* TODO: Deprecated. Made for back compatibility. */ + strncpy(pomlink->addr, config_setting_get_string(setting), 255); + break; + case CONFIG_TYPE_ARRAY: + strncpy(pomlink->addr, config_setting_get_string_elem(setting, 0), 255); + break; + default: + fprintf(stderr, "%s! In file %s, bad entry type for %s. Will not be read.\n" + "Fatal error", + fname, + "host"); + config_destroy(&cfg); + fclose(file); + exit(1); + } + pomlink->connected = FALSE; + pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); + fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0)); + on=1; + setsockopt(pomlink->sock,IPPROTO_TCP,TCP_NODELAY,(char*)&on,sizeof(on)); + Links.append(pomlink); + to_connect++; + } + else { + fprintf(stderr, "%s! In file %s, '%s' was not found.\n", + "Warning", + fname, + "host"); + } + + config_destroy(&cfg); + fclose(file); + + if (k==0) all_connected=TRUE; }