JackConnectionManager.h 12.7 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
3
4

This program is free software; you can redistribute it and/or modify
sletz's avatar
sletz committed
5
6
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
sletz's avatar
sletz committed
7
8
9
10
11
(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
sletz's avatar
sletz committed
12
GNU Lesser General Public License for more details.
sletz's avatar
sletz committed
13

sletz's avatar
sletz committed
14
15
16
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.
sletz's avatar
sletz committed
17
18
19
20
21
22
23
24

*/

#ifndef __JackConnectionManager__
#define __JackConnectionManager__

#include "JackConstants.h"
#include "JackActivationCount.h"
sletz's avatar
sletz committed
25
#include "JackError.h"
26
#include "JackCompilerDeps.h"
sletz's avatar
sletz committed
27

sletz's avatar
sletz committed
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
#include <assert.h>

namespace Jack
{

struct JackClientControl;

/*!
\brief Utility class.
*/

template <int SIZE>
class JackFixedArray
{

    private:

        jack_int_t fTable[SIZE];
        uint32_t fCounter;

    public:

        JackFixedArray()
        {
            Init();
        }

        void Init()
        {
            for (int i = 0; i < SIZE; i++)
                fTable[i] = EMPTY;
            fCounter = 0;
        }

sletz's avatar
sletz committed
62
        bool AddItem(jack_int_t index)
sletz's avatar
sletz committed
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
92
93
94
        {
            for (int i = 0; i < SIZE; i++) {
                if (fTable[i] == EMPTY) {
                    fTable[i] = index;
                    fCounter++;
                    return true;
                }
            }
            return false;
        }

        bool RemoveItem(jack_int_t index)
        {
            for (int i = 0; i < SIZE; i++) {
                if (fTable[i] == index) {
                    fCounter--;
                    // Shift all indexes
                    if (i == SIZE - 1) {
                        fTable[i] = EMPTY;
                    } else {
                        int j;
                        for (j = i; j <= SIZE - 2 && fTable[j] != EMPTY; j++) {
                            fTable[j] = fTable[j + 1];
                        }
                        fTable[j] = EMPTY;
                    }
                    return true;
                }
            }
            return false;
        }

sletz's avatar
sletz committed
95
        jack_int_t GetItem(jack_int_t index) const
sletz's avatar
sletz committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        {
            return (index < SIZE) ? fTable[index] : EMPTY;
        }

        const jack_int_t* GetItems() const
        {
            return fTable;
        }

        bool CheckItem(jack_int_t index) const
        {
            for (int i = 0; i < SIZE && fTable[i] != EMPTY; i++) {
                if (fTable[i] == index)
                    return true;
            }
            return false;
        }

        uint32_t GetItemCount() const
        {
            return fCounter;
        }

119
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

/*!
\brief Utility class.
*/

template <int SIZE>
class JackFixedArray1 : public JackFixedArray<SIZE>
{
    private:

        bool fUsed;

    public:

        JackFixedArray1()
        {
            Init();
        }

        void Init()
        {
            JackFixedArray<SIZE>::Init();
            fUsed = false;
        }

sletz's avatar
sletz committed
145
        bool IsAvailable()
sletz's avatar
sletz committed
146
147
148
149
150
151
152
153
        {
            if (fUsed) {
                return false;
            } else {
                fUsed = true;
                return true;
            }
        }
154
155
        
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

/*!
\brief Utility class.
*/

template <int SIZE>
class JackFixedMatrix
{
    private:

        jack_int_t fTable[SIZE][SIZE];

    public:

        JackFixedMatrix()
        {}

        void Init(jack_int_t index)
        {
            for (int i = 0; i < SIZE; i++) {
                fTable[index][i] = 0;
                fTable[i][index] = 0;
            }
        }

        const jack_int_t* GetItems(jack_int_t index) const
        {
            return fTable[index];
        }

        jack_int_t IncItem(jack_int_t index1, jack_int_t index2)
        {
            fTable[index1][index2]++;
            return fTable[index1][index2];
        }

        jack_int_t DecItem(jack_int_t index1, jack_int_t index2)
        {
            fTable[index1][index2]--;
            return fTable[index1][index2];
        }

        jack_int_t GetItemCount(jack_int_t index1, jack_int_t index2) const
        {
            return fTable[index1][index2];
        }

        /*!
        	\brief Get the output indexes of a given index.
        */
        void GetOutputTable(jack_int_t index, jack_int_t* output) const
        {
            int i, j;

            for (i = 0; i < SIZE; i++)
                output[i] = EMPTY;

            for (i = 0, j = 0; i < SIZE; i++) {
                if (fTable[index][i] > 0) {
                    output[j] = i;
                    j++;
                }
            }
        }

        bool IsInsideTable(jack_int_t index, jack_int_t* output) const
        {
            for (int i = 0; i < SIZE && output[i] != EMPTY; i++) {
                if (output[i] == index)
                    return true;
            }
            return false;
        }

230
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

/*!
\brief Utility class.
*/

template <int SIZE>
class JackLoopFeedback
{
    private:

        int fTable[SIZE][3];

        /*!
        	\brief Add a feedback connection between 2 refnum.
        */
        bool AddConnectionAux(int ref1, int ref2)
        {
            for (int i = 0; i < SIZE; i++) {
                if (fTable[i][0] == EMPTY) {
                    fTable[i][0] = ref1;
                    fTable[i][1] = ref2;
                    fTable[i][2] = 1;
sletz's avatar
sletz committed
253
                    jack_log("JackLoopFeedback::AddConnectionAux ref1 = %ld ref2 = %ld", ref1, ref2);
sletz's avatar
sletz committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
                    return true;
                }
            }
            jack_error("Feedback table is full !!\n");
            return false;
        }

        /*!
        	\brief Remove a feedback connection between 2 refnum.
        */
        bool RemoveConnectionAux(int ref1, int ref2)
        {
            for (int i = 0; i < SIZE; i++) {
                if (fTable[i][0] == ref1 && fTable[i][1] == ref2) {
                    fTable[i][0] = EMPTY;
                    fTable[i][1] = EMPTY;
                    fTable[i][2] = 0;
sletz's avatar
sletz committed
271
                    jack_log("JackLoopFeedback::RemoveConnectionAux ref1 = %ld ref2 = %ld", ref1, ref2);
sletz's avatar
sletz committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
                    return true;
                }
            }
            jack_error("Feedback connection not found\n");
            return false;
        }

        int IncConnection(int index)
        {
            fTable[index][2]++;
            return fTable[index][2];
        }

        int DecConnection(int index)
        {
            fTable[index][2]--;
            return fTable[index][2];
        }

    public:

        JackLoopFeedback()
        {
            Init();
        }

        void Init()
        {
            for (int i = 0; i < SIZE; i++) {
                fTable[i][0] = EMPTY;
                fTable[i][1] = EMPTY;
                fTable[i][2] = 0;
            }
        }

        bool IncConnection(int ref1, int ref2)
        {
            int index = GetConnectionIndex(ref1, ref2);

            if (index >= 0) { // Feedback connection is already added, increment counter
                IncConnection(index);
                return true;
            } else {
                return AddConnectionAux(ref1, ref2); // Add the feedback connection
            }
        }

        bool DecConnection(int ref1, int ref2)
        {
            int index = GetConnectionIndex(ref1, ref2);

            if (index >= 0) {
sletz's avatar
sletz committed
324
                jack_log("JackLoopFeedback::DecConnection ref1 = %ld ref2 = %ld index = %ld", ref1, ref2, index);
sletz's avatar
sletz committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
                return (DecConnection(index) == 0) ? RemoveConnectionAux(ref1, ref2) : true;
            } else {
                return false;
            }
        }

        /*!
        	\brief Test if a connection between 2 refnum is a feedback connection.
        */
        int GetConnectionIndex(int ref1, int ref2) const
        {
            for (int i = 0; i < SIZE; i++) {
                if (fTable[i][0] == ref1 && fTable[i][1] == ref2)
                    return i;
            }
            return -1;
        }

343
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
344
345
346
347
348
349
350
351
352
353

/*!
\brief For client timing measurements.
*/

struct JackClientTiming
{
    jack_time_t fSignaledAt;
    jack_time_t fAwakeAt;
    jack_time_t fFinishedAt;
sletz's avatar
sletz committed
354
355
356
    jack_client_state_t fStatus;

    JackClientTiming(): fSignaledAt(0), fAwakeAt(0), fFinishedAt(0), fStatus(NotTriggered)
sletz's avatar
sletz committed
357
    {}
sletz's avatar
sletz committed
358
    ~JackClientTiming()
sletz's avatar
sletz committed
359
    {}
360
    
sletz's avatar
sletz committed
361
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
362
363
364

/*!
\brief Connection manager.
sletz's avatar
sletz committed
365

sletz's avatar
sletz committed
366
367
368
369
370
<UL>
<LI>The <B>fConnection</B> array contains the list (array line) of connected ports for a given port.
<LI>The <B>fConnectionCount</B> array contains the number of connected ports to a given port.
<LI>The <B>fInputPort</B> array contains the list (array line) of input connected  ports for a given client.
<LI>The <B>fOutputPort</B> array contains the list (array line) of ouput connected  ports for a given client.
sletz's avatar
sletz committed
371
372
373
<LI>The <B>fConnectionRef</B> array contains the number of ports connected between two clients.
<LI>The <B>fInputRef</B> array contains the number of input clients connected to a given client.
<LI>The <B>fInputCounter</B> array contains the number of input clients connected to a given for activation purpose.
sletz's avatar
sletz committed
374
375
376
</UL>
*/

377
class SERVER_EXPORT JackConnectionManager
sletz's avatar
sletz committed
378
379
380
381
{

    private:

382
        JackFixedArray<CONNECTION_NUM_FOR_PORT> fConnection[PORT_NUM];  /*! Connection matrix: list of connected ports for a given port: needed to compute Mix buffer */
sletz's avatar
sletz committed
383
384
385
386
        JackFixedArray1<PORT_NUM_FOR_CLIENT> fInputPort[CLIENT_NUM];	/*! Table of input port per refnum : to find a refnum for a given port */
        JackFixedArray<PORT_NUM_FOR_CLIENT> fOutputPort[CLIENT_NUM];	/*! Table of output port per refnum : to find a refnum for a given port */
        JackFixedMatrix<CLIENT_NUM> fConnectionRef;						/*! Table of port connections by (refnum , refnum) */
        JackActivationCount fInputCounter[CLIENT_NUM];					/*! Activation counter per refnum */
387
        JackLoopFeedback<CONNECTION_NUM_FOR_PORT> fLoopFeedback;		/*! Loop feedback connections */
sletz's avatar
sletz committed
388
389

        bool IsLoopPathAux(int ref1, int ref2) const;
sletz's avatar
sletz committed
390

sletz's avatar
sletz committed
391
392
393
    public:

        JackConnectionManager();
sletz's avatar
Cleanup    
sletz committed
394
        ~JackConnectionManager();
sletz's avatar
sletz committed
395
396
397
398
399
400

        // Connections management
        int Connect(jack_port_id_t port_src, jack_port_id_t port_dst);
        int Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst);
        bool IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst) const;

sletz's avatar
sletz committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
        /*!
          \brief Get the connection number of a given port.
        */
        jack_int_t Connections(jack_port_id_t port_index) const
        {
            return fConnection[port_index].GetItemCount();
        }

        jack_port_id_t GetPort(jack_port_id_t port_index, int connection) const
        {
            assert(connection < CONNECTION_NUM_FOR_PORT);
            return (jack_port_id_t)fConnection[port_index].GetItem(connection);
        }

sletz's avatar
sletz committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
        const jack_int_t* GetConnections(jack_port_id_t port_index) const;

        bool IncFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
        bool DecFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
        bool IsFeedbackConnection(jack_port_id_t port_src, jack_port_id_t port_dst) const;

        bool IsLoopPath(jack_port_id_t port_src, jack_port_id_t port_dst) const;
        void IncDirectConnection(jack_port_id_t port_src, jack_port_id_t port_dst);
        void DecDirectConnection(jack_port_id_t port_src, jack_port_id_t port_dst);

        // Ports management
        int AddInputPort(int refnum, jack_port_id_t port_index);
        int AddOutputPort(int refnum, jack_port_id_t port_index);

        int RemoveInputPort(int refnum, jack_port_id_t port_index);
        int RemoveOutputPort(int refnum, jack_port_id_t port_index);

        const jack_int_t* GetInputPorts(int refnum);
        const jack_int_t* GetOutputPorts(int refnum);

        // Client management
sletz's avatar
sletz committed
436
437
        void InitRefNum(int refnum);
        int GetInputRefNum(jack_port_id_t port_index) const;
sletz's avatar
sletz committed
438
439
440
441
442
443
444
        int GetOutputRefNum(jack_port_id_t port_index) const;

        // Connect/Disconnect 2 refnum "directly"
        bool IsDirectConnection(int ref1, int ref2) const;
        void DirectConnect(int ref1, int ref2);
        void DirectDisconnect(int ref1, int ref2);

445
446
447
448
449
        int GetActivation(int refnum) const
        {
            return fInputCounter[refnum].GetValue();
        }

sletz's avatar
sletz committed
450
451
        // Graph
        void ResetGraph(JackClientTiming* timing);
452
453
        int ResumeRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing);
        int SuspendRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing, long time_out_usec);
sletz's avatar
sletz committed
454
        
455
} POST_PACKED_STRUCTURE;
sletz's avatar
sletz committed
456
457
458
459
460

} // end of namespace

#endif