2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackSocketClientChannel.h"
21 #include "JackRequest.h"
22 #include "JackClient.h"
23 #include "JackGlobals.h"
28 JackSocketClientChannel::JackSocketClientChannel():
31 fNotificationSocket
= NULL
;
35 JackSocketClientChannel::~JackSocketClientChannel()
37 delete fNotificationSocket
;
40 int JackSocketClientChannel::ServerCheck(const char* server_name
)
42 jack_log("JackSocketClientChannel::ServerCheck = %s", server_name
);
45 if (fRequestSocket
.Connect(jack_server_dir
, server_name
, 0) < 0) {
46 jack_error("Cannot connect to server socket");
47 fRequestSocket
.Close();
54 int JackSocketClientChannel::Open(const char* server_name
, const char* name
, char* name_res
, JackClient
* obj
, jack_options_t options
, jack_status_t
* status
)
57 jack_log("JackSocketClientChannel::Open name = %s", name
);
59 if (fRequestSocket
.Connect(jack_server_dir
, server_name
, 0) < 0) {
60 jack_error("Cannot connect to server socket");
64 // Check name in server
65 ClientCheck(name
, name_res
, JACK_PROTOCOL_VERSION
, (int)options
, (int*)status
, &result
);
67 int status1
= *status
;
68 if (status1
& JackVersionError
)
69 jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION
);
71 jack_error("Client name = %s conflits with another running client", name
);
75 if (fNotificationListenSocket
.Bind(jack_client_dir
, name_res
, 0) < 0) {
76 jack_error("Cannot bind socket");
84 fRequestSocket
.Close();
85 fNotificationListenSocket
.Close();
89 void JackSocketClientChannel::Close()
91 fRequestSocket
.Close();
92 fNotificationListenSocket
.Close();
93 if (fNotificationSocket
)
94 fNotificationSocket
->Close();
97 int JackSocketClientChannel::Start()
99 jack_log("JackSocketClientChannel::Start");
101 To be sure notification thread is started before ClientOpen is called.
103 if (fThread
.StartSync() != 0) {
104 jack_error("Cannot start Jack client listener");
112 void JackSocketClientChannel::Stop()
114 jack_log("JackSocketClientChannel::Stop");
118 void JackSocketClientChannel::ServerSyncCall(JackRequest
* req
, JackResult
* res
, int* result
)
120 if (req
->Write(&fRequestSocket
) < 0) {
121 jack_error("Could not write request type = %ld", req
->fType
);
126 if (res
->Read(&fRequestSocket
) < 0) {
127 jack_error("Could not read result type = %ld", req
->fType
);
132 *result
= res
->fResult
;
135 void JackSocketClientChannel::ServerAsyncCall(JackRequest
* req
, JackResult
* res
, int* result
)
137 if (req
->Write(&fRequestSocket
) < 0) {
138 jack_error("Could not write request type = %ld", req
->fType
);
145 void JackSocketClientChannel::ClientCheck(const char* name
, char* name_res
, int protocol
, int options
, int* status
, int* result
)
147 JackClientCheckRequest
req(name
, protocol
, options
);
148 JackClientCheckResult res
;
149 ServerSyncCall(&req
, &res
, result
);
150 *status
= res
.fStatus
;
151 strcpy(name_res
, res
.fName
);
154 void JackSocketClientChannel::ClientOpen(const char* name
, int pid
, int* shared_engine
, int* shared_client
, int* shared_graph
, int* result
)
156 JackClientOpenRequest
req(name
, pid
);
157 JackClientOpenResult res
;
158 ServerSyncCall(&req
, &res
, result
);
159 *shared_engine
= res
.fSharedEngine
;
160 *shared_client
= res
.fSharedClient
;
161 *shared_graph
= res
.fSharedGraph
;
164 void JackSocketClientChannel::ClientClose(int refnum
, int* result
)
166 JackClientCloseRequest
req(refnum
);
168 ServerSyncCall(&req
, &res
, result
);
171 void JackSocketClientChannel::ClientActivate(int refnum
, int is_real_time
, int* result
)
173 JackActivateRequest
req(refnum
, is_real_time
);
175 ServerSyncCall(&req
, &res
, result
);
178 void JackSocketClientChannel::ClientDeactivate(int refnum
, int* result
)
180 JackDeactivateRequest
req(refnum
);
182 ServerSyncCall(&req
, &res
, result
);
185 void JackSocketClientChannel::PortRegister(int refnum
, const char* name
, const char* type
, unsigned int flags
, unsigned int buffer_size
, jack_port_id_t
* port_index
, int* result
)
187 JackPortRegisterRequest
req(refnum
, name
, type
, flags
, buffer_size
);
188 JackPortRegisterResult res
;
189 ServerSyncCall(&req
, &res
, result
);
190 *port_index
= res
.fPortIndex
;
193 void JackSocketClientChannel::PortUnRegister(int refnum
, jack_port_id_t port_index
, int* result
)
195 JackPortUnRegisterRequest
req(refnum
, port_index
);
197 ServerSyncCall(&req
, &res
, result
);
200 void JackSocketClientChannel::PortConnect(int refnum
, const char* src
, const char* dst
, int* result
)
202 JackPortConnectNameRequest
req(refnum
, src
, dst
);
204 ServerSyncCall(&req
, &res
, result
);
207 void JackSocketClientChannel::PortDisconnect(int refnum
, const char* src
, const char* dst
, int* result
)
209 JackPortDisconnectNameRequest
req(refnum
, src
, dst
);
211 ServerSyncCall(&req
, &res
, result
);
214 void JackSocketClientChannel::PortConnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
, int* result
)
216 JackPortConnectRequest
req(refnum
, src
, dst
);
218 ServerSyncCall(&req
, &res
, result
);
221 void JackSocketClientChannel::PortDisconnect(int refnum
, jack_port_id_t src
, jack_port_id_t dst
, int* result
)
223 JackPortDisconnectRequest
req(refnum
, src
, dst
);
225 ServerSyncCall(&req
, &res
, result
);
228 void JackSocketClientChannel::PortRename(int refnum
, jack_port_id_t port
, const char* name
, int* result
)
230 JackPortRenameRequest
req(refnum
, port
, name
);
232 ServerSyncCall(&req
, &res
, result
);
235 void JackSocketClientChannel::SetBufferSize(jack_nframes_t buffer_size
, int* result
)
237 JackSetBufferSizeRequest
req(buffer_size
);
239 ServerSyncCall(&req
, &res
, result
);
242 void JackSocketClientChannel::SetFreewheel(int onoff
, int* result
)
244 JackSetFreeWheelRequest
req(onoff
);
246 ServerSyncCall(&req
, &res
, result
);
249 void JackSocketClientChannel::ReleaseTimebase(int refnum
, int* result
)
251 JackReleaseTimebaseRequest
req(refnum
);
253 ServerSyncCall(&req
, &res
, result
);
256 void JackSocketClientChannel::SetTimebaseCallback(int refnum
, int conditional
, int* result
)
258 JackSetTimebaseCallbackRequest
req(refnum
, conditional
);
260 ServerSyncCall(&req
, &res
, result
);
263 void JackSocketClientChannel::GetInternalClientName(int refnum
, int int_ref
, char* name_res
, int* result
)
265 JackGetInternalClientNameRequest
req(refnum
, int_ref
);
266 JackGetInternalClientNameResult res
;
267 ServerSyncCall(&req
, &res
, result
);
268 strcpy(name_res
, res
.fName
);
271 void JackSocketClientChannel::InternalClientHandle(int refnum
, const char* client_name
, int* status
, int* int_ref
, int* result
)
273 JackInternalClientHandleRequest
req(refnum
, client_name
);
274 JackInternalClientHandleResult res
;
275 ServerSyncCall(&req
, &res
, result
);
276 *int_ref
= res
.fIntRefNum
;
277 *status
= res
.fStatus
;
280 void JackSocketClientChannel::InternalClientLoad(int refnum
, const char* client_name
, const char* so_name
, const char* objet_data
, int options
, int* status
, int* int_ref
, int* result
)
282 JackInternalClientLoadRequest
req(refnum
, client_name
, so_name
, objet_data
, options
);
283 JackInternalClientLoadResult res
;
284 ServerSyncCall(&req
, &res
, result
);
285 *int_ref
= res
.fIntRefNum
;
286 *status
= res
.fStatus
;
289 void JackSocketClientChannel::InternalClientUnload(int refnum
, int int_ref
, int* status
, int* result
)
291 JackInternalClientUnloadRequest
req(refnum
, int_ref
);
292 JackInternalClientUnloadResult res
;
293 ServerSyncCall(&req
, &res
, result
);
294 *status
= res
.fStatus
;
297 bool JackSocketClientChannel::Init()
299 jack_log("JackSocketClientChannel::Init");
300 fNotificationSocket
= fNotificationListenSocket
.Accept();
302 fNotificationListenSocket
.Close();
304 if (!fNotificationSocket
) {
305 jack_error("JackSocketClientChannel: cannot establish notication socket");
308 return fClient
->Init();
312 bool JackSocketClientChannel::Execute()
314 JackClientNotification event
;
317 if (event
.Read(fNotificationSocket
) < 0) {
318 fNotificationSocket
->Close();
319 jack_error("JackSocketClientChannel read fail");
323 res
.fResult
= fClient
->ClientNotify(event
.fRefNum
, event
.fName
, event
.fNotify
, event
.fSync
, event
.fMessage
, event
.fValue1
, event
.fValue2
);
326 if (res
.Write(fNotificationSocket
) < 0) {
327 fNotificationSocket
->Close();
328 jack_error("JackSocketClientChannel write fail");
339 } // end of namespace