4 #include <sys/socket.h>
6 #include <netinet/in.h>
11 #include <sys/types.h>
19 #include <QtCore/qlist.h>
20 #include <QtCore/qfile.h>
21 #include <QtCore/qstring.h>
22 #include <QtCore/qstringlist.h>
23 #include <QtCore/QDir>
26 #include <libconfig.h>
28 #define REMOTE_PATH "REMOTE"
32 #define CODEPORT1 3800
34 #define FILE_BUFFER_SIZE 2048
38 /*************** Interpreter slot ********************/
53 /********************* Network slot ********************/
74 for(i = 0; i < 5; i++)
79 code_transmit = FALSE;
82 /*********************** NET Module *****************************/
94 /* List of the Interpeter slots */
95 QList<INTlink*> Interpreters;
96 /* List of the Network slots */
97 QList<NETlink*> Links;
99 NETMOD(int argc, char ** argv);
101 void load_config(const char*);
102 void write_at_console(char*);
103 void send_to_kernel(MESSAGE*);
104 void sock_reopen(NETlink*);
105 void send_connect_info(NETlink*);
106 void send_accept_info(NETlink*);
107 void send_to_node(NETlink*, MESSAGE*);
108 void send_to_int(MESSAGE*);
109 void send_code_ack(NETlink*);
110 void send_to_all(MESSAGE *);
112 NETlink *findNETlink(int node);
113 INTlink *findINTlink(int id);
114 void transmit_file(int ton, char *fname, int fromINT);
115 void propagate_msg(MESSAGE *msg);
116 void check_node(int, int);
119 void exit_sequence();
120 void disconnect_seq();
121 void connect_seq(char*);
122 void accept_connection();
124 void remote_messages();
126 void get_message(NETlink*);
133 NETMOD::NETMOD(int argc, char ** argv)
135 QString kernelSocket(argv[1]);
136 QString configFile(argv[2]);
141 struct sockaddr_in svr;
142 struct sockaddr_un svr1;
147 Interpreters.clear();
149 bzero(&svr, sizeof(svr));
150 listen_sock = socket(AF_INET, SOCK_STREAM, 0);
151 svr.sin_family = AF_INET;
152 svr.sin_addr.s_addr = INADDR_ANY;
153 svr.sin_port = htons(LOGPORT);
154 bind(listen_sock, (struct sockaddr*)&svr, sizeof(svr));
155 listen(listen_sock,5);
156 fcntl(listen_sock, F_SETFL, O_NONBLOCK | fcntl(listen_sock, F_GETFL, 0));
159 all_connected = FALSE;
161 load_config(configFile.toStdString().c_str());
163 kernel_sock = socket(AF_UNIX, SOCK_STREAM, 0);
164 bzero(&svr1, sizeof(svr1));
165 svr1.sun_family = AF_UNIX;
166 strcpy(svr1.sun_path, kernelSocket.toStdString().c_str());
167 strcpy(kername, kernelSocket.toStdString().c_str());
168 len = strlen(svr1.sun_path) + sizeof(svr1.sun_family);
169 i = connect(kernel_sock, (struct sockaddr*)&svr1, len);
171 fcntl(kernel_sock, F_SETFL, O_NONBLOCK|fcntl(kernel_sock, F_GETFL, 0));
173 setsockopt(kernel_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
174 m.msg_type = MSG_NET;
175 m.param.pword[0] = NET_NODE;
176 m.param.pword[1] = MyNode;
179 /* if (regme) regme_sequence();*/
181 if (to_connect > 0) {
182 write_at_console("Connecting remote VLPs...");
183 while (!all_connected) {
187 sprintf(s, "Local node number %d", MyNode);
191 /*##################### Load configuration ##########################*/
193 void NETMOD::load_config(const char *fname)
196 config_setting_t *setting;
203 /* Hack for checking if file exists without using external libs.*/
204 FILE * file = fopen(fname, "rt");
206 fprintf(stderr, "[Error]/Lognet: Cannot load configuration file %s!\n", fname);
207 write_at_console("Cannot load configuration file!");
212 /* Read the file. If there is an error, report it and exit. */
213 if (!config_read(&cfg, file)) {
214 fprintf(stderr, "%s: In file %s, line %d\n",
215 config_error_text(&cfg),
216 config_error_file(&cfg),
217 config_error_line(&cfg));
218 config_destroy(&cfg);
220 /* from original code. */
224 setting = config_lookup(&cfg, "node_number");
226 MyNode = config_setting_get_int(setting);
229 if (!setting || MyNode == -1) {
230 fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
231 "Error", fname, "node_number");
232 write_at_console("Node number must be specified");
233 config_destroy(&cfg);
238 setting = config_lookup(&cfg, "host");
241 pomlink = new NETlink;
243 switch(config_setting_type(setting)) {
244 /* TODO: Deprecated. Made for back compatibility. */
245 case CONFIG_TYPE_STRING:
246 strncpy(pomlink->addr,
247 config_setting_get_string(setting), 255);
249 case CONFIG_TYPE_ARRAY:
250 strncpy(pomlink->addr,
251 config_setting_get_string_elem(setting, 0), 255);
254 fprintf(stderr, "%s! In file %s, bad entry type for %s."
255 " Will not be read.\n",
256 "Fatal error", fname, "host");
257 config_destroy(&cfg);
261 pomlink->connected = FALSE;
262 pomlink->sock = socket(AF_INET, SOCK_STREAM, 0);
263 fcntl(pomlink->sock, F_SETFL,O_NONBLOCK |
264 fcntl(pomlink->sock, F_GETFL, 0));
266 setsockopt(pomlink->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on,
268 Links.append(pomlink);
271 fprintf(stderr, "%s! In file %s, '%s' was not found.\n",
272 "Warning", fname, "host");
275 config_destroy(&cfg);
279 all_connected = TRUE;
283 void NETMOD::write_at_console(char *s)
287 msg.msg_type = MSG_NET;
288 msg.param.pword[0] = NET_CSWRITELN;
289 strcpy(msg.param.pstr, s);
290 send_to_kernel(&msg);
294 * Sends message to kernel
295 * @param msg Message to send.
297 void NETMOD::send_to_kernel(MESSAGE *msg)
299 write(kernel_sock, msg, sizeof(MESSAGE));
303 * Sends message to given node
304 * @param lnk Interpreter link.
305 * @param msg Message to send.
307 void NETMOD::send_to_node(NETlink *lnk, MESSAGE *msg)
309 msg->msg_type = MSG_NET;
310 /* msg2netmsg(msg);*/
312 write(lnk->sock, msg, sizeof(MESSAGE));
317 * Sends message to interpreter
318 * @param msg Message to send.
320 void NETMOD::send_to_int(MESSAGE *msg)
324 pomlink = findINTlink(msg->param.pword[5]);
325 if (pomlink != NULL) {
326 write(pomlink->sock, msg, sizeof(MESSAGE));
330 void NETMOD::accept_connection()
335 struct sockaddr_in svr;
338 struct timeval tout = {0, 0};
343 FD_SET(listen_sock, &rset);
345 if (select(listen_sock + 1, &rset, &wset, 0, (struct timeval *)&tout) >
347 if (FD_ISSET(listen_sock,&rset)) {
348 /* accept connection on listen socket */
350 bzero(&svr, sizeof(svr));
351 nsock = accept(listen_sock, (struct sockaddr*)&svr,
356 /* i<0 someone wants to connect us */
357 pomlink = new NETlink;
358 strcpy(pomlink->addr, inet_ntoa(svr.sin_addr));
359 pomlink->sock = nsock;
360 pomlink->connected = TRUE;
361 fcntl(pomlink->sock, F_SETFL, O_NONBLOCK |
362 fcntl(pomlink->sock, F_GETFL,0));
364 setsockopt(pomlink->sock, IPPROTO_TCP,
365 TCP_NODELAY, (char*)&on, sizeof(on));
366 Links.append(pomlink);
373 void NETMOD::check_node(int n, int sc)
378 m.msg_type = MSG_NET;
379 m.param.pword[0] = NET_NODE_EXIST;
380 m.param.pword[1] = 0;
382 QListIterator<NETlink*> linksIterator(Links);
384 while (linksIterator.hasNext()) {
385 pomlink = linksIterator.next();
386 if (pomlink->node_number == n) {
387 m.param.pword[1] = 1;
388 strcpy(m.param.pstr, pomlink->addr);
392 write(sc, &m, sizeof(MESSAGE));
397 * Internal message from kernel or INT
399 void NETMOD::get_internal()
408 struct timeval tout= {0, 0};
410 struct sockaddr_un svr;
414 FD_SET(kernel_sock, &readset);
417 QListIterator<INTlink*> interpretersIterator(Interpreters);
418 while (interpretersIterator.hasNext()) {
419 pomlink = interpretersIterator.next();
420 FD_SET(pomlink->sock, &readset);
421 if (nrset < pomlink->sock) {
422 nrset = pomlink->sock;
426 if (select(nrset + 1, &readset, &writeset, 0, (struct timeval *)&tout) >
429 /* Check request sockets */
430 interpretersIterator.toFront();
431 while (interpretersIterator.hasNext()) {
432 pomlink = interpretersIterator.next();
433 if (FD_ISSET(pomlink->sock, &readset)) {
434 nr = read(pomlink->sock, &msg, sizeof(MESSAGE));
436 if (msg.msg_type == MSG_NET) {
437 switch(msg.param.pword[0]) {
442 check_node(msg.param.pword[1], pomlink->sock);
445 conn_info(pomlink->sock);
448 msg.param.pword[0] = NET_NODES_NUM_RESPONSE;
449 msg.param.pword[1] = Links.count();
450 write(pomlink->sock, &msg, sizeof(MESSAGE));
458 /* Check internal socket */
459 if (FD_ISSET(kernel_sock,&readset)) {
460 nr = read(kernel_sock, &msg, sizeof(MESSAGE));
462 if (msg.msg_type == MSG_NET) {
463 switch(msg.param.pword[0]) {
464 case NET_TRANSMIT_CODE:
465 transmit_file(msg.param.pword[2],
474 conn_info(kernel_sock);
483 check_node(msg.param.pword[1], kernel_sock);
486 connect_seq(msg.param.pstr);
490 if (msg.msg_type == MSG_VLP) {
491 switch(msg.param.pword[0]) {
493 pomlink = new INTlink;
494 pomlink->sock = socket(AF_UNIX, SOCK_STREAM, 0);
495 bzero(&svr,sizeof(svr));
496 svr.sun_family = AF_UNIX;
497 strcpy(svr.sun_path, msg.param.pstr);
498 si = strlen(svr.sun_path) + sizeof(svr.sun_family);
499 sj = connect(pomlink->sock, (struct sockaddr*)&svr, si);
501 fcntl(pomlink->sock, F_SETFL, O_NONBLOCK | fcntl(pomlink->sock, F_GETFL, 0));
503 setsockopt(pomlink->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
504 pomlink->ID = msg.param.pword[1];
505 pomlink->connected=TRUE;
506 Interpreters.append(pomlink);
509 case VLP_INTERPRETER_DOWN: {
510 pomlink = findINTlink(msg.param.pword[1]);
511 if (pomlink != NULL) {
512 close(pomlink->sock);
513 Interpreters.removeOne(pomlink);
525 void NETMOD::get_message(NETlink *lnk)
535 struct sockaddr_in svr;
536 unsigned char buffer[FILE_BUFFER_SIZE];
539 if (lnk->connected) {
540 nr = read(lnk->sock, &msg, sizeof(MESSAGE));
542 /* netmsg2msg(&msg); */
543 if (msg.msg_type == MSG_NET) {
544 switch(msg.param.pword[0]) {
546 lnk->code_transmit = TRUE;
547 sprintf(pomstr, "%s/%s", REMOTE_PATH, msg.param.pstr);
548 strcat(pomstr,".ccd");
549 lnk->CodeFile = fopen(pomstr, "wb");
550 if (lnk->CodeFile == NULL) {
551 write_at_console("Cannot open file\n");
552 lnk->code_transmit = FALSE;
554 lnk->CodeSize = msg.param.pword[1];
555 psock = socket(AF_INET, SOCK_STREAM, 0);
556 bzero(&svr, sizeof(svr));
557 svr.sin_family = AF_INET;
558 svr.sin_port = htons(CODEPORT);
559 svr.sin_addr.s_addr = inet_addr(lnk->addr);
560 j = connect(psock, (struct sockaddr*)&svr, sizeof(svr));
562 /*fcntl(psock, F_SETFL,O_NONBLOCK | fcntl(psock, F_GETFL,0));*/
564 while (sz < lnk->CodeSize) {
565 rd = read(psock, &buffer, sizeof(buffer));
566 rdbt = fwrite(&buffer, sizeof(unsigned char), rd, lnk->CodeFile);
570 fclose(lnk->CodeFile);
574 lnk->code_transmit = TRUE;
575 sprintf(pomstr, "%s/%s", REMOTE_PATH, msg.param.pstr);
576 strcat(pomstr, ".pcd");
577 lnk->CodeFile = fopen(pomstr, "wb");
578 if (lnk->CodeFile == NULL) {
579 write_at_console("Cannot open file\n");
580 lnk->code_transmit = FALSE;
582 lnk->CodeSize = msg.param.pword[1];
583 psock = socket(AF_INET, SOCK_STREAM, 0);
584 bzero(&svr, sizeof(svr));
585 svr.sin_family = AF_INET;
586 svr.sin_port = htons(CODEPORT1);
587 svr.sin_addr.s_addr = inet_addr(lnk->addr);
588 j = connect(psock, (struct sockaddr*)&svr, sizeof(svr));
590 /*fcntl(psock, F_SETFL,O_NONBLOCK | fcntl(psock, F_GETFL,0));*/
592 while (sz < lnk->CodeSize) {
593 rd = read(psock, &proto, sizeof(proto));
594 rdbt = fwrite(&proto, sizeof(unsigned char), rd, lnk->CodeFile);
598 fclose(lnk->CodeFile);
602 sprintf(pomstr, "Node: %d Addr: %s",
603 msg.param.pword[1], lnk->addr);
604 lnk->node_number = msg.param.pword[1];
605 write_at_console(pomstr);
606 send_accept_info(lnk);
609 sprintf(pomstr, "Node: %d Addr: %s",
610 msg.param.pword[1], lnk->addr);
611 lnk->node_number = msg.param.pword[1];
612 write_at_console(pomstr);
615 sprintf(pomstr,"Node: %d disconnected",
617 write_at_console(pomstr);
619 Links.removeOne(lnk);
623 if (msg.param.pword[1] == MSG_VLP) {
624 send_to_kernel(&msg);
625 } else if (msg.param.pword[1] == MSG_INT) {
638 void NETMOD::remote_messages()
642 struct timeval tout={0, 0};
649 QListIterator<NETlink*> linksIterator(Links);
650 while (linksIterator.hasNext()) {
651 pomlink = linksIterator.next();
652 if (pomlink->connected) {
653 FD_SET(pomlink->sock, &rset);
654 if (max < pomlink->sock){
660 if (select(max + 1, &rset, &wset, 0, (struct timeval *)&tout) > 0) {
661 linksIterator.toFront();
662 while (linksIterator.hasNext()) {
663 pomlink = linksIterator.next();
664 if (FD_ISSET(pomlink->sock,&rset)) {
665 get_message(pomlink);
671 /******************************2010********************************************/
672 void NETMOD::doitall() {
674 int nsock,max = 0, on, nr, si, sj;
675 struct sockaddr_in svr;
679 struct sockaddr_un svr2;
683 FD_SET(listen_sock, &rset);
685 FD_SET(kernel_sock, &rset);
686 if (max < kernel_sock) {
690 QListIterator<INTlink*> interpretersIterator(Interpreters);
691 while (interpretersIterator.hasNext()) {
692 pomlink2 = interpretersIterator.next();
693 FD_SET(pomlink2->sock, &rset);
694 if (max < pomlink2->sock) {
695 max = pomlink2->sock;
699 QListIterator<NETlink*> linksIterator(Links);
700 while (linksIterator.hasNext()) {
701 pomlink = linksIterator.next();
702 if (pomlink->connected) {
703 FD_SET(pomlink->sock, &rset);
704 if (max < pomlink->sock) {
711 if (select(max + 1, &rset, 0, 0, NULL) > 0) {
712 /* accept connection */
713 if (FD_ISSET(listen_sock,&rset)) {
714 /* accept connection on listen socket */
716 bzero(&svr, sizeof(svr));
717 nsock = accept(listen_sock, (struct sockaddr*)&svr, &sz);
719 pomlink = new NETlink;
720 strcpy(pomlink->addr, inet_ntoa(svr.sin_addr));
721 pomlink->sock = nsock;
722 pomlink->connected = TRUE;
723 fcntl(pomlink->sock, F_SETFL,O_NONBLOCK |
724 fcntl(pomlink->sock, F_GETFL, 0));
726 setsockopt(pomlink->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
727 Links.append(pomlink);
731 /* get internal message*/
732 /* Check request sockets */
733 interpretersIterator.toFront();
734 while (interpretersIterator.hasNext()) {
735 pomlink2 = interpretersIterator.next();
736 if (FD_ISSET(pomlink2->sock,&rset)) {
737 nr = read(pomlink2->sock, &msg, sizeof(MESSAGE));
739 if (msg.msg_type == MSG_NET) {
740 switch(msg.param.pword[0]) {
745 check_node(msg.param.pword[1], pomlink2->sock);
748 conn_info(pomlink2->sock);
751 msg.param.pword[0] = NET_NODES_NUM_RESPONSE;
752 msg.param.pword[1] = Links.count();
753 write(pomlink2->sock, &msg, sizeof(MESSAGE));
761 /* Check internal socket */
762 if (FD_ISSET(kernel_sock, &rset)) {
763 nr = read(kernel_sock, &msg, sizeof(MESSAGE));
765 if (msg.msg_type == MSG_NET) {
766 switch(msg.param.pword[0]) {
767 case NET_TRANSMIT_CODE:
768 transmit_file(msg.param.pword[2], msg.param.pstr, msg.param.pword[1]);
775 conn_info(kernel_sock);
784 check_node(msg.param.pword[1], kernel_sock);
787 connect_seq(msg.param.pstr);
790 if (msg.msg_type == MSG_VLP) {
791 switch(msg.param.pword[0]) {
793 pomlink2 = new INTlink;
794 pomlink2->sock = socket(AF_UNIX, SOCK_STREAM, 0);
795 bzero(&svr2, sizeof(svr2));
796 svr2.sun_family = AF_UNIX;
797 strcpy(svr2.sun_path, msg.param.pstr);
798 si = strlen(svr2.sun_path) + sizeof(svr2.sun_family);
799 sj = connect(pomlink2->sock,(struct sockaddr*)&svr2, si);
801 fcntl(pomlink2->sock,F_SETFL, O_NONBLOCK | fcntl(pomlink2->sock, F_GETFL, 0));
803 setsockopt(pomlink2->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
804 pomlink2->ID = msg.param.pword[1];
805 pomlink2->connected = TRUE;
806 Interpreters.append(pomlink2);
809 case VLP_INTERPRETER_DOWN: {
810 pomlink2 = findINTlink(msg.param.pword[1]);
811 if (pomlink2 != NULL) {
812 close(pomlink2->sock);
813 Interpreters.removeOne(pomlink2);
822 /* get remote message*/
824 linksIterator.toFront();
825 while (linksIterator.hasNext()) {
826 pomlink = linksIterator.next();
827 if (FD_ISSET(pomlink->sock, &rset))
828 get_message(pomlink);
832 /****************************** END 2010 **************************************/
836 void NETMOD::propagate_msg(MESSAGE *msg)
841 pomlink = findNETlink(msg->param.pword[4]);
842 if ((pomlink != NULL) && (pomlink->connected))
843 send_to_node(pomlink, msg);
845 if (msg->param.pword[1] == MSG_INT) {
848 sprintf(ss, "Not connected to Node %d", msg->param.pword[4]);
849 write_at_console(ss);
854 void NETMOD::connect_seq(char *a)
857 struct sockaddr_in svr;
860 QListIterator<NETlink*> linksIterator(Links);
861 while (linksIterator.hasNext()) {
862 pom = linksIterator.next();
863 if (strcmp(pom->addr, a) == 0)
867 strcpy(pom->addr, a);
868 pom->connected = FALSE;
869 pom->sock = socket(AF_INET, SOCK_STREAM, 0);
870 bzero(&svr, sizeof(svr));
871 svr.sin_family = AF_INET;
872 svr.sin_port = htons(LOGPORT);
873 svr.sin_addr.s_addr = inet_addr(pom->addr);
874 j = connect(pom->sock, (struct sockaddr*)&svr, sizeof(svr));
876 pom->connected = TRUE;
877 fcntl(pom->sock, F_SETFL, O_NONBLOCK | fcntl(pom->sock, F_GETFL,
880 setsockopt(pom->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&on,
882 send_connect_info(pom);
885 write_at_console("Connection failed");
889 void NETMOD::check_links()
892 struct sockaddr_in svr;
894 QListIterator<NETlink*> linksIterator(Links);
896 /* connect to all other nodes */
897 if (!all_connected) {
898 while (linksIterator.hasNext()) {
899 pomlink = linksIterator.next();
900 if (!(pomlink->connected)) {
901 bzero(&svr, sizeof(svr));
902 svr.sin_family = AF_INET;
903 svr.sin_port = htons(LOGPORT);
904 svr.sin_addr.s_addr = inet_addr(pomlink->addr);
906 j = connect(pomlink->sock,
907 (struct sockaddr*)&svr, sizeof(svr));
909 pomlink->connected = TRUE;
910 fcntl(pomlink->sock, F_SETFL, O_NONBLOCK
911 | fcntl(pomlink->sock, F_GETFL,
913 send_connect_info(pomlink);
915 if (errno == ECONNREFUSED) {
916 sock_reopen(pomlink);
918 } /* not connected */
921 all_connected = TRUE;
922 linksIterator.toFront();
923 while(linksIterator.hasNext()) {
924 pomlink = linksIterator.next();
925 if (pomlink->connected == FALSE) {
926 all_connected = FALSE;
932 void NETMOD::sock_reopen(NETlink *lnk)
937 lnk->sock = socket(AF_INET, SOCK_STREAM, 0);
938 fcntl(lnk->sock, F_SETFL,O_NONBLOCK | fcntl(lnk->sock, F_GETFL, 0));
939 setsockopt(lnk->sock, IPPROTO_TCP,TCP_NODELAY, (char*)&on, sizeof(on));
942 /***************** Acknowledges **************************/
944 void NETMOD::send_connect_info(NETlink *lnk)
948 m.param.pword[0] = NET_CONNECT;
949 m.param.pword[1] = MyNode;
950 m.msg_type = MSG_NET;
953 write(lnk->sock, &m, sizeof(MESSAGE));
956 void NETMOD::send_accept_info(NETlink *lnk)
960 m.param.pword[0] = NET_ACCEPT;
961 m.param.pword[1] = MyNode;
962 m.msg_type = MSG_NET;
965 write(lnk->sock, &m, sizeof(MESSAGE));
968 void NETMOD::send_code_ack(NETlink *lnk)
972 m.param.pword[0] = NET_CODESTREAM_OK;
973 m.msg_type = MSG_NET;
976 write(lnk->sock, &m, sizeof(MESSAGE));
979 void NETMOD::send_to_all(MESSAGE *msg)
983 QListIterator<NETlink*> linksIterator(Links);
984 while (linksIterator.hasNext()) {
985 pomlink = linksIterator.next();
986 write(pomlink->sock, msg, sizeof(MESSAGE));
1003 void NETMOD::exit_sequence()
1007 ::close(kernel_sock);
1008 ::close(listen_sock);
1011 QListIterator<NETlink*> linksIterator(Links);
1012 while (linksIterator.hasNext()) {
1013 pomlink = linksIterator.next();
1014 ::close(pomlink->sock);
1019 void NETMOD::disconnect_seq()
1024 bzero(&m, sizeof(MESSAGE));
1025 m.msg_type = MSG_NET;
1026 m.param.pword[0] = NET_DISCONNECT;
1027 m.param.pword[1] = MyNode;
1029 QListIterator<NETlink*> linksIterator(Links);
1030 while(linksIterator.hasNext()) {
1031 p = linksIterator.next();
1032 send_to_node(p, &m);
1035 linksIterator.toFront();
1036 while(linksIterator.hasNext()) {
1037 p = linksIterator.next();
1043 NETlink * NETMOD::findNETlink(int node)
1045 NETlink *pomlink = NULL;
1046 QListIterator<NETlink*> linksIterator(Links);
1048 while(linksIterator.hasNext()) {
1049 pomlink = linksIterator.next();
1051 if (pomlink->node_number == node)
1057 INTlink * NETMOD::findINTlink(int id)
1059 INTlink *pomlink = NULL;
1060 QListIterator<INTlink*> interpretersIterator(Interpreters);
1061 while(interpretersIterator.hasNext()) {
1062 pomlink = interpretersIterator.next();
1063 if (pomlink->ID == id)
1071 * Sending code to a remote node
1073 void NETMOD::transmit_file(int ton, char *fname, int fromINT)
1079 unsigned char buffer[FILE_BUFFER_SIZE];
1084 struct sockaddr_in svr;
1088 /***************** CCD FILE */
1091 f = fopen(fn, "rb");
1093 fseek(f, 0, SEEK_END);
1094 msg.param.pword[1] = ftell(f);
1096 f = fopen(fn, "rb");
1098 strcpy(b, rindex(fname, '/'));
1099 for(i = 0; i < strlen(b); i++)
1102 msg.param.pword[0] = NET_CCD_START;
1103 strcpy(msg.param.pstr, b);
1105 outlink = findNETlink(ton);
1106 if (outlink == NULL)
1108 bzero(&svr, sizeof(svr));
1109 sock = socket(AF_INET, SOCK_STREAM, 0);
1110 svr.sin_family = AF_INET;
1111 svr.sin_addr.s_addr = INADDR_ANY;
1112 svr.sin_port = htons(CODEPORT);
1113 bind(sock, (struct sockaddr*)&svr, sizeof(svr));
1115 send_to_node(outlink, &msg);
1119 FD_SET(sock, &rset);
1120 if (select(sock + 1, &rset, &wset, 0, 0))
1121 if (FD_ISSET(sock,&rset))
1122 tsock = accept(sock, (struct sockaddr*)&svr,
1127 i = fread(&buffer, 1, sizeof(buffer), f);
1128 write(tsock, &buffer, i);
1131 FD_SET(tsock, &wset);
1132 select(tsock + 1, &rset, &wset, 0, 0);
1139 sprintf(b, "Cannot open file to send %s\n", fname);
1140 write_at_console(b);
1144 /**************** PCD FILE */
1150 fseek(f, 0, SEEK_END);
1151 msg.param.pword[1] = ftell(f);
1153 f = fopen(fn, "rb");
1155 strcpy(b, rindex(fname, '/'));
1156 for(i = 0; i < strlen(b); i++)
1158 msg.param.pword[0] = NET_PCD_START;
1159 strcpy(msg.param.pstr, b);
1161 outlink = findNETlink(ton);
1162 if (outlink == NULL)
1164 bzero(&svr, sizeof(svr));
1165 sock = socket(AF_INET, SOCK_STREAM, 0);
1166 svr.sin_family = AF_INET;
1167 svr.sin_addr.s_addr = INADDR_ANY;
1168 svr.sin_port = htons(CODEPORT1);
1169 bind(sock, (struct sockaddr*)&svr, sizeof(svr));
1171 send_to_node(outlink, &msg);
1175 FD_SET(sock, &rset);
1176 if (select(sock + 1, &rset, &wset, 0, 0))
1177 if (FD_ISSET(sock, &rset))
1178 tsock = accept(sock, (struct sockaddr*)&svr,
1183 i = fread(&proto, 1, sizeof(proto), f);
1184 write(tsock, &proto, i);
1187 FD_SET(tsock, &wset);
1188 select(tsock + 1, &rset, &wset, 0, 0);
1195 sprintf(b, "Cannot open file to send %s\n", fname);
1196 write_at_console(b);
1199 msg.msg_type = MSG_NET;
1200 msg.param.pword[0] = NET_TRANSMITTED;
1201 msg.param.pword[1] = fromINT;
1202 send_to_kernel(&msg);
1205 void NETMOD::conn_info(int sk)
1212 m.msg_type = MSG_NET;
1213 m.param.pword[0] = NET_INFO;
1214 strcpy(m.param.pstr, "");
1216 QListIterator<NETlink*> linksIterator(Links);
1217 while (linksIterator.hasNext()) {
1218 pom = linksIterator.next();
1219 sprintf(poms, "%d=%s;", pom->node_number, pom->addr);
1220 strcat(m.param.pstr, poms);
1223 m.param.pword[1] = 12;
1224 write(sk, &m, sizeof(MESSAGE));
1229 m.param.pword[1] = k;
1230 write(sk, &m, sizeof(MESSAGE));
1232 m.msg_type = MSG_NET;
1233 m.param.pword[0] = NET_INFO_END;
1234 write(sk, &m, sizeof(MESSAGE));
1238 * argv0 = program current path
1239 * argv1 = socket path
1240 * argv2 = config path
1242 int main(int argc, char **argv)
1248 fprintf(stderr, "lognet: Net module path: %s\n", argv[0]);
1249 fprintf(stderr, "lognet: Kernel socket: %s\n", argv[1]);
1250 fprintf(stderr, "lognet: Configuration file: %s\n", argv[2]);
1252 NETMOD netter(argc, argv);