Correct netjack2 components help.
[jack2.git] / common / JackLibClient.cpp
blob72b05642892f7f9a1116f7fd38d41e6c4af6e0c4
1 /*
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 "JackLibClient.h"
21 #include "JackTime.h"
22 #include "JackLibGlobals.h"
23 #include "JackGlobals.h"
24 #include "JackPlatformPlug.h"
25 #include "JackTools.h"
27 namespace Jack
30 // Used for external C API (JackAPI.cpp)
31 JackGraphManager* GetGraphManager()
33 if (JackLibGlobals::fGlobals) {
34 return JackLibGlobals::fGlobals->fGraphManager;
35 } else {
36 return NULL;
40 JackEngineControl* GetEngineControl()
42 if (JackLibGlobals::fGlobals) {
43 return JackLibGlobals::fGlobals->fEngineControl;
44 } else {
45 return NULL;
49 JackSynchro* GetSynchroTable()
51 return (JackLibGlobals::fGlobals ? JackLibGlobals::fGlobals->fSynchroTable : 0);
54 //-------------------
55 // Client management
56 //-------------------
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))
65 void JackLibClient::ShutDown()
67 jack_log("JackLibClient::ShutDown");
68 JackGlobals::fServerRunning = false;
69 JackClient::ShutDown();
72 JackLibClient::JackLibClient(JackSynchro* table): JackClient(table)
74 jack_log("JackLibClient::JackLibClient table = %x", table);
75 fChannel = new JackClientChannel();
78 JackLibClient::~JackLibClient()
80 jack_log("JackLibClient::~JackLibClient");
81 delete fChannel;
84 int JackLibClient::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
86 int shared_engine, shared_client, shared_graph, result;
87 jack_log("JackLibClient::Open name = %s", name);
89 strncpy(fServerName, server_name, sizeof(fServerName));
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;
98 // Start receiving notifications
99 if (fChannel->Start() < 0) {
100 jack_error("Cannot start channel");
101 goto error;
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;
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;
122 SetupDriverSync(false);
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;
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;
136 error:
137 fChannel->Stop();
138 fChannel->Close();
139 return -1;
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)
147 int res = 0;
149 // Done all time
150 switch (notify) {
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;
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;
165 return res;
168 JackGraphManager* JackLibClient::GetGraphManager() const
170 assert(JackLibGlobals::fGlobals->fGraphManager);
171 return JackLibGlobals::fGlobals->fGraphManager;
174 JackEngineControl* JackLibClient::GetEngineControl() const
176 assert(JackLibGlobals::fGlobals->fEngineControl);
177 return JackLibGlobals::fGlobals->fEngineControl;
180 JackClientControl* JackLibClient::GetClientControl() const
182 return fClientControl;
185 } // end of namespace