JackSocketServerChannel.cpp 8.21 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
3
Copyright (C) 2004-2008 Grame

sletz's avatar
sletz committed
4
5
6
7
8
9
10
11
12
13
14
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
15
along with this program; if not, write to the Free Software
sletz's avatar
sletz committed
16
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
sletz's avatar
sletz committed
17
18
19
20
21
22

*/

#include "JackSocketServerChannel.h"
#include "JackRequest.h"
#include "JackServer.h"
sletz's avatar
sletz committed
23
#include "JackLockedEngine.h"
sletz's avatar
sletz committed
24
#include "JackGlobals.h"
25
#include "JackServerGlobals.h"
sletz's avatar
sletz committed
26
#include "JackClient.h"
27
#include "JackTools.h"
28
#include "JackNotification.h"
29
30
#include "JackException.h"

sletz's avatar
sletz committed
31
#include <assert.h>
sletz's avatar
sletz committed
32
#include <signal.h>
sletz's avatar
sletz committed
33
34
35
36
37
38

using namespace std;

namespace Jack
{

39
JackSocketServerChannel::JackSocketServerChannel():
sletz's avatar
sletz committed
40
    fThread(this), fDecoder(NULL)
sletz's avatar
sletz committed
41
42
43
44
45
46
47
48
49
50
{
    fPollTable = NULL;
    fRebuild = true;
}

JackSocketServerChannel::~JackSocketServerChannel()
{
    delete[] fPollTable;
}

51
int JackSocketServerChannel::Open(const char* server_name, JackServer* server)
sletz's avatar
sletz committed
52
{
53
    jack_log("JackSocketServerChannel::Open");
sletz's avatar
sletz committed
54
  
sletz's avatar
sletz committed
55
    // Prepare request socket
56
    if (fRequestListenSocket.Bind(jack_server_dir, server_name, 0) < 0) {
sletz's avatar
sletz committed
57
        jack_log("JackSocketServerChannel::Open : cannot create result listen socket");
sletz's avatar
sletz committed
58
59
60
61
62
        return -1;
    }

    // Prepare for poll
    BuildPoolTable();
sletz's avatar
sletz committed
63
64
    
    fDecoder = new JackRequestDecoder(server, this);
65
    fServer = server;
sletz's avatar
sletz committed
66
67
68
69
70
    return 0;
}

void JackSocketServerChannel::Close()
{
sletz's avatar
sletz committed
71
   fRequestListenSocket.Close();
72
73
74

    // Close remaining client sockets
    std::map<int, std::pair<int, JackClientSocket*> >::iterator it;
sletz's avatar
sletz committed
75

76
    for (it = fSocketTable.begin(); it != fSocketTable.end(); it++) {
77
78
79
80
81
        pair<int, JackClientSocket*> elem = (*it).second;
        JackClientSocket* socket = elem.second;
        assert(socket);
        socket->Close();
        delete socket;
82
    }
sletz's avatar
sletz committed
83
84
85

    delete fDecoder;
    fDecoder = NULL;
sletz's avatar
sletz committed
86
}
87

88
89
90
91
92
int JackSocketServerChannel::Start()
{
    if (fThread.Start() != 0) {
        jack_error("Cannot start Jack server listener");
        return -1;
sletz's avatar
sletz committed
93
94
    } else {
        return 0;
95
    }
sletz's avatar
sletz committed
96
}
97

sletz's avatar
sletz committed
98
99
100
void JackSocketServerChannel::Stop()
{
    fThread.Kill();
101
}
sletz's avatar
sletz committed
102

sletz's avatar
Cleanup    
sletz committed
103
void JackSocketServerChannel::ClientCreate()
sletz's avatar
sletz committed
104
{
sletz's avatar
sletz committed
105
    jack_log("JackSocketServerChannel::ClientCreate socket");
sletz's avatar
sletz committed
106
107
    JackClientSocket* socket = fRequestListenSocket.Accept();
    if (socket) {
sletz's avatar
sletz committed
108
        fSocketTable[socket->GetFd()] = make_pair(-1, socket);
sletz's avatar
sletz committed
109
110
111
112
113
114
        fRebuild = true;
    } else {
        jack_error("Client socket cannot be created");
    }
}

sletz's avatar
sletz committed
115
116
117
118
int JackSocketServerChannel::GetFd(JackClientSocket* socket_aux)
{
    std::map<int, std::pair<int, JackClientSocket*> >::iterator it;

sletz's avatar
sletz committed
119
    for (it = fSocketTable.begin(); it != fSocketTable.end(); it++) {
sletz's avatar
sletz committed
120
121
122
123
124
125
126
127
128
129
        pair<int, JackClientSocket*> elem = (*it).second;
        JackClientSocket* socket = elem.second;
        if (socket_aux == socket) {
            return (*it).first;
        }
    }
    return -1;
}

void JackSocketServerChannel::ClientAdd(detail::JackChannelTransactionInterface* socket_aux, JackClientOpenRequest* req, JackClientOpenResult *res)
sletz's avatar
sletz committed
130
131
{
    int refnum = -1;
sletz's avatar
sletz committed
132
133
134
135
136
137
    res->fResult = fServer->GetEngine()->ClientExternalOpen(req->fName, req->fPID, req->fUUID, &refnum, &res->fSharedEngine, &res->fSharedClient, &res->fSharedGraph);
    if (res->fResult == 0) {
        JackClientSocket* socket = dynamic_cast<JackClientSocket*>(socket_aux);
        assert(socket);
        int fd = GetFd(socket);
        assert(fd >= 0);
sletz's avatar
sletz committed
138
139
        fSocketTable[fd].first = refnum;
        fRebuild = true;
sletz's avatar
sletz committed
140
        jack_log("JackSocketServerChannel::ClientAdd ref = %d fd = %d", refnum, fd);
Stéphane LETZ's avatar
Stéphane LETZ committed
141
142
143
    #ifdef __APPLE__
        int on = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
sletz's avatar
sletz committed
144
            jack_log("JackSocketServerChannel::ClientAdd : setsockopt SO_NOSIGPIPE fd = %ld err = %s", fd, strerror(errno));
Stéphane LETZ's avatar
Stéphane LETZ committed
145
146
        }
    #endif
sletz's avatar
sletz committed
147
148
149
150
151
    } else {
        jack_error("Cannot create new client");
    }
}

