JackMachClientChannel.cpp 8.29 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
/*
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 "JackMachClientChannel.h"
#include "JackRPCEngine.h"
#include "JackRPCClientServer.c"
#include "JackError.h"
#include "JackLibClient.h"
#include "JackLibGlobals.h"
#include "JackMachThread.h"
#include "JackConstants.h"

namespace Jack
{

JackMachClientChannel::JackMachClientChannel()
{
    fThread = new JackMachThread(this);
}

JackMachClientChannel::~JackMachClientChannel()
{
    delete fThread;
}

// Server <===> client

44
int JackMachClientChannel::Open(const char* name, char* name_res, JackClient* client, jack_options_t options, jack_status_t* status)
sletz's avatar
sletz committed
45
46
47
48
49
50
51
52
{
    JackLog("JackMachClientChannel::Open name = %s\n", name);

    // Connect to server
    if (!fServerPort.ConnectPort(jack_server_entry)) {
        jack_error("Cannot connect to server Mach port");
        return -1;
    }
53
54
55
56
57
	
	// Check name in server
	int result = 0;
	ClientCheck(name, name_res, (int)options, (int*)status, &result);
    if (result < 0) {
58
        jack_error("Client name = %s conflits with another running client", name);
59
60
		return -1;
    }
sletz's avatar
sletz committed
61
62

    // Prepare local port using client name
63
    char buf[JACK_CLIENT_NAME_SIZE];
64
    snprintf(buf, sizeof(buf) - 1, "%s:%s", jack_client_entry, name_res);
sletz's avatar
sletz committed
65
66
67
68
69
70

    if (!fClientPort.AllocatePort(buf, 16)) {
        jack_error("Cannot allocate client Mach port");
        return -1;
    }

71
    JackLibGlobals::fGlobals->fClientTable[fClientPort.GetPort()] = client;
sletz's avatar
sletz committed
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
    return 0;
}

void JackMachClientChannel::Close()
{
    JackLog("JackMachClientChannel::Close\n");
    JackLibGlobals::fGlobals->fClientTable.erase(fClientPort.GetPort());
    fServerPort.DisconnectPort();
    fClientPort.DestroyPort();

    // TO CHECK
    kern_return_t res;
    if ((res = mach_port_destroy(mach_task_self(), fPrivatePort)) != KERN_SUCCESS) {
        jack_error("JackMachClientChannel::Close err = %s", mach_error_string(res));
    }
}

int JackMachClientChannel::Start()
{
    JackLog("JackMachClientChannel::Start\n");
    if (fThread->Start() != 0) {
        jack_error("Cannot start Jack client listener");
        return -1;
    } else {
        return 0;
    }
}

void JackMachClientChannel::Stop()
{
    JackLog("JackMachClientChannel::Stop\n");
    fThread->Kill();
}

106
107
void JackMachClientChannel::ClientCheck(const char* name, char* name_res, int options, int* status, int* result)
{
108
	kern_return_t res = rpc_jack_client_check(fServerPort.GetPort(), (char*)name, name_res, options, status, result);
109
110
111
112
113
114
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::ClientCheck err = %s", mach_error_string(res));
    }
}

sletz's avatar
sletz committed
115
void JackMachClientChannel::ClientOpen(const char* name, int* shared_engine, int* shared_client, int* shared_graph, int* result)
sletz's avatar
sletz committed
116
{
sletz's avatar
sletz committed
117
    kern_return_t res = rpc_jack_client_open(fServerPort.GetPort(), (char*)name, &fPrivatePort, shared_engine, shared_client, shared_graph, result);
sletz's avatar
sletz committed
118
119
    if (res != KERN_SUCCESS) {
        *result = -1;
sletz's avatar
sletz committed
120
        jack_error("JackMachClientChannel::ClientOpen err = %s", mach_error_string(res));
sletz's avatar
sletz committed
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
    }
}

void JackMachClientChannel::ClientClose(int refnum, int* result)
{
    kern_return_t res = rpc_jack_client_close(fPrivatePort, refnum, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::ClientClose err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::ClientActivate(int refnum, int* result)
{
    kern_return_t res = rpc_jack_client_activate(fPrivatePort, refnum, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::ClientActivate err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::ClientDeactivate(int refnum, int* result)
{
    kern_return_t res = rpc_jack_client_deactivate(fPrivatePort, refnum, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::ClientDeactivate err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index, int* result)
{
    kern_return_t res = rpc_jack_port_register(fPrivatePort, refnum, (char*)name, flags, buffer_size, port_index, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::PortRegister err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
{
    kern_return_t res = rpc_jack_port_unregister(fPrivatePort, refnum, port_index, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::PortUnRegister err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
{
    kern_return_t res = rpc_jack_port_connect_name(fPrivatePort, refnum, (char*)src, (char*)dst, result);
    if (res != KERN_SUCCESS) {
        jack_error("JackMachClientChannel::PortConnect err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
{
    kern_return_t res = rpc_jack_port_disconnect_name(fPrivatePort, refnum, (char*)src, (char*)dst, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::PortDisconnect err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
{
    kern_return_t res = rpc_jack_port_connect(fPrivatePort, refnum, src, dst, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::PortConnect err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
{
    kern_return_t res = rpc_jack_port_disconnect(fPrivatePort, refnum, src, dst, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::PortDisconnect err = %s", mach_error_string(res));
    }
}

204
void JackMachClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
sletz's avatar
sletz committed
205
{
206
    kern_return_t res = rpc_jack_set_buffer_size(fPrivatePort, buffer_size, result);
sletz's avatar
sletz committed
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
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::SetBufferSize err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::SetFreewheel(int onoff, int* result)
{
    kern_return_t res = rpc_jack_set_freewheel(fPrivatePort, onoff, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::SetFreewheel err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::ReleaseTimebase(int refnum, int* result)
{
    kern_return_t res = rpc_jack_release_timebase(fPrivatePort, refnum, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::ReleaseTimebase err = %s", mach_error_string(res));
    }
}

void JackMachClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
{
    kern_return_t res = rpc_jack_set_timebase_callback(fPrivatePort, refnum, conditional, result);
    if (res != KERN_SUCCESS) {
        *result = -1;
        jack_error("JackMachClientChannel::SetTimebaseCallback err = %s", mach_error_string(res));
    }
}

bool JackMachClientChannel::Execute()
{
    kern_return_t res;
    if ((res = mach_msg_server(JackRPCClient_server, 1024, fClientPort.GetPort(), 0)) != KERN_SUCCESS) {
        jack_error("JackMachClientChannel::Execute err = %s", mach_error_string(res));
        //fClient->ShutDown();
        return false;
    } else {
        return true;
    }
}

} // end of namespace