JackSocketServerChannel.cpp 12.2 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Copyright (C) 2004-2006 Grame  
  
  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
  along with this program; if not, write to the Free Software 
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include "JackSocketServerChannel.h"
#include "JackRequest.h"
#include "JackServer.h"
#include "JackEngine.h"
#include "JackGlobals.h"
#include "JackClient.h"
26
#include "JackNotification.h"
sletz's avatar
sletz committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <assert.h>

using namespace std;

namespace Jack
{

JackSocketServerChannel::JackSocketServerChannel()
{
    fThread = JackGlobals::MakeThread(this);
    fPollTable = NULL;
    fRebuild = true;
}

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

int JackSocketServerChannel::Open(JackServer* server)
{
    JackLog("JackSocketServerChannel::Open \n");
    fServer = server;

    // Prepare request socket
    if (fRequestListenSocket.Bind(jack_server_dir, 0) < 0) {
        JackLog("JackSocketServerChannel::Open : cannot create result listen socket\n");
        return -1;
    }

    // Prepare for poll
    BuildPoolTable();

    // Start listening
    if (fThread->Start() != 0) {
        jack_error("Cannot start Jack server listener");
        goto error;
    }

    return 0;

error:
    fRequestListenSocket.Close();
    return -1;
}

void JackSocketServerChannel::Close()
{
    fThread->Kill();
    fRequestListenSocket.Close();
}

void JackSocketServerChannel::CreateClient()
{
    JackLog("JackSocketServerChannel::CreateClient socket\n");
    JackClientSocket* socket = fRequestListenSocket.Accept();
    if (socket) {
        fSocketTable[socket->GetFd()] = make_pair( -1, socket);
        fRebuild = true;
    } else {
        jack_error("Client socket cannot be created");
    }
}

92
void JackSocketServerChannel::ClientAdd(int fd, char* name, int* shared_engine, int* shared_client, int* shared_graph, int* result)
sletz's avatar
sletz committed
93
{
94
    JackLog("JackSocketServerChannel::ClientAdd\n");
sletz's avatar
sletz committed
95
    int refnum = -1;
sletz's avatar
sletz committed
96
	*result = fServer->GetEngine()->ClientExternalOpen(name, &refnum, shared_engine, shared_client, shared_graph);
sletz's avatar
sletz committed
97
98
99
100
101
102
103
104
    if (*result == 0) {
        fSocketTable[fd].first = refnum;
        fRebuild = true;
    } else {
        jack_error("Cannot create new client");
    }
}

105
void JackSocketServerChannel::ClientRemove(int fd, int refnum)
sletz's avatar
sletz committed
106
107
108
109
{
    pair<int, JackClientSocket*> elem = fSocketTable[fd];
    JackClientSocket* socket = elem.second;
    assert(socket);
110
    JackLog("JackSocketServerChannel::ClientRemove ref = %d\n", refnum);
sletz's avatar
sletz committed
111
112
113
114
115
116
    fSocketTable.erase(fd);
    socket->Close();
    delete socket;
    fRebuild = true;
}

117
void JackSocketServerChannel::ClientKill(int fd)
sletz's avatar
sletz committed
118
119
120
121
122
123
{
    pair<int, JackClientSocket*> elem = fSocketTable[fd];
    JackClientSocket* socket = elem.second;
    int refnum = elem.first;

    assert(socket);
124
    JackLog("JackSocketServerChannel::ClientKill ref = %d\n", refnum);
sletz's avatar
sletz committed
125
126
127
128

    if (refnum == -1) {  // Should never happen... correspond to a client that started the socket but never opened...
        jack_error("Client not opened");
    } else {
129
        fServer->Notify(refnum, kDeadClient, 0);
sletz's avatar
sletz committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    }

    fSocketTable.erase(fd);
    socket->Close();
    delete socket;
    fRebuild = true;
}

int JackSocketServerChannel::HandleRequest(int fd)
{
    pair<int, JackClientSocket*> elem = fSocketTable[fd];
    JackClientSocket* socket = elem.second;
    assert(socket);

    // Read header
    JackRequest header;
    if (header.Read(socket) < 0) {
        jack_error("HandleRequest: cannot read header");
        return -1;
    }

    // Read data
    switch (header.fType) {
153
154
155
156
157
158
159
160
161
162
	
		case JackRequest::kClientCheck: {
                JackLog("JackRequest::kClientCheck\n");
                JackClientCheckRequest req;
                JackClientCheckResult res;
                if (req.Read(socket) == 0)
					 res.fResult = fServer->GetEngine()->ClientCheck(req.fName, res.fName, req.fOptions, &res.fStatus);
                res.Write(socket);
                break;
            }
sletz's avatar
sletz committed
163

sletz's avatar
sletz committed
164
165
166
167
        case JackRequest::kClientOpen: {
                JackLog("JackRequest::ClientOpen\n");
                JackClientOpenRequest req;
                JackClientOpenResult res;
168
                if (req.Read(socket) == 0)
169
					ClientAdd(fd, req.fName, &res.fSharedEngine, &res.fSharedClient, &res.fSharedGraph, &res.fResult);
sletz's avatar
sletz committed
170
171
172
173
174
175
176
177
                res.Write(socket);
                break;
            }

        case JackRequest::kClientClose: {
                JackLog("JackRequest::ClientClose\n");
                JackClientCloseRequest req;
                JackResult res;
178
                if (req.Read(socket) == 0)
sletz's avatar
sletz committed
179
					res.fResult = fServer->GetEngine()->ClientExternalClose(req.fRefNum);
sletz's avatar
sletz committed
180
                res.Write(socket);
181
                ClientRemove(fd, req.fRefNum);
sletz's avatar
sletz committed
182
183
184
185
186
187
188
                break;
            }

        case JackRequest::kActivateClient: {
                JackActivateRequest req;
                JackResult res;
                JackLog("JackRequest::ActivateClient\n");
189
                if (req.Read(socket) == 0)
190
					res.fResult = fServer->GetEngine()->ClientActivate(req.fRefNum);
sletz's avatar
sletz committed
191
192
193
194
195
196
197
198
                res.Write(socket);
                break;
            }

        case JackRequest::kDeactivateClient: {
                JackLog("JackRequest::DeactivateClient\n");
                JackDeactivateRequest req;
                JackResult res;
199
                if (req.Read(socket) == 0)
200
					res.fResult = fServer->GetEngine()->ClientDeactivate(req.fRefNum);
sletz's avatar
sletz committed
201
202
203
204
205
206
207
208
                res.Write(socket);
                break;
            }

        case JackRequest::kRegisterPort: {
                JackLog("JackRequest::RegisterPort\n");
                JackPortRegisterRequest req;
                JackPortRegisterResult res;
209
                if (req.Read(socket) == 0)
210
					res.fResult = fServer->GetEngine()->PortRegister(req.fRefNum, req.fName, req.fFlags, req.fBufferSize, &res.fPortIndex);
sletz's avatar
sletz committed
211
212
213
214
215
216
217
218
                res.Write(socket);
                break;
            }

        case JackRequest::kUnRegisterPort: {
                JackLog("JackRequest::UnRegisterPort\n");
                JackPortUnRegisterRequest req;
                JackResult res;
219
220
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->PortUnRegister(req.fRefNum, req.fPortIndex);
sletz's avatar
sletz committed
221
222
223
224
225
226
227
228
                res.Write(socket);
                break;
            }

        case JackRequest::kConnectNamePorts: {
                JackLog("JackRequest::ConnectPorts\n");
                JackPortConnectNameRequest req;
                JackResult res;
229
230
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
sletz's avatar
sletz committed
231
232
233
234
235
236
237
238
                res.Write(socket);
                break;
            }

        case JackRequest::kDisconnectNamePorts: {
                JackLog("JackRequest::DisconnectPorts\n");
                JackPortDisconnectNameRequest req;
                JackResult res;
239
240
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
sletz's avatar
sletz committed
241
242
243
244
245
246
247
248
                res.Write(socket);
                break;
            }

        case JackRequest::kConnectPorts: {
                JackLog("JackRequest::ConnectPorts\n");
                JackPortConnectRequest req;
                JackResult res;
249
250
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
sletz's avatar
sletz committed
251
252
253
254
255
256
257
258
                res.Write(socket);
                break;
            }

        case JackRequest::kDisconnectPorts: {
                JackLog("JackRequest::DisconnectPorts\n");
                JackPortDisconnectRequest req;
                JackResult res;
259
260
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
sletz's avatar
sletz committed
261
262
263
264
265
266
267
268
                res.Write(socket);
                break;
            }

        case JackRequest::kSetBufferSize: {
                JackLog("JackRequest::SetBufferSize\n");
                JackSetBufferSizeRequest req;
                JackResult res;
269
270
                if (req.Read(socket) == 0)
					res.fResult = fServer->SetBufferSize(req.fBufferSize);
sletz's avatar
sletz committed
271
272
273
274
275
276
277
278
                res.Write(socket);
                break;
            }

        case JackRequest::kSetFreeWheel: {
                JackLog("JackRequest::SetFreeWheel\n");
                JackSetFreeWheelRequest req;
                JackResult res;
279
280
                if (req.Read(socket) == 0)
					res.fResult = fServer->SetFreewheel(req.fOnOff);
sletz's avatar
sletz committed
281
282
283
284
285
286
287
288
                res.Write(socket);
                break;
            }

        case JackRequest::kReleaseTimebase: {
                JackLog("JackRequest::kReleaseTimebase\n");
                JackReleaseTimebaseRequest req;
                JackResult res;
289
290
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->ReleaseTimebase(req.fRefNum);
sletz's avatar
sletz committed
291
292
293
294
295
296
297
298
                res.Write(socket);
                break;
            }

        case JackRequest::kSetTimebaseCallback: {
                JackLog("JackRequest::kSetTimebaseCallback\n");
                JackSetTimebaseCallbackRequest req;
                JackResult res;
299
300
                if (req.Read(socket) == 0)
					res.fResult = fServer->GetEngine()->SetTimebaseCallback(req.fRefNum, req.fConditionnal);
sletz's avatar
sletz committed
301
302
303
304
305
306
307
                res.Write(socket);
                break;
            }

        case JackRequest::kNotification: {
                JackLog("JackRequest::Notification\n");
                JackClientNotificationRequest req;
308
309
				if (req.Read(socket) == 0)
					fServer->Notify(req.fRefNum, req.fNotify, req.fValue);
sletz's avatar
sletz committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
                break;
            }

        default:
            JackLog("Unknown request %ld\n", header.fType);
            break;
    }

    return 0;
}

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