sletz's avatar
sletz committed
152
void JackSocketServerChannel::ClientRemove(detail::JackChannelTransactionInterface* socket_aux, int refnum)
sletz's avatar
sletz committed
153
{
sletz's avatar
sletz committed
154
    JackClientSocket* socket = dynamic_cast<JackClientSocket*>(socket_aux);
sletz's avatar
sletz committed
155
    assert(socket);
sletz's avatar
sletz committed
156
157
158
    int fd = GetFd(socket);
    assert(fd >= 0);

sletz's avatar
sletz committed
159
    jack_log("JackSocketServerChannel::ClientRemove ref = %d fd = %d", refnum, fd);
sletz's avatar
sletz committed
160
161
162
163
164
165
    fSocketTable.erase(fd);
    socket->Close();
    delete socket;
    fRebuild = true;
}

166
void JackSocketServerChannel::ClientKill(int fd)
sletz's avatar
sletz committed
167
168
169
170
171
{
    pair<int, JackClientSocket*> elem = fSocketTable[fd];
    JackClientSocket* socket = elem.second;
    int refnum = elem.first;
    assert(socket);
sletz's avatar
sletz committed
172
    
sletz's avatar
sletz committed
173
    if (refnum == -1) {  // Should never happen... correspond to a client that started the socket but never opened...
sletz's avatar
sletz committed
174
        jack_log("Client was not opened : probably correspond to server_check");
sletz's avatar
sletz committed
175
    } else {
176
        fServer->ClientKill(refnum);
sletz's avatar
sletz committed
177
178
    }

sletz's avatar
sletz committed
179
    jack_log("JackSocketServerChannel::ClientKill ref = %d fd = %d", refnum, fd);
sletz's avatar
sletz committed
180
181
182
183
184
185
186
187
188
189
190
191
192
    fSocketTable.erase(fd);
    socket->Close();
    delete socket;
    fRebuild = true;
}

