JackLibClient.cpp 4.54 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
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
/*
Copyright (C) 2004-2006 Grame  

This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "JackLibClient.h"
#include "JackTime.h"
#include "JackLibGlobals.h"
#include "JackGlobals.h"
#include "JackChannel.h"

namespace Jack
{

// Used for external C API (JackAPI.cpp)
JackGraphManager* GetGraphManager()
{
    assert(JackLibGlobals::fGlobals->fGraphManager);
    return JackLibGlobals::fGlobals->fGraphManager;
}

JackEngineControl* GetEngineControl()
{
    assert(JackLibGlobals::fGlobals->fEngineControl);
    return JackLibGlobals::fGlobals->fEngineControl;
}

JackSynchro** GetSynchroTable()
{
    assert(JackLibGlobals::fGlobals);
    return JackLibGlobals::fGlobals->fSynchroTable;
}

//-------------------
// Client management
//-------------------

JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
{
    JackLog("JackLibClient::JackLibClient table = %x\n", table);
    fChannel = JackGlobals::MakeClientChannel();
}

JackLibClient::~JackLibClient()
{
    JackLog("JackLibClient::~JackLibClient\n");
    delete fChannel;
}

int JackLibClient::Open(const char* name)
{
    int shared_engine, shared_client, shared_ports, result;
    JackLog("JackLibClient::Open %s\n", name);

    // Open server/client channel
    if (fChannel->Open(name, this) < 0) {
        jack_error("Cannot connect to the server");
        goto error;
    }

    // Start receiving notifications
    if (fChannel->Start() < 0) {
        jack_error("Cannot start channel");
        goto error;
    }

    // Require new client
    fChannel->ClientNew(name, &shared_engine, &shared_client, &shared_ports, &result);
    if (result < 0) {
        jack_error("Cannot open %s client", name);
        goto error;
    }

    try {
        // Map shared memory segments
        JackLibGlobals::fGlobals->fEngineControl = shared_engine;
        JackLibGlobals::fGlobals->fGraphManager = shared_ports;
        fClientControl = shared_client;
        verbose = GetEngineControl()->fVerbose;
    } catch (int n) {
        jack_error("Map shared memory segments exception %d", n);
        goto error;
    }

    SetupDriverSync(false);

    // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
    if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
        jack_error("Cannot ConnectSemaphore %s client", name);
        goto error;
    }

107
    JackLog("JackLibClient::Open name = %s refnum = %ld\n", name, fClientControl->fRefNum);
sletz's avatar
sletz committed
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
    return 0;

error:
    fChannel->Stop();
    fChannel->Close();
    return -1;
}

// Notifications received from the server
// TODO this should be done once for all clients in the process, when a shared notification channel
// will be shared by all clients...
int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
{
    int res = 0;

    // Done all time
    switch (notify) {

        case JackNotifyChannelInterface::kAddClient:
            JackLog("JackClient::AddClient name = %s, ref = %ld \n", name, refnum);
            // the synchro must be usable in I/O mode when several clients live in the same process
            res = fSynchroTable[refnum]->Connect(name) ? 0 : -1;
            break;

        case JackNotifyChannelInterface::kRemoveClient:
            JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", name, refnum);
            if (strcmp(GetClientControl()->fName, name) != 0)
                res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
            break;
    }

    return res;
}

JackGraphManager* JackLibClient::GetGraphManager() const
{
    assert(JackLibGlobals::fGlobals->fGraphManager);
    return JackLibGlobals::fGlobals->fGraphManager;
}

JackEngineControl* JackLibClient::GetEngineControl() const
{
    assert(JackLibGlobals::fGlobals->fEngineControl);
    return JackLibGlobals::fGlobals->fEngineControl;
}

JackClientControl* JackLibClient::GetClientControl() const
{
    return fClientControl;
}

} // end of namespace