-rw-r--r-- | x11/ipc/client/ocopclient.cpp | 129 | ||||
-rw-r--r-- | x11/ipc/client/ocopclient.h | 56 | ||||
-rw-r--r-- | x11/ipc/server/ocopserver.cpp | 1 |
3 files changed, 186 insertions, 0 deletions
diff --git a/x11/ipc/client/ocopclient.cpp b/x11/ipc/client/ocopclient.cpp new file mode 100644 index 0000000..1c25271 --- a/dev/null +++ b/x11/ipc/client/ocopclient.cpp | |||
@@ -0,0 +1,129 @@ | |||
1 | #include <errno.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <stdio.h> | ||
4 | #include <unistd.h> | ||
5 | #include <sys/socket.h> | ||
6 | #include <sys/un.h> | ||
7 | |||
8 | |||
9 | #include <qfile.h> | ||
10 | #include <qtimer.h> | ||
11 | |||
12 | #include "../common/ocoppacket.h" | ||
13 | |||
14 | #include "ocopclient.h" | ||
15 | |||
16 | OCOPClient::OCOPClient( const QString& path, QObject* obj ) | ||
17 | : QObject( obj ) | ||
18 | { | ||
19 | init(QFile::encodeName(path) ); | ||
20 | } | ||
21 | OCOPClient::~OCOPClient() { | ||
22 | close( m_socket ); | ||
23 | } | ||
24 | void OCOPClient::init( const QCString& str ) { | ||
25 | struct sockaddr_un unix_adr; | ||
26 | if ( (m_socket = socket(PF_UNIX, SOCK_STREAM, 0) ) < 0 ) { | ||
27 | qWarning("could not socket"); | ||
28 | QTimer::singleShot(400, this,SLOT(init() ) ); | ||
29 | return; | ||
30 | } | ||
31 | memset(&unix_adr, 0, sizeof(unix_adr ) ); | ||
32 | unix_adr.sun_family = AF_UNIX; | ||
33 | sprintf(unix_adr.sun_path,"%s/.opie.cop", getenv("HOME") ); | ||
34 | int length = sizeof(unix_adr.sun_family) + strlen(unix_adr.sun_path); | ||
35 | |||
36 | if ( ::connect(m_socket, (struct sockaddr*)&unix_adr, length ) < 0 ) { | ||
37 | qWarning("could not connect %d", errno ); | ||
38 | close( m_socket ); | ||
39 | QTimer::singleShot(400, this, SLOT(init() ) ); | ||
40 | return; | ||
41 | } | ||
42 | m_notify = new QSocketNotifier(m_socket, QSocketNotifier::Read, this ); | ||
43 | connect( m_notify, SIGNAL(activated(int) ), | ||
44 | this, SLOT(newData() ) ); | ||
45 | } | ||
46 | /** | ||
47 | * new data | ||
48 | * read the header check magic number | ||
49 | * and maybe read body | ||
50 | */ | ||
51 | void OCOPClient::newData() { | ||
52 | OCOPPacket pack = packet(); | ||
53 | if ( pack.channel().isEmpty() ) | ||
54 | return; | ||
55 | |||
56 | switch( pack.type() ) { | ||
57 | case OCOPPacket::Register: | ||
58 | case OCOPPacket::Unregister: | ||
59 | case OCOPPacket::Method: | ||
60 | case OCOPPacket::RegisterChannel: | ||
61 | case OCOPPacket::UnregisterChannel: | ||
62 | case OCOPPacket::Return: | ||
63 | case OCOPPacket::Signal: | ||
64 | /* is Registered should be handled sync */ | ||
65 | case OCOPPacket::isRegistered: | ||
66 | break; | ||
67 | /* emit the signal */ | ||
68 | case OCOPPacket::Call: | ||
69 | emit called( pack.channel(), pack.header(), pack.content() ); | ||
70 | break; | ||
71 | } | ||
72 | } | ||
73 | OCOPPacket OCOPClient::packet() { | ||
74 | QCString chan; | ||
75 | QCString func; | ||
76 | QByteArray ar; | ||
77 | OCOPHead head; | ||
78 | memset(&head, 0, sizeof(head) ); | ||
79 | read(m_socket, &head, sizeof(head) ); | ||
80 | if ( head.magic == 47 ) { | ||
81 | read(m_socket, chan.data(), head.chlen ); | ||
82 | read(m_socket, func.data(), head.funclen ); | ||
83 | read(m_socket, ar.data(), head.datalen ); | ||
84 | } | ||
85 | OCOPPacket pack(head.type, chan, func, data ); | ||
86 | return pack; | ||
87 | } | ||
88 | /* | ||
89 | * we've blocking IO here on these sockets | ||
90 | * so we send and go on read | ||
91 | * this will be blocked | ||
92 | */ | ||
93 | bool OCOPClient::isRegistered( const QCString& chan ) { | ||
94 | /* should I disconnect the socket notfier? */ | ||
95 | OCOPPacket packe(OCOPPacket::IsRegistered, chan ); | ||
96 | OCOPHead head = packe.head(); | ||
97 | write(m_socket, &head, sizeof(head) ); | ||
98 | |||
99 | /* block */ | ||
100 | OCOPPacket pack = packet(); | ||
101 | |||
102 | /* connect here again */ | ||
103 | if ( pack.channel() == chan ) { | ||
104 | QCString func = pack.header(); | ||
105 | if (func[0] == 1 ) | ||
106 | return; | ||
107 | } | ||
108 | |||
109 | return false; | ||
110 | }; | ||
111 | void OCOPClient::send( const QCString& chan, const QCString& fu, const QByteArray& arr ) { | ||
112 | OCOPPacket pack(OCOPPacket::Call, chan, fu, arr ); | ||
113 | call( pack ); | ||
114 | } | ||
115 | void OCOPClient::addChannel(const QCString& channet) { | ||
116 | OCOPPacket pack(OCOPPacket::RegisterChannel, channel ); | ||
117 | call( pack ); | ||
118 | } | ||
119 | void OCOPClient::delChannel(const QCString& chan ) { | ||
120 | OCOPPacket pack(OCOPPacket::UnregisterChannel, channel ); | ||
121 | call( pack ); | ||
122 | } | ||
123 | void OCOPPacket::call( const OCOPPacket& pack ) { | ||
124 | OCOPHead head = pack.head(); | ||
125 | write(m_socket, &head, sizeof(head) ); | ||
126 | write(m_socket, pack.channel().data(), pack.channel().size() ); | ||
127 | write(m_socket, pack.header().data(), pack.header().size() ); | ||
128 | write(m_socket, pack.content().data(), pack.content().size() ); | ||
129 | } | ||
diff --git a/x11/ipc/client/ocopclient.h b/x11/ipc/client/ocopclient.h new file mode 100644 index 0000000..5300132 --- a/dev/null +++ b/x11/ipc/client/ocopclient.h | |||
@@ -0,0 +1,56 @@ | |||
1 | #ifndef OPIE_OCOP_CLIENT_H | ||
2 | #define OPIE_OCOP_CLIENT_H | ||
3 | |||
4 | |||
5 | #include <qobject.h> | ||
6 | #include <qcstring.h> | ||
7 | #include <qmap.h> | ||
8 | #include <qsignal.h> | ||
9 | #include <qstring.h> | ||
10 | #include <qsocketnotifier.h> | ||
11 | |||
12 | |||
13 | /** | ||
14 | * This is the OCOP client | ||
15 | * It currently only supports | ||
16 | * asking if a Channel is registered, | ||
17 | * calling and receiving calls | ||
18 | */ | ||
19 | class OCOPPacket; | ||
20 | class OCOPClient : public QObject{ | ||
21 | Q_OBJECT | ||
22 | public: | ||
23 | |||
24 | /** | ||
25 | * Occasionally I decide to start a Server from here | ||
26 | */ | ||
27 | OCOPClient(const QString& pathToServer = QString::null, QObject* obj = 0l); | ||
28 | ~OCOPClient(); | ||
29 | |||
30 | bool isRegistered( const QCString& )const; | ||
31 | void send( const QCString& chan, const QCString&, const QByteArray& msg ); | ||
32 | |||
33 | /** | ||
34 | * add a channel and does connect to a signal | ||
35 | * callback is the object | ||
36 | * slot is the SLOT() | ||
37 | */ | ||
38 | void addChannel( const QCString& channel ); | ||
39 | void delChannel( const QCString& channel ); | ||
40 | /* no direct signals due the design */ | ||
41 | signals: | ||
42 | void called(const QCString&, const QCString&, const QByteArray ); | ||
43 | private slots: | ||
44 | void init(const QCString& pa); | ||
45 | void newData(); | ||
46 | private: | ||
47 | OCOPPacket packet(); | ||
48 | void call( const OCOPPacket& ); | ||
49 | |||
50 | QSocketNotifier* m_notify; | ||
51 | int m_socket; | ||
52 | private slots: | ||
53 | |||
54 | }; | ||
55 | |||
56 | #endif | ||
diff --git a/x11/ipc/server/ocopserver.cpp b/x11/ipc/server/ocopserver.cpp index 3df574b..e76657e 100644 --- a/x11/ipc/server/ocopserver.cpp +++ b/x11/ipc/server/ocopserver.cpp | |||
@@ -376,6 +376,7 @@ void OCopServer::call( const OCOPPacket& p, int fd ) { | |||
376 | OCOPHead head = p.head(); | 376 | OCOPHead head = p.head(); |
377 | for (it = cli.begin(); it != cli.end(); ++it ) { | 377 | for (it = cli.begin(); it != cli.end(); ++it ) { |
378 | write( (*it), &head, sizeof(head ) ); | 378 | write( (*it), &head, sizeof(head ) ); |
379 | /* expl. shared! */ | ||
379 | write( (*it), p.channel().data(), p.channel().size() ); | 380 | write( (*it), p.channel().data(), p.channel().size() ); |
380 | write( (*it), p.header().data(), p.header().size() ); | 381 | write( (*it), p.header().data(), p.header().size() ); |
381 | write( (*it), p.content().data(), p.content().size() ); | 382 | write( (*it), p.content().data(), p.content().size() ); |