summaryrefslogtreecommitdiff
path: root/common/JackConnectionManager.h
blob: ead32092a1d3f897984c6b78aabc674a7fad9810 (plain)
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
26
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
145
146
147
148
149
150
151
152
153
154
155
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Copyright (C) 2004-2008 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.

*/

#ifndef __JackConnectionManager__
#define __JackConnectionManager__

#include "JackConstants.h"
#include "JackActivationCount.h"
#include "JackError.h"
#include "JackCompilerDeps.h"
#include <vector>
#include <assert.h>

namespace Jack
{

struct JackClientControl;

/*!
\brief Utility class.
*/

PRE_PACKED_STRUCTURE
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;
        }

        bool AddItem(jack_int_t index)
        {
            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;
        }

        jack_int_t GetItem(jack_int_t index) const
        {
            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;
        }

} POST_PACKED_STRUCTURE;

/*!
\brief Utility class.
*/

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

        bool fUsed;

    public:

        JackFixedArray1()
        {
            Init();
        }

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

        bool IsAvailable()
        {
            if (fUsed) {
                return false;
            } else {
                fUsed = true;
                return true;
            }
        }

} POST_PACKED_STRUCTURE;

/*!
\brief Utility class.
*/

PRE_PACKED_STRUCTURE
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];
        }

        void ClearItem(jack_int_t index1, jack_int_t index2)
        {
            fTable[index1][index2] = 0;
        }

        /*!
        	\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++;
                }
            }
        }

        void GetOutputTable1(jack_int_t index, jack_int_t* output) const
        {
            for (int i = 0; i < SIZE; i++) {
                output[i] = fTable[i][index];
            }
        }

        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;
        }

        void Copy(JackFixedMatrix& copy)
        {
            for (int i = 0; i < SIZE; i++) {
                memcpy(copy.fTable[i], fTable[i], sizeof(jack_int_t) * SIZE);
            }
        }


} POST_PACKED_STRUCTURE;

/*!
\brief Utility class.
*/

PRE_PACKED_STRUCTURE
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;
                    jack_log("JackLoopFeedback::AddConnectionAux ref1 = %ld ref2 = %ld", ref1, ref2);
                    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;
                    jack_log("JackLoopFeedback::RemoveConnectionAux ref1 = %ld ref2 = %ld", ref1, ref2);
                    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) {
                jack_log("JackLoopFeedback::DecConnection ref1 = %ld ref2 = %ld index = %ld", ref1, ref2, index);
                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;
        }

} POST_PACKED_STRUCTURE;

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

PRE_PACKED_STRUCTURE
struct JackClientTiming
{
    jack_time_t fSignaledAt;
    jack_time_t fAwakeAt;
    jack_time_t fFinishedAt;
    jack_client_state_t fStatus;

    JackClientTiming()
    {
        Init();
    }
    ~JackClientTiming()
    {}

    void Init()
    {
        fSignaledAt = 0;
        fAwakeAt = 0;
        fFinishedAt = 0;
        fStatus = NotTriggered;
    }

} POST_PACKED_STRUCTURE;

/*!
\brief Connection manager.

<UL>
<LI>The <B>fConnection</B> array contains the list (array line) of connected ports for 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 output connected  ports for a given client.
<LI>The <B>fConnectionRef</B> array contains the number of ports connected between two clients.
<LI>The <B>fInputCounter</B> array contains the number of input clients connected to a given for activation purpose.
</UL>
*/

PRE_PACKED_STRUCTURE
class SERVER_EXPORT JackConnectionManager
{

    private:

        JackFixedArray<CONNECTION_NUM_FOR_PORT> fConnection[PORT_NUM_MAX];  /*! Connection matrix: list of connected ports for a given port: needed to compute Mix buffer */
        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 */
        JackLoopFeedback<CONNECTION_NUM_FOR_PORT> fLoopFeedback;		/*! Loop feedback connections */

        bool IsLoopPathAux(int ref1, int ref2) const;

    public:

        JackConnectionManager();
        ~JackConnectionManager();

        // 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;

        /*!
          \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);
        }

        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
        void InitRefNum(int refnum);
        int GetInputRefNum(jack_port_id_t port_index) const;
        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);

        int GetActivation(int refnum) const
        {
            return fInputCounter[refnum].GetValue();
        }

        // Graph
        void ResetGraph(JackClientTiming* timing);
        int ResumeRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing);
        int SuspendRefNum(JackClientControl* control, JackSynchro* table, JackClientTiming* timing, long time_out_usec);
        void TopologicalSort(std::vector<jack_int_t>& sorted);

} POST_PACKED_STRUCTURE;

} // end of namespace

#endif