summaryrefslogtreecommitdiff
path: root/x11/ipc/client/ocopclient.cpp
authorzecke <zecke>2002-10-01 18:04:40 (UTC)
committer zecke <zecke>2002-10-01 18:04:40 (UTC)
commitde95e9ad55685630512a8ee67d6e9214af1b1071 (patch) (unidiff)
treea2f62044d8ba48aab1ca3f74376358f5aef352b3 /x11/ipc/client/ocopclient.cpp
parent1d9e6c252f74bfc8fcf6c80d8ce2a80cbd566d26 (diff)
downloadopie-de95e9ad55685630512a8ee67d6e9214af1b1071.zip
opie-de95e9ad55685630512a8ee67d6e9214af1b1071.tar.gz
opie-de95e9ad55685630512a8ee67d6e9214af1b1071.tar.bz2
The client should be working
it's complete but I never compiled it
Diffstat (limited to 'x11/ipc/client/ocopclient.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--x11/ipc/client/ocopclient.cpp129
1 files changed, 129 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
16OCOPClient::OCOPClient( const QString& path, QObject* obj )
17 : QObject( obj )
18{
19 init(QFile::encodeName(path) );
20}
21OCOPClient::~OCOPClient() {
22 close( m_socket );
23}
24void 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 */
51void 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}
73OCOPPacket 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 */
93bool 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};
111void OCOPClient::send( const QCString& chan, const QCString& fu, const QByteArray& arr ) {
112 OCOPPacket pack(OCOPPacket::Call, chan, fu, arr );
113 call( pack );
114}
115void OCOPClient::addChannel(const QCString& channet) {
116 OCOPPacket pack(OCOPPacket::RegisterChannel, channel );
117 call( pack );
118}
119void OCOPClient::delChannel(const QCString& chan ) {
120 OCOPPacket pack(OCOPPacket::UnregisterChannel, channel );
121 call( pack );
122}
123void 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}