Jack2  1.9.9
JackLibClient.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #include "JackLibClient.h"
21 #include "JackTime.h"
22 #include "JackLibGlobals.h"
23 #include "JackGlobals.h"
24 #include "JackPlatformPlug.h"
25 #include "JackTools.h"
26 
27 namespace Jack
28 {
29 
30 // Used for external C API (JackAPI.cpp)
31 JackGraphManager* GetGraphManager()
32 {
33  if (JackLibGlobals::fGlobals) {
34  return JackLibGlobals::fGlobals->fGraphManager;
35  } else {
36  return NULL;
37  }
38 }
39 
40 JackEngineControl* GetEngineControl()
41 {
42  if (JackLibGlobals::fGlobals) {
43  return JackLibGlobals::fGlobals->fEngineControl;
44  } else {
45  return NULL;
46  }
47 }
48 
49 JackSynchro* GetSynchroTable()
50 {
51  return (JackLibGlobals::fGlobals ? JackLibGlobals::fGlobals->fSynchroTable : 0);
52 }
53 
54 //-------------------
55 // Client management
56 //-------------------
57 
58 /*
59 ShutDown is called:
60 - from the RT thread when Execute method fails
61 - possibly from a "closed" notification channel
62 (Not needed since the synch object used (Sema of Fifo will fails when server quits... see ShutDown))
63 */
64 
65 void JackLibClient::ShutDown()
66 {
67  jack_log("JackLibClient::ShutDown");
68  JackGlobals::fServerRunning = false;
69  JackClient::ShutDown();
70 }
71 
73 {
74  jack_log("JackLibClient::JackLibClient table = %x", table);
76 }
77 
78 JackLibClient::~JackLibClient()
79 {
80  jack_log("JackLibClient::~JackLibClient");
81  delete fChannel;
82 }
83 
84 int JackLibClient::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
85 {
86  int shared_engine, shared_client, shared_graph, result;
87  jack_log("JackLibClient::Open name = %s", name);
88 
89  strncpy(fServerName, server_name, sizeof(fServerName));
90 
91  // Open server/client channel
92  char name_res[JACK_CLIENT_NAME_SIZE + 1];
93  if (fChannel->Open(server_name, name, uuid, name_res, this, options, status) < 0) {
94  jack_error("Cannot connect to the server");
95  goto error;
96  }
97 
98  // Start receiving notifications
99  if (fChannel->Start() < 0) {
100  jack_error("Cannot start channel");
101  goto error;
102  }
103 
104  // Require new client
105  fChannel->ClientOpen(name_res, JackTools::GetPID(), uuid, &shared_engine, &shared_client, &shared_graph, &result);
106  if (result < 0) {
107  jack_error("Cannot open %s client", name_res);
108  goto error;
109  }
110 
111  try {
112  // Map shared memory segments
113  JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
114  JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
115  fClientControl.SetShmIndex(shared_client, fServerName);
116  JackGlobals::fVerbose = GetEngineControl()->fVerbose;
117  } catch (...) {
118  jack_error("Map shared memory segments exception");
119  goto error;
120  }
121 
122  SetupDriverSync(false);
123 
124  // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
125  if (!fSynchroTable[GetClientControl()->fRefNum].Connect(name_res, fServerName)) {
126  jack_error("Cannot ConnectSemaphore %s client", name_res);
127  goto error;
128  }
129 
130  JackGlobals::fClientTable[GetClientControl()->fRefNum] = this;
131  JackGlobals::fServerRunning = true;
132  SetClockSource(GetEngineControl()->fClockSource);
133  jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
134  return 0;
135 
136 error:
137  fChannel->Stop();
138  fChannel->Close();
139  return -1;
140 }
141 
142 // Notifications received from the server
143 // TODO this should be done once for all clients in the process, when a shared notification channel
144 // will be shared by all clients...
145 int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
146 {
147  int res = 0;
148 
149  // Done all time
150  switch (notify) {
151 
152  case kAddClient:
153  jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
154  // the synchro must be usable in I/O mode when several clients live in the same process
155  res = fSynchroTable[refnum].Connect(name, fServerName) ? 0 : -1;
156  break;
157 
158  case kRemoveClient:
159  jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
160  if (GetClientControl() && strcmp(GetClientControl()->fName, name) != 0)
161  res = fSynchroTable[refnum].Disconnect() ? 0 : -1;
162  break;
163  }
164 
165  return res;
166 }
167 
168 JackGraphManager* JackLibClient::GetGraphManager() const
169 {
170  assert(JackLibGlobals::fGlobals->fGraphManager);
171  return JackLibGlobals::fGlobals->fGraphManager;
172 }
173 
174 JackEngineControl* JackLibClient::GetEngineControl() const
175 {
176  assert(JackLibGlobals::fGlobals->fEngineControl);
177  return JackLibGlobals::fGlobals->fEngineControl;
178 }
179 
180 JackClientControl* JackLibClient::GetClientControl() const
181 {
182  return fClientControl;
183 }
184 
185 } // end of namespace
186 
187 
188 
Inter process synchronization using using Mach semaphore.
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
Graph manager: contains the connection manager and the port array.
void SetupDriverSync(bool freewheel)
Definition: JackClient.cpp:140
JackClientChannel using sockets.
JackLibClient(JackSynchro *table)
int ClientNotifyImp(int refnum, const char *name, int notify, int sync, const char *message, int value1, int value2)
Notification received from the server.
detail::JackClientChannelInterface * fChannel
Definition: JackClient.h:93
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107
The base class for clients: share part of the implementation for JackInternalClient and JackLibClient...
Definition: JackClient.h:47