-rw-r--r-- | x11/ipc/client/ocopclient.cpp | 25 | ||||
-rw-r--r-- | x11/ipc/client/ocopclient.h | 4 |
2 files changed, 26 insertions, 3 deletions
diff --git a/x11/ipc/client/ocopclient.cpp b/x11/ipc/client/ocopclient.cpp index 43e426c..ac6e4a3 100644 --- a/x11/ipc/client/ocopclient.cpp +++ b/x11/ipc/client/ocopclient.cpp | |||
@@ -1,63 +1,75 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <sys/socket.h> | 5 | #include <sys/socket.h> |
6 | #include <sys/types.h> | ||
6 | #include <sys/un.h> | 7 | #include <sys/un.h> |
7 | 8 | ||
8 | 9 | ||
9 | #include <qfile.h> | 10 | #include <qfile.h> |
10 | #include <qtimer.h> | 11 | #include <qtimer.h> |
11 | 12 | ||
12 | #include "../common/ocoppacket.h" | 13 | #include "../common/ocoppacket.h" |
13 | 14 | ||
14 | #include "ocopclient.h" | 15 | #include "ocopclient.h" |
15 | 16 | ||
16 | OCOPClient::OCOPClient( const QString& path, QObject* obj ) | 17 | OCOPClient::OCOPClient( const QString& path, QObject* obj ) |
17 | : QObject( obj ) | 18 | : QObject( obj ) |
18 | { | 19 | { |
20 | m_tries = 0; | ||
19 | init(QFile::encodeName(path) ); | 21 | init(QFile::encodeName(path) ); |
20 | } | 22 | } |
21 | OCOPClient::~OCOPClient() { | 23 | OCOPClient::~OCOPClient() { |
24 | delete m_notify; | ||
22 | close( m_socket ); | 25 | close( m_socket ); |
23 | } | 26 | } |
27 | void OCOPClient::init() { | ||
28 | // failed start ther server NOW!!! | ||
29 | startUP(); | ||
30 | QCString str; | ||
31 | init(str ); | ||
32 | } | ||
24 | void OCOPClient::init( const QCString& ) { | 33 | void OCOPClient::init( const QCString& ) { |
34 | m_tries++; | ||
25 | struct sockaddr_un unix_adr; | 35 | struct sockaddr_un unix_adr; |
26 | if ( (m_socket = socket(PF_UNIX, SOCK_STREAM, 0) ) < 0 ) { | 36 | if ( (m_socket = socket(PF_UNIX, SOCK_STREAM, 0) ) < 0 ) { |
27 | qWarning("could not socket"); | 37 | qWarning("could not socket"); |
28 | QTimer::singleShot(400, this,SLOT(init() ) ); | 38 | if ( m_tries < 8 ) |
39 | QTimer::singleShot(400, this,SLOT(init() ) ); | ||
29 | return; | 40 | return; |
30 | } | 41 | } |
31 | memset(&unix_adr, 0, sizeof(unix_adr ) ); | 42 | memset(&unix_adr, 0, sizeof(unix_adr ) ); |
32 | unix_adr.sun_family = AF_UNIX; | 43 | unix_adr.sun_family = AF_UNIX; |
33 | sprintf(unix_adr.sun_path,"%s/.opie.cop", getenv("HOME") ); | 44 | sprintf(unix_adr.sun_path,"%s/.opie.cop", getenv("HOME") ); |
34 | int length = sizeof(unix_adr.sun_family) + strlen(unix_adr.sun_path); | 45 | int length = sizeof(unix_adr.sun_family) + strlen(unix_adr.sun_path); |
35 | 46 | ||
36 | if ( ::connect(m_socket, (struct sockaddr*)&unix_adr, length ) < 0 ) { | 47 | if ( ::connect(m_socket, (struct sockaddr*)&unix_adr, length ) < 0 ) { |
37 | qWarning("could not connect %d", errno ); | 48 | qWarning("could not connect %d", errno ); |
38 | close( m_socket ); | 49 | close( m_socket ); |
39 | QTimer::singleShot(400, this, SLOT(init() ) ); | 50 | if ( m_tries < 8 ) |
51 | QTimer::singleShot(400, this, SLOT(init() ) ); | ||
40 | return; | 52 | return; |
41 | } | 53 | } |
42 | m_notify = new QSocketNotifier(m_socket, QSocketNotifier::Read, this ); | 54 | m_notify = new QSocketNotifier(m_socket, QSocketNotifier::Read, this ); |
43 | connect( m_notify, SIGNAL(activated(int) ), | 55 | connect( m_notify, SIGNAL(activated(int) ), |
44 | this, SLOT(newData() ) ); | 56 | this, SLOT(newData() ) ); |
45 | } | 57 | } |
46 | /** | 58 | /** |
47 | * new data | 59 | * new data |
48 | * read the header check magic number | 60 | * read the header check magic number |
49 | * and maybe read body | 61 | * and maybe read body |
50 | */ | 62 | */ |
51 | void OCOPClient::newData() { | 63 | void OCOPClient::newData() { |
52 | OCOPPacket pack = packet(); | 64 | OCOPPacket pack = packet(); |
53 | if ( pack.channel().isEmpty() ) | 65 | if ( pack.channel().isEmpty() ) |
54 | return; | 66 | return; |
55 | 67 | ||
56 | switch( pack.type() ) { | 68 | switch( pack.type() ) { |
57 | case OCOPPacket::Register: | 69 | case OCOPPacket::Register: |
58 | case OCOPPacket::Unregister: | 70 | case OCOPPacket::Unregister: |
59 | case OCOPPacket::Method: | 71 | case OCOPPacket::Method: |
60 | case OCOPPacket::RegisterChannel: | 72 | case OCOPPacket::RegisterChannel: |
61 | case OCOPPacket::UnregisterChannel: | 73 | case OCOPPacket::UnregisterChannel: |
62 | case OCOPPacket::Return: | 74 | case OCOPPacket::Return: |
63 | case OCOPPacket::Signal: | 75 | case OCOPPacket::Signal: |
@@ -106,24 +118,33 @@ bool OCOPClient::isRegistered( const QCString& chan ) const{ | |||
106 | return true; | 118 | return true; |
107 | } | 119 | } |
108 | 120 | ||
109 | return false; | 121 | return false; |
110 | }; | 122 | }; |
111 | void OCOPClient::send( const QCString& chan, const QCString& fu, const QByteArray& arr ) { | 123 | void OCOPClient::send( const QCString& chan, const QCString& fu, const QByteArray& arr ) { |
112 | OCOPPacket pack(OCOPPacket::Call, chan, fu, arr ); | 124 | OCOPPacket pack(OCOPPacket::Call, chan, fu, arr ); |
113 | call( pack ); | 125 | call( pack ); |
114 | } | 126 | } |
115 | void OCOPClient::addChannel(const QCString& channel) { | 127 | void OCOPClient::addChannel(const QCString& channel) { |
116 | OCOPPacket pack(OCOPPacket::RegisterChannel, channel ); | 128 | OCOPPacket pack(OCOPPacket::RegisterChannel, channel ); |
117 | call( pack ); | 129 | call( pack ); |
118 | } | 130 | } |
119 | void OCOPClient::delChannel(const QCString& chan ) { | 131 | void OCOPClient::delChannel(const QCString& chan ) { |
120 | OCOPPacket pack(OCOPPacket::UnregisterChannel, chan ); | 132 | OCOPPacket pack(OCOPPacket::UnregisterChannel, chan ); |
121 | call( pack ); | 133 | call( pack ); |
122 | } | 134 | } |
123 | void OCOPClient::call( const OCOPPacket& pack ) { | 135 | void OCOPClient::call( const OCOPPacket& pack ) { |
124 | OCOPHead head = pack.head(); | 136 | OCOPHead head = pack.head(); |
125 | write(m_socket, &head, sizeof(head) ); | 137 | write(m_socket, &head, sizeof(head) ); |
126 | write(m_socket, pack.channel().data(), pack.channel().size() ); | 138 | write(m_socket, pack.channel().data(), pack.channel().size() ); |
127 | write(m_socket, pack.header().data(), pack.header().size() ); | 139 | write(m_socket, pack.header().data(), pack.header().size() ); |
128 | write(m_socket, pack.content().data(), pack.content().size() ); | 140 | write(m_socket, pack.content().data(), pack.content().size() ); |
129 | } | 141 | } |
142 | void OCOPClient::startUP() { | ||
143 | qWarning("Start me up"); | ||
144 | pid_t pi = fork(); | ||
145 | if ( pi == 0 ) { | ||
146 | setsid(); | ||
147 | execlp("opie-ipc", "opie-ipc", NULL ); | ||
148 | _exit(1); | ||
149 | } | ||
150 | } | ||
diff --git a/x11/ipc/client/ocopclient.h b/x11/ipc/client/ocopclient.h index e9544b9..53018a5 100644 --- a/x11/ipc/client/ocopclient.h +++ b/x11/ipc/client/ocopclient.h | |||
@@ -23,37 +23,39 @@ public: | |||
23 | 23 | ||
24 | /** | 24 | /** |
25 | * Occasionally I decide to start a Server from here | 25 | * Occasionally I decide to start a Server from here |
26 | */ | 26 | */ |
27 | OCOPClient(const QString& pathToServer = QString::null, QObject* obj = 0l); | 27 | OCOPClient(const QString& pathToServer = QString::null, QObject* obj = 0l); |
28 | ~OCOPClient(); | 28 | ~OCOPClient(); |
29 | 29 | ||
30 | bool isRegistered( const QCString& )const; | 30 | bool isRegistered( const QCString& )const; |
31 | void send( const QCString& chan, const QCString&, const QByteArray& msg ); | 31 | void send( const QCString& chan, const QCString&, const QByteArray& msg ); |
32 | 32 | ||
33 | /** | 33 | /** |
34 | * add a channel and does connect to a signal | 34 | * add a channel and does connect to a signal |
35 | * callback is the object | 35 | * callback is the object |
36 | * slot is the SLOT() | 36 | * slot is the SLOT() |
37 | */ | 37 | */ |
38 | void addChannel( const QCString& channel ); | 38 | void addChannel( const QCString& channel ); |
39 | void delChannel( const QCString& channel ); | 39 | void delChannel( const QCString& channel ); |
40 | 40 | ||
41 | /* make it singleton? */ | 41 | /* make it singleton? */ |
42 | //static OCOPClient* self(); | 42 | //static OCOPClient* self(); |
43 | /* no direct signals due the design */ | 43 | /* no direct signals due the design */ |
44 | signals: | 44 | signals: |
45 | void called(const QCString&, const QCString&, const QByteArray& ); | 45 | void called(const QCString&, const QCString&, const QByteArray& ); |
46 | private slots: | 46 | private slots: |
47 | void init(); | ||
47 | void init(const QCString& pa); | 48 | void init(const QCString& pa); |
48 | void newData(); | 49 | void newData(); |
49 | private: | 50 | private: |
51 | void startUP(); | ||
50 | OCOPPacket packet()const; | 52 | OCOPPacket packet()const; |
51 | void call( const OCOPPacket& ); | 53 | void call( const OCOPPacket& ); |
52 | 54 | ||
53 | QSocketNotifier* m_notify; | 55 | QSocketNotifier* m_notify; |
54 | int m_socket; | 56 | int m_socket; |
55 | private slots: | 57 | int m_tries; |
56 | 58 | ||
57 | }; | 59 | }; |
58 | 60 | ||
59 | #endif | 61 | #endif |