Jack2  1.9.8
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 
59 {
60  jack_log("JackLibClient::JackLibClient table = %x", table);
62 }
63 
64 JackLibClient::~JackLibClient()
65 {
66  jack_log("JackLibClient::~JackLibClient");
67  delete fChannel;
68 }
69 
70 int JackLibClient::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
71 {
72  int shared_engine, shared_client, shared_graph, result;
73  jack_log("JackLibClient::Open name = %s", name);
74 
75  strncpy(fServerName, server_name, sizeof(fServerName));
76 
77  // Open server/client channel
78  char name_res[JACK_CLIENT_NAME_SIZE + 1];
79  if (fChannel->Open(server_name, name, uuid, name_res, this, options, status) < 0) {
80  jack_error("Cannot connect to the server");
81  goto error;
82  }
83 
84  // Start receiving notifications
85  if (fChannel->Start() < 0) {
86  jack_error("Cannot start channel");
87  goto error;
88  }
89 
90  // Require new client
91  fChannel->ClientOpen(name_res, JackTools::GetPID(), uuid, &shared_engine, &shared_client, &shared_graph, &result);
92  if (result < 0) {
93  jack_error("Cannot open %s client", name_res);
94  goto error;
95  }
96 
97  try {
98  // Map shared memory segments
99  JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
100  JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
101  fClientControl.SetShmIndex(shared_client, fServerName);
102  JackGlobals::fVerbose = GetEngineControl()->fVerbose;
103  } catch (...) {
104  jack_error("Map shared memory segments exception");
105  goto error;
106  }
107 
108  SetupDriverSync(false);
109 
110  // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
111  if (!fSynchroTable[GetClientControl()->fRefNum].Connect(name_res, fServerName)) {
112  jack_error("Cannot ConnectSemaphore %s client", name_res);
113  goto error;
114  }
115 
116  JackGlobals::fClientTable[GetClientControl()->fRefNum] = this;
117  JackGlobals::fServerRunning = true;
118  SetClockSource(GetEngineControl()->fClockSource);
119  jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
120  return 0;
121 
122 error:
123  fChannel->Stop();
124  fChannel->Close();
125  return -1;
126 }
127 
128 // Notifications received from the server
129 // TODO this should be done once for all clients in the process, when a shared notification channel
130 // will be shared by all clients...
131 int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
132 {
133  int res = 0;
134 
135  // Done all time
136  switch (notify) {
137 
138  case kAddClient:
139  jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
140  // the synchro must be usable in I/O mode when several clients live in the same process
141  res = fSynchroTable[refnum].Connect(name, fServerName) ? 0 : -1;
142  break;
143 
144  case kRemoveClient:
145  jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
146  if (GetClientControl() && strcmp(GetClientControl()->fName, name) != 0)
147  res = fSynchroTable[refnum].Disconnect() ? 0 : -1;
148  break;
149  }
150 
151  return res;
152 }
153 
154 JackGraphManager* JackLibClient::GetGraphManager() const
155 {
156  assert(JackLibGlobals::fGlobals->fGraphManager);
157  return JackLibGlobals::fGlobals->fGraphManager;
158 }
159 
160 JackEngineControl* JackLibClient::GetEngineControl() const
161 {
162  assert(JackLibGlobals::fGlobals->fEngineControl);
163  return JackLibGlobals::fGlobals->fEngineControl;
164 }
165 
166 JackClientControl* JackLibClient::GetClientControl() const
167 {
168  return fClientControl;
169 }
170 
171 } // end of namespace
172 
173 
174