        JackLog("JackSocketServerChannel::BuildPoolTable size = %d\n", fSocketTable.size() + 1);

        // 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++) {
            JackLog("fSocketTable i = %ld fd = %ld\n", i, it->first);
            fPollTable[i].fd = it->first;
            fPollTable[i].events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
        }
    }
}

bool JackSocketServerChannel::Execute()
{
    // Global poll
    if ((poll(fPollTable, fSocketTable.size() + 1, 10000) < 0) && (errno != EINTR)) {
        jack_error("Engine poll failed err = %s request thread quits...", strerror(errno));
        return false;
    } else {

        // Poll all clients
        for (unsigned int i = 1; i < fSocketTable.size() + 1; i++) {
            int fd = fPollTable[i].fd;
            JackLog("fPollTable i = %ld fd = %ld\n", i, fd);
            if (fPollTable[i].revents & ~POLLIN) {
                jack_error("Poll client error err = %s", strerror(errno));
360
                ClientKill(fd);
sletz's avatar
sletz committed
361
362
363
            } else if (fPollTable[i].revents & POLLIN) {
                if (HandleRequest(fd) < 0) {
                    jack_error("Could not handle external client request");
364
                    //ClientRemove(fd); TO CHECK
sletz's avatar
sletz committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
                }
            }
        }

        // Check the server request socket */
        if (fPollTable[0].revents & POLLERR) {
            jack_error("Error on server request socket err = %s", strerror(errno));
            //return false; TO CHECK
        }

        if (fPollTable[0].revents & POLLIN) {
            CreateClient();
        }
    }

    BuildPoolTable();
    return true;
}

} // end of namespace