void JackSocketServerChannel::BuildPoolTable()
{
    if (fRebuild) {
        fRebuild = false;
        delete[] fPollTable;
        fPollTable = new pollfd[fSocketTable.size() + 1];

sletz's avatar
sletz committed
193
        jack_log("JackSocketServerChannel::BuildPoolTable size = %d", fSocketTable.size() + 1);
sletz's avatar
sletz committed
194
195
196
197
198
199
200
201
202
203

        // First fd is the server request socket
        fPollTable[0].fd = fRequestListenSocket.GetFd();
        fPollTable[0].events = POLLIN | POLLERR;

        // Next fd for clients
        map<int, pair<int, JackClientSocket*> >::iterator it;
        int i;

        for (i = 1, it = fSocketTable.begin(); it != fSocketTable.end(); it++, i++) {
sletz's avatar
sletz committed
204
            jack_log("JackSocketServerChannel::BuildPoolTable fSocketTable i = %ld fd = %ld", i, it->first);
sletz's avatar
sletz committed
205
206
207
208
209
210
            fPollTable[i].fd = it->first;
            fPollTable[i].events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
        }
    }
}

sletz's avatar
sletz committed
211
212
213
214
215
216
217
218
219
bool JackSocketServerChannel::Init()
{
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGPIPE);
    pthread_sigmask(SIG_BLOCK, &set, 0);
    return true;
}

sletz's avatar
sletz committed
220
221
bool JackSocketServerChannel::Execute()
{
222
    try {
223

224
225
        // Global poll
        if ((poll(fPollTable, fSocketTable.size() + 1, 10000) < 0) && (errno != EINTR)) {
sletz's avatar
sletz committed
226
            jack_error("JackSocketServerChannel::Execute : engine poll failed err = %s request thread quits...", strerror(errno));
227
228
229
230
231
232
            return false;
        } else {

            // Poll all clients
            for (unsigned int i = 1; i < fSocketTable.size() + 1; i++) {
                int fd = fPollTable[i].fd;
sletz's avatar
sletz committed
233
                jack_log("JackSocketServerChannel::Execute : fPollTable i = %ld fd = %ld", i, fd);
234
                if (fPollTable[i].revents & ~POLLIN) {
sletz's avatar
sletz committed
235
                    jack_log("JackSocketServerChannel::Execute : poll client error err = %s", strerror(errno));
236
237
                    ClientKill(fd);
                } else if (fPollTable[i].revents & POLLIN) {
sletz's avatar
sletz committed
238
                    JackClientSocket* socket = fSocketTable[fd].second;
sletz's avatar
sletz committed
239
                    // Decode header
sletz's avatar
sletz committed
240
241
                    JackRequest header;
                    if (header.Read(socket) < 0) {
sletz's avatar
sletz committed
242
                        jack_log("JackSocketServerChannel::Execute : cannot decode header");
sletz's avatar
sletz committed
243
                        ClientKill(fd);
sletz's avatar
sletz committed
244
                    // Decode request
sletz's avatar
sletz committed
245
246
247
                    } else {
                        // Result is not needed here
                        fDecoder->HandleRequest(socket, header.fType);
sletz's avatar
sletz committed
248
                    }
249
                }
sletz's avatar
sletz committed
250
            }
251
252

            // Check the server request socket */
sletz's avatar
sletz committed
253
            if (fPollTable[0].revents & POLLERR) {
254
                jack_error("Error on server request socket err = %s", strerror(errno));
sletz's avatar
sletz committed
255
            }
256

sletz's avatar
sletz committed
257
            if (fPollTable[0].revents & POLLIN) {
258
                ClientCreate();
sletz's avatar
sletz committed
259
            }
sletz's avatar
sletz committed
260
261
        }

262
263
        BuildPoolTable();
        return true;
264

265
    } catch (JackQuitException& e) {
sletz's avatar
sletz committed
266
        jack_log("JackSocketServerChannel::Execute : JackQuitException");
267
        return false;
sletz's avatar
sletz committed
268
269
270
271
272
273
    }
}

} // end of namespace