Commit e2f2fd02ba00a29bb1c6c9badcdf0cd2ccb656e1
1 parent
509cd479
Exists in
master
and in
5 other branches
Prevenindo erro ao tentar acesso ao bus quando for criado mais de um objeto de acesso
Showing
2 changed files
with
97 additions
and
33 deletions
Show diff stats
src/classlib/remote.cc
... | ... | @@ -36,6 +36,7 @@ |
36 | 36 | #include <malloc.h> |
37 | 37 | #include <sys/types.h> |
38 | 38 | #include <unistd.h> |
39 | + #include <limits.h> | |
39 | 40 | |
40 | 41 | #ifndef DBUS_TIMEOUT_INFINITE |
41 | 42 | #define DBUS_TIMEOUT_INFINITE ((int) 0x7fffffff) |
... | ... | @@ -179,6 +180,7 @@ |
179 | 180 | char * dest; |
180 | 181 | char * path; |
181 | 182 | char * intf; |
183 | + int sequence; | |
182 | 184 | |
183 | 185 | DBusMessage * create_message(const char *method) |
184 | 186 | { |
... | ... | @@ -358,6 +360,45 @@ |
358 | 360 | |
359 | 361 | public: |
360 | 362 | |
363 | +#if defined(HAVE_DBUS) | |
364 | + const char * makeBusName(char *buffer, size_t sz) | |
365 | + { | |
366 | + size_t bytes = strlen(buffer); | |
367 | + char * ptr = buffer; | |
368 | + int val; | |
369 | + | |
370 | + sz -= 2; | |
371 | + | |
372 | + // First uses the object ID | |
373 | + val = this->sequence; | |
374 | + while(bytes < sz && val > 0) | |
375 | + { | |
376 | + *(ptr++) = 'a'+(val % 25); | |
377 | + val /= 25; | |
378 | + bytes++; | |
379 | + } | |
380 | + *(ptr++) = '.'; | |
381 | + | |
382 | + // Then the PID | |
383 | + val = (int) getpid(); | |
384 | + while(bytes < sz && val > 0) | |
385 | + { | |
386 | + *(ptr++) = 'a'+(val % 25); | |
387 | + val /= 25; | |
388 | + bytes++; | |
389 | + } | |
390 | + *(ptr++) = '.'; | |
391 | + | |
392 | + // And last, the project info | |
393 | + strncpy(ptr,intf,sz); | |
394 | + | |
395 | + trace("Busname=\"%s\" sequence=%d this=%p",buffer,sequence,this); | |
396 | + | |
397 | + return buffer; | |
398 | + | |
399 | + } | |
400 | +#endif // HAVE_DBUS | |
401 | + | |
361 | 402 | remote(const char *session) |
362 | 403 | { |
363 | 404 | #if defined(WIN32) |
... | ... | @@ -480,15 +521,16 @@ |
480 | 521 | |
481 | 522 | #elif defined(HAVE_DBUS) |
482 | 523 | |
524 | + static int sq = 0; | |
483 | 525 | DBusError err; |
484 | 526 | int rc; |
485 | 527 | char * str = strdup(session); |
486 | 528 | char * ptr; |
487 | 529 | char busname[4096]; |
488 | - char pidname[10]; | |
489 | - int pid = (int) getpid(); | |
490 | 530 | |
491 | - trace("%s str=%p",__FUNCTION__,str); | |
531 | + this->sequence = (++sq) + time(0); | |
532 | + | |
533 | + trace("%s str=%p sequence=%d",__FUNCTION__,str,sequence); | |
492 | 534 | |
493 | 535 | for(ptr=str;*ptr;ptr++) |
494 | 536 | *ptr = tolower(*ptr); |
... | ... | @@ -569,19 +611,9 @@ |
569 | 611 | return; |
570 | 612 | } |
571 | 613 | |
572 | - memset(pidname,0,10); | |
573 | - for(int f = 0; f < 9 && pid > 0;f++) | |
574 | - { | |
575 | - pidname[f] = 'a'+(pid % 25); | |
576 | - pid /= 25; | |
577 | - } | |
578 | - | |
579 | - snprintf(busname, 4095, "%s.rx3270.br.com.bb",pidname); | |
580 | 614 | |
581 | - trace("Busname: [%s]",busname); | |
582 | - | |
583 | - rc = dbus_bus_request_name(conn, busname, DBUS_NAME_FLAG_REPLACE_EXISTING , &err); | |
584 | - trace("dbus_bus_request_name rc=%d",rc); | |
615 | + rc = dbus_bus_request_name(conn, makeBusName(busname,4095), DBUS_NAME_FLAG_REPLACE_EXISTING , &err); | |
616 | + trace("dbus_bus_request_name(%s) rc=%d",busname,rc); | |
585 | 617 | |
586 | 618 | if (dbus_error_is_set(&err)) |
587 | 619 | { |
... | ... | @@ -632,10 +664,25 @@ |
632 | 664 | |
633 | 665 | } |
634 | 666 | |
667 | + char busname[4096]; | |
668 | + makeBusName(busname,4096); | |
669 | + | |
635 | 670 | free(dest); |
636 | 671 | free(path); |
637 | 672 | free(intf); |
638 | 673 | |
674 | + DBusError err; | |
675 | + | |
676 | + dbus_error_init(&err); | |
677 | + dbus_bus_release_name(conn,busname,&err); | |
678 | + | |
679 | + if (dbus_error_is_set(&err)) | |
680 | + { | |
681 | + exception e = exception("Error when releasing DBUS name (%s)", err.message); | |
682 | + dbus_error_free(&err); | |
683 | + throw e; | |
684 | + } | |
685 | + | |
639 | 686 | #else |
640 | 687 | |
641 | 688 | #endif | ... | ... |
src/classlib/testprogram.cc
... | ... | @@ -28,6 +28,7 @@ |
28 | 28 | */ |
29 | 29 | |
30 | 30 | #include <pw3270/class.h> |
31 | + #include <unistd.h> | |
31 | 32 | #include <iostream> |
32 | 33 | |
33 | 34 | using namespace std; |
... | ... | @@ -37,31 +38,47 @@ |
37 | 38 | |
38 | 39 | int main(int numpar, char *param[]) |
39 | 40 | { |
40 | - string *s; | |
41 | - session *session = session::start("pw3270:a"); | |
42 | -// session *session = session::start("new"); | |
43 | 41 | |
44 | - cout << "pw3270 version: " << session->get_version() << endl; | |
45 | - cout << "pw3270 revision: " << session->get_revision() << endl << endl; | |
42 | + { | |
43 | + string *s; | |
44 | + session *session = session::start("pw3270:a"); | |
45 | + // session *session = session::start("new"); | |
46 | 46 | |
47 | - if(session->is_connected()) | |
48 | - cout << "\tConnected to host" << endl; | |
49 | - else | |
50 | - cout << "\tDisconnected" << endl; | |
47 | + cout << "pw3270 version: " << session->get_version() << endl; | |
48 | + cout << "pw3270 revision: " << session->get_revision() << endl << endl; | |
51 | 49 | |
52 | - cout << "\tSession state: " << session->get_cstate() << endl; | |
50 | + if(session->is_connected()) | |
51 | + cout << "\tConnected to host" << endl; | |
52 | + else | |
53 | + cout << "\tDisconnected" << endl; | |
53 | 54 | |
54 | - s = session->get_display_charset(); | |
55 | - cout << "\tDisplay charset: " << s->c_str() << endl; | |
56 | - delete s; | |
55 | + cout << "\tSession state: " << session->get_cstate() << endl; | |
57 | 56 | |
58 | - s = session->get_host_charset(); | |
59 | - cout << "\tHost charset: " << s->c_str() << endl; | |
60 | - delete s; | |
57 | + s = session->get_display_charset(); | |
58 | + cout << "\tDisplay charset: " << s->c_str() << endl; | |
59 | + delete s; | |
60 | + | |
61 | + s = session->get_host_charset(); | |
62 | + cout << "\tHost charset: " << s->c_str() << endl; | |
63 | + delete s; | |
64 | + | |
65 | + session->connect(false); | |
66 | + delete session; | |
67 | + } | |
68 | + | |
69 | + // Waits | |
70 | + sleep(2); | |
71 | + | |
72 | + // Create another session | |
73 | + { | |
74 | + session *session = session::start("pw3270:a"); | |
75 | + | |
76 | + session->disconnect(); | |
77 | + delete session; | |
78 | + | |
79 | + } | |
61 | 80 | |
62 | - session->connect(false); | |
63 | 81 | |
64 | - delete session; | |
65 | 82 | return 0; |
66 | 83 | } |
67 | 84 | ... | ... |