-rw-r--r-- | x11/ipc/client/ocopclient.cpp | 4 | ||||
-rw-r--r-- | x11/ipc/server/ocopserver.cpp | 10 | ||||
-rw-r--r-- | x11/ipc/server/ocopserver.pro | 1 |
3 files changed, 9 insertions, 6 deletions
diff --git a/x11/ipc/client/ocopclient.cpp b/x11/ipc/client/ocopclient.cpp index ac6e4a3..c1def73 100644 --- a/x11/ipc/client/ocopclient.cpp +++ b/x11/ipc/client/ocopclient.cpp | |||
@@ -1,150 +1,150 @@ | |||
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/types.h> |
7 | #include <sys/un.h> | 7 | #include <sys/un.h> |
8 | 8 | ||
9 | 9 | ||
10 | #include <qfile.h> | 10 | #include <qfile.h> |
11 | #include <qtimer.h> | 11 | #include <qtimer.h> |
12 | 12 | ||
13 | #include "../common/ocoppacket.h" | 13 | #include "../common/ocoppacket.h" |
14 | 14 | ||
15 | #include "ocopclient.h" | 15 | #include "ocopclient.h" |
16 | 16 | ||
17 | OCOPClient::OCOPClient( const QString& path, QObject* obj ) | 17 | OCOPClient::OCOPClient( const QString& path, QObject* obj ) |
18 | : QObject( obj ) | 18 | : QObject( obj ) |
19 | { | 19 | { |
20 | m_tries = 0; | 20 | m_tries = 0; |
21 | init(QFile::encodeName(path) ); | 21 | init(QFile::encodeName(path) ); |
22 | } | 22 | } |
23 | OCOPClient::~OCOPClient() { | 23 | OCOPClient::~OCOPClient() { |
24 | delete m_notify; | 24 | delete m_notify; |
25 | close( m_socket ); | 25 | close( m_socket ); |
26 | } | 26 | } |
27 | void OCOPClient::init() { | 27 | void OCOPClient::init() { |
28 | // failed start ther server NOW!!! | 28 | // failed start ther server NOW!!! |
29 | startUP(); | 29 | startUP(); |
30 | QCString str; | 30 | QCString str; |
31 | init(str ); | 31 | init(str ); |
32 | } | 32 | } |
33 | void OCOPClient::init( const QCString& ) { | 33 | void OCOPClient::init( const QCString& ) { |
34 | m_tries++; | 34 | m_tries++; |
35 | struct sockaddr_un unix_adr; | 35 | struct sockaddr_un unix_adr; |
36 | if ( (m_socket = socket(PF_UNIX, SOCK_STREAM, 0) ) < 0 ) { | 36 | if ( (m_socket = socket(PF_UNIX, SOCK_STREAM, 0) ) < 0 ) { |
37 | qWarning("could not socket"); | 37 | qWarning("could not socket"); |
38 | if ( m_tries < 8 ) | 38 | if ( m_tries < 8 ) |
39 | QTimer::singleShot(400, this,SLOT(init() ) ); | 39 | QTimer::singleShot(400, this,SLOT(init() ) ); |
40 | return; | 40 | return; |
41 | } | 41 | } |
42 | memset(&unix_adr, 0, sizeof(unix_adr ) ); | 42 | memset(&unix_adr, 0, sizeof(unix_adr ) ); |
43 | unix_adr.sun_family = AF_UNIX; | 43 | unix_adr.sun_family = AF_UNIX; |
44 | sprintf(unix_adr.sun_path,"%s/.opie.cop", getenv("HOME") ); | 44 | sprintf(unix_adr.sun_path,"%s/.opie.cop", getenv("HOME") ); |
45 | 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); |
46 | 46 | ||
47 | if ( ::connect(m_socket, (struct sockaddr*)&unix_adr, length ) < 0 ) { | 47 | if ( ::connect(m_socket, (struct sockaddr*)&unix_adr, length ) < 0 ) { |
48 | qWarning("could not connect %d", errno ); | 48 | qWarning("could not connect %d", errno ); |
49 | close( m_socket ); | 49 | close( m_socket ); |
50 | if ( m_tries < 8 ) | 50 | if ( m_tries < 8 ) |
51 | QTimer::singleShot(400, this, SLOT(init() ) ); | 51 | QTimer::singleShot(400, this, SLOT(init() ) ); |
52 | return; | 52 | return; |
53 | } | 53 | } |
54 | m_notify = new QSocketNotifier(m_socket, QSocketNotifier::Read, this ); | 54 | m_notify = new QSocketNotifier(m_socket, QSocketNotifier::Read, this ); |
55 | connect( m_notify, SIGNAL(activated(int) ), | 55 | connect( m_notify, SIGNAL(activated(int) ), |
56 | this, SLOT(newData() ) ); | 56 | this, SLOT(newData() ) ); |
57 | } | 57 | } |
58 | /** | 58 | /** |
59 | * new data | 59 | * new data |
60 | * read the header check magic number | 60 | * read the header check magic number |
61 | * and maybe read body | 61 | * and maybe read body |
62 | */ | 62 | */ |
63 | void OCOPClient::newData() { | 63 | void OCOPClient::newData() { |
64 | OCOPPacket pack = packet(); | 64 | OCOPPacket pack = packet(); |
65 | if ( pack.channel().isEmpty() ) | 65 | if ( pack.channel().isEmpty() ) |
66 | return; | 66 | return; |
67 | 67 | ||
68 | switch( pack.type() ) { | 68 | switch( pack.type() ) { |
69 | case OCOPPacket::Register: | 69 | case OCOPPacket::Register: |
70 | case OCOPPacket::Unregister: | 70 | case OCOPPacket::Unregister: |
71 | case OCOPPacket::Method: | 71 | case OCOPPacket::Method: |
72 | case OCOPPacket::RegisterChannel: | 72 | case OCOPPacket::RegisterChannel: |
73 | case OCOPPacket::UnregisterChannel: | 73 | case OCOPPacket::UnregisterChannel: |
74 | case OCOPPacket::Return: | 74 | case OCOPPacket::Return: |
75 | case OCOPPacket::Signal: | 75 | case OCOPPacket::Signal: |
76 | /* is Registered should be handled sync */ | 76 | /* is Registered should be handled sync */ |
77 | case OCOPPacket::IsRegistered: | 77 | case OCOPPacket::IsRegistered: |
78 | break; | 78 | break; |
79 | /* emit the signal */ | 79 | /* emit the signal */ |
80 | case OCOPPacket::Call: | 80 | case OCOPPacket::Call: |
81 | emit called( pack.channel(), pack.header(), pack.content() ); | 81 | emit called( pack.channel(), pack.header(), pack.content() ); |
82 | break; | 82 | break; |
83 | } | 83 | } |
84 | } | 84 | } |
85 | OCOPPacket OCOPClient::packet() const{ | 85 | OCOPPacket OCOPClient::packet() const{ |
86 | QCString chan; | 86 | QCString chan; |
87 | QCString func; | 87 | QCString func; |
88 | QByteArray ar; | 88 | QByteArray ar; |
89 | OCOPHead head; | 89 | OCOPHead head; |
90 | memset(&head, 0, sizeof(head) ); | 90 | memset(&head, 0, sizeof(head) ); |
91 | read(m_socket, &head, sizeof(head) ); | 91 | read(m_socket, &head, sizeof(head) ); |
92 | if ( head.magic == 47 ) { | 92 | if ( head.magic == 47 ) { |
93 | read(m_socket, chan.data(), head.chlen ); | 93 | read(m_socket, chan.data(), head.chlen ); |
94 | read(m_socket, func.data(), head.funclen ); | 94 | read(m_socket, func.data(), head.funclen ); |
95 | read(m_socket, ar.data(), head.datalen ); | 95 | read(m_socket, ar.data(), head.datalen ); |
96 | } | 96 | } |
97 | OCOPPacket pack(head.type, chan, func, ar ); | 97 | OCOPPacket pack(head.type, chan, func, ar ); |
98 | return pack; | 98 | return pack; |
99 | } | 99 | } |
100 | /* | 100 | /* |
101 | * we've blocking IO here on these sockets | 101 | * we've blocking IO here on these sockets |
102 | * so we send and go on read | 102 | * so we send and go on read |
103 | * this will be blocked | 103 | * this will be blocked |
104 | */ | 104 | */ |
105 | bool OCOPClient::isRegistered( const QCString& chan ) const{ | 105 | bool OCOPClient::isRegistered( const QCString& chan ) const{ |
106 | /* should I disconnect the socket notfier? */ | 106 | /* should I disconnect the socket notfier? */ |
107 | OCOPPacket packe(OCOPPacket::IsRegistered, chan ); | 107 | OCOPPacket packe(OCOPPacket::IsRegistered, chan ); |
108 | OCOPHead head = packe.head(); | 108 | OCOPHead head = packe.head(); |
109 | write(m_socket, &head, sizeof(head) ); | 109 | write(m_socket, &head, sizeof(head) ); |
110 | 110 | write(m_socket, chan.data(), chan.size() ); | |
111 | /* block */ | 111 | /* block */ |
112 | OCOPPacket pack = packet(); | 112 | OCOPPacket pack = packet(); |
113 | 113 | ||
114 | /* connect here again */ | 114 | /* connect here again */ |
115 | if ( pack.channel() == chan ) { | 115 | if ( pack.channel() == chan ) { |
116 | QCString func = pack.header(); | 116 | QCString func = pack.header(); |
117 | if (func[0] == 1 ) | 117 | if (func[0] == 1 ) |
118 | return true; | 118 | return true; |
119 | } | 119 | } |
120 | 120 | ||
121 | return false; | 121 | return false; |
122 | }; | 122 | }; |
123 | 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 ) { |
124 | OCOPPacket pack(OCOPPacket::Call, chan, fu, arr ); | 124 | OCOPPacket pack(OCOPPacket::Call, chan, fu, arr ); |
125 | call( pack ); | 125 | call( pack ); |
126 | } | 126 | } |
127 | void OCOPClient::addChannel(const QCString& channel) { | 127 | void OCOPClient::addChannel(const QCString& channel) { |
128 | OCOPPacket pack(OCOPPacket::RegisterChannel, channel ); | 128 | OCOPPacket pack(OCOPPacket::RegisterChannel, channel ); |
129 | call( pack ); | 129 | call( pack ); |
130 | } | 130 | } |
131 | void OCOPClient::delChannel(const QCString& chan ) { | 131 | void OCOPClient::delChannel(const QCString& chan ) { |
132 | OCOPPacket pack(OCOPPacket::UnregisterChannel, chan ); | 132 | OCOPPacket pack(OCOPPacket::UnregisterChannel, chan ); |
133 | call( pack ); | 133 | call( pack ); |
134 | } | 134 | } |
135 | void OCOPClient::call( const OCOPPacket& pack ) { | 135 | void OCOPClient::call( const OCOPPacket& pack ) { |
136 | OCOPHead head = pack.head(); | 136 | OCOPHead head = pack.head(); |
137 | write(m_socket, &head, sizeof(head) ); | 137 | write(m_socket, &head, sizeof(head) ); |
138 | write(m_socket, pack.channel().data(), pack.channel().size() ); | 138 | write(m_socket, pack.channel().data(), pack.channel().size() ); |
139 | write(m_socket, pack.header().data(), pack.header().size() ); | 139 | write(m_socket, pack.header().data(), pack.header().size() ); |
140 | write(m_socket, pack.content().data(), pack.content().size() ); | 140 | write(m_socket, pack.content().data(), pack.content().size() ); |
141 | } | 141 | } |
142 | void OCOPClient::startUP() { | 142 | void OCOPClient::startUP() { |
143 | qWarning("Start me up"); | 143 | qWarning("Start me up"); |
144 | pid_t pi = fork(); | 144 | pid_t pi = fork(); |
145 | if ( pi == 0 ) { | 145 | if ( pi == 0 ) { |
146 | setsid(); | 146 | setsid(); |
147 | execlp("opie-ipc", "opie-ipc", NULL ); | 147 | execlp("ocopserver", "ocopserver", NULL ); |
148 | _exit(1); | 148 | _exit(1); |
149 | } | 149 | } |
150 | } | 150 | } |
diff --git a/x11/ipc/server/ocopserver.cpp b/x11/ipc/server/ocopserver.cpp index e76657e..421e49c 100644 --- a/x11/ipc/server/ocopserver.cpp +++ b/x11/ipc/server/ocopserver.cpp | |||
@@ -1,384 +1,386 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <signal.h> | 2 | #include <signal.h> |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <unistd.h> | 5 | #include <unistd.h> |
6 | #include <sys/socket.h> | 6 | #include <sys/socket.h> |
7 | #include <sys/un.h> | 7 | #include <sys/un.h> |
8 | 8 | ||
9 | #include <qcstring.h> | 9 | #include <qcstring.h> |
10 | #include <qtimer.h> | 10 | #include <qtimer.h> |
11 | 11 | ||
12 | #include "ocopserver.h" | 12 | #include "ocopserver.h" |
13 | 13 | ||
14 | OCopServer::OCopServer() | 14 | OCopServer::OCopServer() |
15 | : QObject() | 15 | : QObject() |
16 | { | 16 | { |
17 | setName( "ocopserver"); | 17 | setName( "ocopserver"); |
18 | 18 | ||
19 | /* | 19 | /* |
20 | * init the server | 20 | * init the server |
21 | */ | 21 | */ |
22 | init(); | 22 | init(); |
23 | initSocket(); | 23 | initSocket(); |
24 | } | 24 | } |
25 | OCopServer::~OCopServer() { | 25 | OCopServer::~OCopServer() { |
26 | // socket notifiers should be deleted | 26 | // socket notifiers should be deleted |
27 | close(m_serverfd ); | 27 | close(m_serverfd ); |
28 | } | 28 | } |
29 | void OCopServer::init() { | 29 | void OCopServer::init() { |
30 | /* | 30 | /* |
31 | * we set SIGPIPE to SIG_IGN | 31 | * we set SIGPIPE to SIG_IGN |
32 | * to get EPIPE on reads ;) | 32 | * to get EPIPE on reads ;) |
33 | */ | 33 | */ |
34 | qWarning("SIGPIPE to be ignored"); | 34 | qWarning("SIGPIPE to be ignored"); |
35 | signal(SIGPIPE, SIG_IGN ); | 35 | signal(SIGPIPE, SIG_IGN ); |
36 | 36 | ||
37 | /* | 37 | /* |
38 | * initialize some variables | 38 | * initialize some variables |
39 | */ | 39 | */ |
40 | m_server = 0l; | 40 | m_server = 0l; |
41 | m_serverError = 0l; | 41 | m_serverError = 0l; |
42 | } | 42 | } |
43 | 43 | ||
44 | /** | 44 | /** |
45 | * here we will init our server | 45 | * here we will init our server |
46 | * socket and bind and do the listen | 46 | * socket and bind and do the listen |
47 | */ | 47 | */ |
48 | void OCopServer::initSocket() { | 48 | void OCopServer::initSocket() { |
49 | /* get the home dir */ | 49 | /* get the home dir */ |
50 | QCString home( getenv("HOME") ); | 50 | QCString home( getenv("HOME") ); |
51 | QCString path( home + "/.opie.cop"); | 51 | QCString path( home + "/.opie.cop"); |
52 | 52 | ||
53 | if ( ( m_serverfd = socket( PF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) { | 53 | if ( ( m_serverfd = socket( PF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) { |
54 | qWarning("failed to create server socket"); | 54 | qWarning("failed to create server socket"); |
55 | /* try again later */ | 55 | /* try again later */ |
56 | QTimer::singleShot( 400, this, SLOT(initSocket() ) ); | 56 | QTimer::singleShot( 400, this, SLOT(initSocket() ) ); |
57 | return; | 57 | return; |
58 | } | 58 | } |
59 | qWarning( "unlinking file %s", path.data() ); | 59 | qWarning( "unlinking file %s", path.data() ); |
60 | 60 | ||
61 | /* unlink previous sockets */ | 61 | /* unlink previous sockets */ |
62 | unlink( path.data() ); | 62 | unlink( path.data() ); |
63 | 63 | ||
64 | struct sockaddr_un m_address; | 64 | struct sockaddr_un m_address; |
65 | memset(&m_address, 0, sizeof(m_address ) ); | 65 | memset(&m_address, 0, sizeof(m_address ) ); |
66 | m_address.sun_family = AF_UNIX; /* unix domain socket */ | 66 | m_address.sun_family = AF_UNIX; /* unix domain socket */ |
67 | strcpy(m_address.sun_path, path.data() ); | 67 | strcpy(m_address.sun_path, path.data() ); |
68 | m_adrlaenge = sizeof(m_address.sun_family) + strlen(m_address.sun_path ); | 68 | m_adrlaenge = sizeof(m_address.sun_family) + strlen(m_address.sun_path ); |
69 | 69 | ||
70 | /* cast to make it a (sockadr*) */ | 70 | /* cast to make it a (sockadr*) */ |
71 | if (bind(m_serverfd, (struct sockaddr*)&m_address, m_adrlaenge ) == -1 ) { | 71 | if (bind(m_serverfd, (struct sockaddr*)&m_address, m_adrlaenge ) == -1 ) { |
72 | qWarning("Server could not bind try again"); | 72 | qWarning("Server could not bind try again"); |
73 | close(m_serverfd); | 73 | close(m_serverfd); |
74 | QTimer::singleShot(400, this, SLOT(initSocket() ) ); | 74 | QTimer::singleShot(400, this, SLOT(initSocket() ) ); |
75 | return; | 75 | return; |
76 | } | 76 | } |
77 | 77 | ||
78 | /* tell the kernel that we're listening and accepting | 78 | /* tell the kernel that we're listening and accepting |
79 | * 5 pending connections */ | 79 | * 5 pending connections */ |
80 | if (listen(m_serverfd, 5) == -1 ) { | 80 | if (listen(m_serverfd, 5) == -1 ) { |
81 | qWarning("could not listen"); | 81 | qWarning("could not listen"); |
82 | close(m_serverfd ); | 82 | close(m_serverfd ); |
83 | QTimer::singleShot(400, this, SLOT(initSocket() ) ); | 83 | QTimer::singleShot(400, this, SLOT(initSocket() ) ); |
84 | return; | 84 | return; |
85 | } | 85 | } |
86 | 86 | ||
87 | /* | 87 | /* |
88 | * now we will create two QSocketNotifier | 88 | * now we will create two QSocketNotifier |
89 | * which will us notify on reads | 89 | * which will us notify on reads |
90 | * and errors | 90 | * and errors |
91 | * we do this because they integrate | 91 | * we do this because they integrate |
92 | * nicely into the QApplication eventloop | 92 | * nicely into the QApplication eventloop |
93 | */ | 93 | */ |
94 | m_server = new QSocketNotifier(m_serverfd, QSocketNotifier::Read, this ); | 94 | m_server = new QSocketNotifier(m_serverfd, QSocketNotifier::Read, this ); |
95 | connect( m_server, SIGNAL(activated(int) ), | 95 | connect( m_server, SIGNAL(activated(int) ), |
96 | this, SLOT(newOnServer() ) ); | 96 | this, SLOT(newOnServer() ) ); |
97 | 97 | ||
98 | m_serverError = new QSocketNotifier( m_serverfd, QSocketNotifier::Exception, this); | 98 | m_serverError = new QSocketNotifier( m_serverfd, QSocketNotifier::Exception, this); |
99 | connect(m_serverError, SIGNAL(activated(int) ), | 99 | connect(m_serverError, SIGNAL(activated(int) ), |
100 | this, SLOT(errorOnServer() ) ); | 100 | this, SLOT(errorOnServer() ) ); |
101 | 101 | ||
102 | qWarning("done with registering"); | 102 | qWarning("done with registering"); |
103 | } | 103 | } |
104 | /** | 104 | /** |
105 | * we got the possibility to read | 105 | * we got the possibility to read |
106 | * on the server | 106 | * on the server |
107 | * this is mostly due a connect | 107 | * this is mostly due a connect |
108 | * on a client side | 108 | * on a client side |
109 | * we will accept it | 109 | * we will accept it |
110 | * add it to our list | 110 | * add it to our list |
111 | */ | 111 | */ |
112 | void OCopServer::newOnServer() { | 112 | void OCopServer::newOnServer() { |
113 | int fd = accept(); | 113 | int fd = accept(); |
114 | if ( fd < 0 ) | 114 | if ( fd < 0 ) |
115 | return; | 115 | return; |
116 | 116 | ||
117 | /* | 117 | /* |
118 | * we got a successfull new connection | 118 | * we got a successfull new connection |
119 | * be happy | 119 | * be happy |
120 | * set SocketNotifier | 120 | * set SocketNotifier |
121 | * connect it | 121 | * connect it |
122 | * and a OCOPClient | 122 | * and a OCOPClient |
123 | */ | 123 | */ |
124 | qWarning("Heureka new connection %d", fd ); | 124 | qWarning("Heureka new connection %d", fd ); |
125 | 125 | ||
126 | 126 | ||
127 | registerClient( fd ); | 127 | registerClient( fd ); |
128 | } | 128 | } |
129 | int OCopServer::accept() { | 129 | int OCopServer::accept() { |
130 | /* | 130 | /* |
131 | * accept it | 131 | * accept it |
132 | * the socket is currently blocking IIRC | 132 | * the socket is currently blocking IIRC |
133 | */ | 133 | */ |
134 | return ::accept( m_serverfd, (struct sockaddr*)&m_address, &m_adrlaenge ); | 134 | return ::accept( m_serverfd, (struct sockaddr*)&m_address, &m_adrlaenge ); |
135 | } | 135 | } |
136 | void OCopServer::newOnClient( int fd ) { | 136 | void OCopServer::newOnClient( int fd ) { |
137 | int bug[4096]; | ||
138 | //qWarning("new stuff for client on fd %d", fd ); | ||
139 | errno = 0; | 137 | errno = 0; |
140 | OCOPHead head; | 138 | OCOPHead head; |
141 | memset(&head, 0, sizeof(head) ); | 139 | memset(&head, 0, sizeof(head) ); |
142 | int rea = ::read(fd, &head, sizeof(head) ); | 140 | int rea = ::read(fd, &head, sizeof(head) ); |
143 | //qWarning("read %d %d", rea, errno); | 141 | //qWarning("read %d %d", rea, errno); |
144 | /* | 142 | /* |
145 | * I should get EPIPE but nothing like this happens | 143 | * I should get EPIPE but nothing like this happens |
146 | * so if rea == 0 and we were signaled by the notifier | 144 | * so if rea == 0 and we were signaled by the notifier |
147 | * we close it and drop the clients... | 145 | * we close it and drop the clients... |
148 | */ | 146 | */ |
149 | if ( rea <= 0 ) { | 147 | if ( rea <= 0 ) { |
150 | deregisterClient( fd ); | 148 | deregisterClient( fd ); |
151 | return; | 149 | return; |
152 | } | 150 | } |
153 | /* | 151 | /* |
154 | * OCOPHead | 152 | * OCOPHead |
155 | */ | 153 | */ |
156 | qWarning("data %s %d", bug, rea ); | 154 | //qWarning("data %s %d", &bug, rea ); |
157 | 155 | ||
158 | /* | 156 | /* |
159 | * Check the magic | 157 | * Check the magic |
160 | * if chcked read till EOF if magic does not match | 158 | * if chcked read till EOF if magic does not match |
161 | * otherwise do read | 159 | * otherwise do read |
162 | * channel | 160 | * channel |
163 | * func | 161 | * func |
164 | * data into mem | 162 | * data into mem |
165 | * and then send the OCOPPacket | 163 | * and then send the OCOPPacket |
166 | * | 164 | * |
167 | */ | 165 | */ |
168 | if (head.magic == 47 ) { | 166 | if (head.magic == 47 ) { |
169 | qWarning("magic match"); | 167 | qWarning("magic match"); |
170 | QCString channel( head.chlen+1 ); | 168 | QCString channel( head.chlen+1 ); |
171 | QCString func( head.funclen+1 ); | 169 | QCString func( head.funclen+1 ); |
172 | QByteArray data ( head.datalen ); | 170 | QByteArray data ( head.datalen ); |
173 | 171 | ||
174 | /* | 172 | /* |
175 | * we do not check for errors | 173 | * we do not check for errors |
176 | */ | 174 | */ |
175 | qWarning("read "); | ||
177 | int s = read(fd, channel.data(), head.chlen ); | 176 | int s = read(fd, channel.data(), head.chlen ); |
178 | s = read(fd, func.data(), head.funclen ); | 177 | s = read(fd, func.data(), head.funclen ); |
179 | s = read(fd, data.data(), head.datalen ); | 178 | s = read(fd, data.data(), head.datalen ); |
179 | qWarning("read"); | ||
180 | 180 | ||
181 | /* debug output */ | 181 | /* debug output */ |
182 | qWarning("channel %s %d", channel.data(), head.chlen ); | 182 | qWarning("channel %s %d", channel.data(), head.chlen ); |
183 | qWarning("func %s %d", func.data(), head.funclen ); | 183 | qWarning("func %s %d", func.data(), head.funclen ); |
184 | /* debug end */ | 184 | /* debug end */ |
185 | 185 | ||
186 | /* | 186 | /* |
187 | * now that we got the complete body | 187 | * now that we got the complete body |
188 | * we need to make a package | 188 | * we need to make a package |
189 | * and then we need to send it to clients | 189 | * and then we need to send it to clients |
190 | * making a package is done here | 190 | * making a package is done here |
191 | * dispatching it not | 191 | * dispatching it not |
192 | */ | 192 | */ |
193 | OCOPPacket packet( head.type, channel, func, data ); | 193 | OCOPPacket packet( head.type, channel, func, data ); |
194 | dispatch( packet, fd ); | 194 | dispatch( packet, fd ); |
195 | 195 | ||
196 | }else{ | 196 | }else{ |
197 | qWarning("magic does not match"); | 197 | qWarning("magic does not match"); |
198 | qWarning("magic %d", head.magic ); | 198 | qWarning("magic %d", head.magic ); |
199 | } | 199 | } |
200 | } | 200 | } |
201 | void OCopServer::registerClient( int fd ) { | 201 | void OCopServer::registerClient( int fd ) { |
202 | if (m_clients.contains(fd) ) | 202 | if (m_clients.contains(fd) ) |
203 | return; | 203 | return; |
204 | 204 | ||
205 | QSocketNotifier* notify = new QSocketNotifier(fd, QSocketNotifier::Read, this ); | 205 | QSocketNotifier* notify = new QSocketNotifier(fd, QSocketNotifier::Read, this ); |
206 | connect(notify, SIGNAL(activated(int) ), | 206 | connect(notify, SIGNAL(activated(int) ), |
207 | this, SLOT(newOnClient(int) ) ); | 207 | this, SLOT(newOnClient(int) ) ); |
208 | OCOPClient client; | 208 | OCOPClient client; |
209 | client.fd = fd; | 209 | client.fd = fd; |
210 | client.notify = notify; | 210 | client.notify = notify; |
211 | m_clients.insert( client.fd, client ); | 211 | m_clients.insert( client.fd, client ); |
212 | qWarning("clients are up to %d", m_clients.count() ); | 212 | qWarning("clients are up to %d", m_clients.count() ); |
213 | }; | 213 | }; |
214 | void OCopServer::deregisterClient(int fd ) { | 214 | void OCopServer::deregisterClient(int fd ) { |
215 | QMap<int, OCOPClient>::Iterator it = m_clients.find( fd ); | 215 | QMap<int, OCOPClient>::Iterator it = m_clients.find( fd ); |
216 | if (it != m_clients.end() ) { | 216 | if (it != m_clients.end() ) { |
217 | OCOPClient client = (*it); | 217 | OCOPClient client = (*it); |
218 | delete client.notify; | 218 | delete client.notify; |
219 | m_clients.remove(fd ); | 219 | m_clients.remove(fd ); |
220 | close(fd ); | 220 | close(fd ); |
221 | /* | 221 | /* |
222 | * TIME_ME | 222 | * TIME_ME |
223 | * | 223 | * |
224 | * now delete from all channels | 224 | * now delete from all channels |
225 | * go through all channels | 225 | * go through all channels |
226 | * remove the fd from the list | 226 | * remove the fd from the list |
227 | * if count becomes 0 remove the channel | 227 | * if count becomes 0 remove the channel |
228 | * otherwise replace QArray<int> | 228 | * otherwise replace QArray<int> |
229 | */ | 229 | */ |
230 | QMap<QCString, QValueList<int> >::Iterator it; | 230 | QMap<QCString, QValueList<int> >::Iterator it; |
231 | for ( it = m_channels.begin(); it != m_channels.end(); ++it ) { | 231 | for ( it = m_channels.begin(); it != m_channels.end(); ++it ) { |
232 | /* | 232 | /* |
233 | * The channel contains this fd | 233 | * The channel contains this fd |
234 | */ | 234 | */ |
235 | if ( it.data().contains( fd ) ) { | 235 | if ( it.data().contains( fd ) ) { |
236 | QValueList<int> array = it.data(); | 236 | QValueList<int> array = it.data(); |
237 | 237 | ||
238 | /* | 238 | /* |
239 | * remove channel or just replace | 239 | * remove channel or just replace |
240 | */ | 240 | */ |
241 | if ( array.count() == 1 ) { | 241 | if ( array.count() == 1 ) { |
242 | /* is the list now invalidatet? */ | 242 | /* is the list now invalidatet? */ |
243 | m_channels.remove( it ); | 243 | m_channels.remove( it ); |
244 | }else{ | 244 | }else{ |
245 | array.remove( fd ); | 245 | array.remove( fd ); |
246 | it = m_channels.replace( it.key(), array ); | 246 | it = m_channels.replace( it.key(), array ); |
247 | } | 247 | } |
248 | } | 248 | } |
249 | } // off all channels | 249 | } // off all channels |
250 | } | 250 | } |
251 | qWarning("clients are now at %d", m_clients.count() ); | 251 | qWarning("clients are now at %d", m_clients.count() ); |
252 | }; | 252 | }; |
253 | /** | 253 | /** |
254 | * this function will evaluate | 254 | * this function will evaluate |
255 | * the package and then do the appropriate thins | 255 | * the package and then do the appropriate thins |
256 | */ | 256 | */ |
257 | void OCopServer::dispatch( const OCOPPacket& packet, int sourceFD ) { | 257 | void OCopServer::dispatch( const OCOPPacket& packet, int sourceFD ) { |
258 | qWarning("packet.type() == %d", packet.type() ); | 258 | qWarning("packet.type() == %d", packet.type() ); |
259 | switch( packet.type() ) { | 259 | switch( packet.type() ) { |
260 | case OCOPPacket::Register: | 260 | case OCOPPacket::Register: |
261 | registerClient(sourceFD ); | 261 | registerClient(sourceFD ); |
262 | break; | 262 | break; |
263 | case OCOPPacket::Unregister: | 263 | case OCOPPacket::Unregister: |
264 | deregisterClient(sourceFD ); | 264 | deregisterClient(sourceFD ); |
265 | break; | 265 | break; |
266 | case OCOPPacket::Call: | 266 | case OCOPPacket::Call: |
267 | call( packet, sourceFD ); | 267 | call( packet, sourceFD ); |
268 | break; | 268 | break; |
269 | /* not implemented */ | 269 | /* not implemented */ |
270 | case OCOPPacket::Method: | 270 | case OCOPPacket::Method: |
271 | break; | 271 | break; |
272 | /* nit implemented */ | 272 | /* nit implemented */ |
273 | case OCOPPacket::Reply: | 273 | case OCOPPacket::Reply: |
274 | break; | 274 | break; |
275 | case OCOPPacket::RegisterChannel: | 275 | case OCOPPacket::RegisterChannel: |
276 | addChannel( packet.channel() , sourceFD ); | 276 | addChannel( packet.channel() , sourceFD ); |
277 | break; | 277 | break; |
278 | case OCOPPacket::UnregisterChannel: | 278 | case OCOPPacket::UnregisterChannel: |
279 | delChannel( packet.channel(), sourceFD ); | 279 | delChannel( packet.channel(), sourceFD ); |
280 | break; | 280 | break; |
281 | /* not implemented */ | 281 | /* not implemented */ |
282 | case OCOPPacket::Return: | 282 | case OCOPPacket::Return: |
283 | break; | 283 | break; |
284 | /* not implemented :( */ | 284 | /* not implemented :( */ |
285 | case OCOPPacket::Signal: | 285 | case OCOPPacket::Signal: |
286 | break; | 286 | break; |
287 | case OCOPPacket::IsRegistered: | 287 | case OCOPPacket::IsRegistered: |
288 | qWarning("IsRegistered"); | ||
288 | isRegistered( packet.channel(), sourceFD ); | 289 | isRegistered( packet.channel(), sourceFD ); |
289 | break; | 290 | break; |
290 | }; | 291 | }; |
291 | } | 292 | } |
292 | void OCopServer::errorOnServer() { | 293 | void OCopServer::errorOnServer() { |
293 | /* | 294 | /* |
294 | * something is wrong on the server socket? | 295 | * something is wrong on the server socket? |
295 | * what should we do? | 296 | * what should we do? |
296 | * FIXME | 297 | * FIXME |
297 | */ | 298 | */ |
298 | } | 299 | } |
299 | QStringList OCopServer::channels() { | 300 | QStringList OCopServer::channels() { |
300 | QStringList list; | 301 | QStringList list; |
301 | { | 302 | { |
302 | QMap<QCString, QValueList<int> >::Iterator it; | 303 | QMap<QCString, QValueList<int> >::Iterator it; |
303 | for (it = m_channels.begin(); it != m_channels.end(); ++it ) { | 304 | for (it = m_channels.begin(); it != m_channels.end(); ++it ) { |
304 | list << it.key(); | 305 | list << it.key(); |
305 | }; | 306 | }; |
306 | } | 307 | } |
307 | return list; | 308 | return list; |
308 | } | 309 | } |
309 | bool OCopServer::isChannelRegistered( const QCString& chan ) const{ | 310 | bool OCopServer::isChannelRegistered( const QCString& chan ) const{ |
310 | return m_channels.contains( chan ); | 311 | return m_channels.contains( chan ); |
311 | } | 312 | } |
312 | void OCopServer::addChannel( const QCString& channel, | 313 | void OCopServer::addChannel( const QCString& channel, |
313 | int fd ) { | 314 | int fd ) { |
314 | QMap<QCString, QValueList<int> >::Iterator it; | 315 | QMap<QCString, QValueList<int> >::Iterator it; |
315 | it = m_channels.find( channel ); | 316 | it = m_channels.find( channel ); |
316 | 317 | ||
317 | /* could be empty */ | 318 | /* could be empty */ |
318 | QValueList<int> list = it.data(); | 319 | QValueList<int> list = it.data(); |
319 | list.append( fd ); | 320 | list.append( fd ); |
320 | it = m_channels.replace( channel, list ); | 321 | it = m_channels.replace( channel, list ); |
321 | }; | 322 | }; |
322 | void OCopServer::delChannel( const QCString& channel, | 323 | void OCopServer::delChannel( const QCString& channel, |
323 | int fd ) { | 324 | int fd ) { |
324 | if (!m_channels.contains( channel ) ) | 325 | if (!m_channels.contains( channel ) ) |
325 | return; | 326 | return; |
326 | 327 | ||
327 | QMap<QCString, QValueList<int> >::Iterator it; | 328 | QMap<QCString, QValueList<int> >::Iterator it; |
328 | it = m_channels.find( channel ); | 329 | it = m_channels.find( channel ); |
329 | 330 | ||
330 | if ( it.data().contains(fd) ) { | 331 | if ( it.data().contains(fd) ) { |
331 | 332 | ||
332 | QValueList<int> ints = it.data(); | 333 | QValueList<int> ints = it.data(); |
333 | if ( ints.count() == 1 ) | 334 | if ( ints.count() == 1 ) |
334 | m_channels.remove( it ); | 335 | m_channels.remove( it ); |
335 | else{ | 336 | else{ |
336 | QValueList<int> ints = it.data(); | 337 | QValueList<int> ints = it.data(); |
337 | ints.remove( fd ); | 338 | ints.remove( fd ); |
338 | m_channels.replace( it.key(), ints ); | 339 | m_channels.replace( it.key(), ints ); |
339 | } | 340 | } |
340 | } | 341 | } |
341 | } | 342 | } |
342 | void OCopServer::isRegistered( const QCString& channel, int fd) { | 343 | void OCopServer::isRegistered( const QCString& channel, int fd) { |
344 | qWarning("isRegistered"); | ||
343 | OCOPHead head; | 345 | OCOPHead head; |
344 | QCString func(2); | 346 | QCString func(2); |
345 | 347 | ||
346 | memset(&head, 0, sizeof(head ) ); | 348 | memset(&head, 0, sizeof(head ) ); |
347 | head.magic = 47; | 349 | head.magic = 47; |
348 | head.type = OCOPPacket::IsRegistered; | 350 | head.type = OCOPPacket::IsRegistered; |
349 | head.chlen = channel.size(); | 351 | head.chlen = channel.size(); |
350 | head.funclen = func.size(); | 352 | head.funclen = func.size(); |
351 | head.datalen = 0; | 353 | head.datalen = 0; |
352 | 354 | ||
353 | if ( isChannelRegistered( channel ) ) { | 355 | if ( isChannelRegistered( channel ) ) { |
354 | //is registered | 356 | //is registered |
355 | func[0] = 1; | 357 | func[0] = 1; |
356 | }else{ | 358 | }else{ |
357 | func[0] = 0; | 359 | func[0] = 0; |
358 | } | 360 | } |
359 | 361 | ||
360 | /** | 362 | /** |
361 | * write the head | 363 | * write the head |
362 | * and then channel | 364 | * and then channel |
363 | * success/failure inside func | 365 | * success/failure inside func |
364 | */ | 366 | */ |
365 | write(fd, &head, sizeof(head) ); | 367 | write(fd, &head, sizeof(head) ); |
366 | write(fd, channel.data(), channel.size() ); | 368 | write(fd, channel.data(), channel.size() ); |
367 | write(fd, func.data(), func.size() ); | 369 | write(fd, func.data(), func.size() ); |
368 | } | 370 | } |
369 | QValueList<int> OCopServer::clients( const QCString& channel ) { | 371 | QValueList<int> OCopServer::clients( const QCString& channel ) { |
370 | return m_channels[channel]; | 372 | return m_channels[channel]; |
371 | } | 373 | } |
372 | void OCopServer::call( const OCOPPacket& p, int fd ) { | 374 | void OCopServer::call( const OCOPPacket& p, int ) { |
373 | QValueList<int> cli = clients( p.channel() ); | 375 | QValueList<int> cli = clients( p.channel() ); |
374 | QValueList<int>::Iterator it; | 376 | QValueList<int>::Iterator it; |
375 | 377 | ||
376 | OCOPHead head = p.head(); | 378 | OCOPHead head = p.head(); |
377 | for (it = cli.begin(); it != cli.end(); ++it ) { | 379 | for (it = cli.begin(); it != cli.end(); ++it ) { |
378 | write( (*it), &head, sizeof(head ) ); | 380 | write( (*it), &head, sizeof(head ) ); |
379 | /* expl. shared! */ | 381 | /* expl. shared! */ |
380 | write( (*it), p.channel().data(), p.channel().size() ); | 382 | write( (*it), p.channel().data(), p.channel().size() ); |
381 | write( (*it), p.header().data(), p.header().size() ); | 383 | write( (*it), p.header().data(), p.header().size() ); |
382 | write( (*it), p.content().data(), p.content().size() ); | 384 | write( (*it), p.content().data(), p.content().size() ); |
383 | }; | 385 | }; |
384 | } | 386 | } |
diff --git a/x11/ipc/server/ocopserver.pro b/x11/ipc/server/ocopserver.pro index 1776063..e0a928c 100644 --- a/x11/ipc/server/ocopserver.pro +++ b/x11/ipc/server/ocopserver.pro | |||
@@ -1,9 +1,10 @@ | |||
1 | TEMPLATE= app | 1 | TEMPLATE= app |
2 | DESTDIR = $(OPIEDIR)/bin | ||
2 | CONFIG = qt warn_on debug | 3 | CONFIG = qt warn_on debug |
3 | #CONFIG = qt warn_on release | 4 | #CONFIG = qt warn_on release |
4 | HEADERS = ../common/ocoppacket.h ocopclient.h ocopserver.h | 5 | HEADERS = ../common/ocoppacket.h ocopclient.h ocopserver.h |
5 | SOURCES = ../common/ocoppacket.cpp main.cpp ocopserver.cpp | 6 | SOURCES = ../common/ocoppacket.cpp main.cpp ocopserver.cpp |
6 | INCLUDEPATH+= $(OPIEDIR)/include | 7 | INCLUDEPATH+= $(OPIEDIR)/include |
7 | DEPENDPATH+= $(OPIEDIR)/include | 8 | DEPENDPATH+= $(OPIEDIR)/include |
8 | TARGET = ocopserver | 9 | TARGET = ocopserver |
9 | 10 | ||