Jack2  1.9.10
JackInternalClient.cpp
00001 /*
00002 Copyright (C) 2001 Paul Davis
00003 Copyright (C) 2004-2008 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #include "JackSystemDeps.h"
00022 #include "JackServerGlobals.h"
00023 #include "JackGraphManager.h"
00024 #include "JackConstants.h"
00025 #include "JackInternalClient.h"
00026 #include "JackLockedEngine.h"
00027 #include "JackServer.h"
00028 #include "JackEngineControl.h"
00029 #include "JackClientControl.h"
00030 #include "JackInternalClientChannel.h"
00031 #include "JackTools.h"
00032 #include <assert.h>
00033 
00034 namespace Jack
00035 {
00036 
00037 JackGraphManager* JackInternalClient::fGraphManager = NULL;
00038 JackEngineControl* JackInternalClient::fEngineControl = NULL;
00039 
00040 // Used for external C API (JackAPI.cpp)
00041 SERVER_EXPORT JackGraphManager* GetGraphManager()
00042 {
00043     return JackServerGlobals::fInstance->GetGraphManager();
00044 }
00045 
00046 SERVER_EXPORT JackEngineControl* GetEngineControl()
00047 {
00048     return JackServerGlobals::fInstance->GetEngineControl();
00049 }
00050 
00051 SERVER_EXPORT JackSynchro* GetSynchroTable()
00052 {
00053     return JackServerGlobals::fInstance->GetSynchroTable();
00054 }
00055 
00056 JackInternalClient::JackInternalClient(JackServer* server, JackSynchro* table): JackClient(table)
00057 {
00058     fChannel = new JackInternalClientChannel(server);
00059 }
00060 
00061 JackInternalClient::~JackInternalClient()
00062 {
00063     delete fChannel;
00064 }
00065 
00066 int JackInternalClient::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
00067 {
00068     int result;
00069     jack_log("JackInternalClient::Open name = %s", name);
00070     
00071     if (strlen(name) >= JACK_CLIENT_NAME_SIZE) {
00072         jack_error("\"%s\" is too long to be used as a JACK client name.\n"
00073                    "Please use %lu characters or less",
00074                    name,
00075                    JACK_CLIENT_NAME_SIZE - 1);
00076         return -1; 
00077     }
00078 
00079     strncpy(fServerName, server_name, sizeof(fServerName));
00080 
00081     // Open server/client direct channel
00082     char name_res[JACK_CLIENT_NAME_SIZE + 1];
00083     fChannel->ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, false);
00084     if (result < 0) {
00085         int status1 = *status;
00086         if (status1 & JackVersionError) {
00087             jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
00088         } else {
00089             jack_error("Client name = %s conflits with another running client", name);
00090         }
00091         goto error;
00092     }
00093 
00094     strcpy(fClientControl.fName, name_res);
00095 
00096     // Require new client
00097     fChannel->ClientOpen(name_res, &fClientControl.fRefNum, &fEngineControl, &fGraphManager, this, &result);
00098     if (result < 0) {
00099         jack_error("Cannot open client name = %s", name_res);
00100         goto error;
00101     }
00102 
00103     SetupDriverSync(false);
00104     JackGlobals::fClientTable[fClientControl.fRefNum] = this;
00105     JackGlobals::fServerRunning = true;
00106     jack_log("JackInternalClient::Open name = %s refnum = %ld", name_res, fClientControl.fRefNum);
00107     return 0;
00108 
00109 error:
00110     fChannel->Close();
00111     return -1;
00112 }
00113 
00114 void JackInternalClient::ShutDown(jack_status_t code, const char* message)
00115 {
00116     jack_log("JackInternalClient::ShutDown");
00117     JackClient::ShutDown(code, message);
00118 }
00119 
00120 JackGraphManager* JackInternalClient::GetGraphManager() const
00121 {
00122     assert(fGraphManager);
00123     return fGraphManager;
00124 }
00125 
00126 JackEngineControl* JackInternalClient::GetEngineControl() const
00127 {
00128     assert(fEngineControl);
00129     return fEngineControl;
00130 }
00131 
00132 JackClientControl* JackInternalClient::GetClientControl() const
00133 {
00134     return const_cast<JackClientControl*>(&fClientControl);
00135 }
00136 
00137 int JackLoadableInternalClient::Init(const char* so_name)
00138 {
00139     char path_to_so[JACK_PATH_MAX + 1];
00140     BuildClientPath(path_to_so, sizeof(path_to_so), so_name);
00141 
00142     fHandle = LoadJackModule(path_to_so);
00143     jack_log("JackLoadableInternalClient::JackLoadableInternalClient path_to_so = %s", path_to_so);
00144 
00145     if (fHandle == NULL) {
00146         PrintLoadError(so_name);
00147         return -1;
00148     }
00149 
00150     fFinish = (FinishCallback)GetJackProc(fHandle, "jack_finish");
00151     if (fFinish == NULL) {
00152         UnloadJackModule(fHandle);
00153         jack_error("symbol jack_finish cannot be found in %s", so_name);
00154         return -1;
00155     }
00156 
00157     fDescriptor = (JackDriverDescFunction)GetJackProc(fHandle, "jack_get_descriptor");
00158     if (fDescriptor == NULL) {
00159         jack_info("No jack_get_descriptor entry-point for %s", so_name);
00160     }
00161     return 0;
00162 }
00163 
00164 int JackLoadableInternalClient1::Init(const char* so_name)
00165 {
00166     if (JackLoadableInternalClient::Init(so_name) < 0) {
00167         return -1;
00168     }
00169 
00170     fInitialize = (InitializeCallback)GetJackProc(fHandle, "jack_initialize");
00171     if (fInitialize == NULL) {
00172         UnloadJackModule(fHandle);
00173         jack_error("symbol jack_initialize cannot be found in %s", so_name);
00174         return -1;
00175     }
00176 
00177     return 0;
00178 }
00179 
00180 int JackLoadableInternalClient2::Init(const char* so_name)
00181 {
00182     if (JackLoadableInternalClient::Init(so_name) < 0) {
00183         return -1;
00184     }
00185 
00186     fInitialize = (InternalInitializeCallback)GetJackProc(fHandle, "jack_internal_initialize");
00187     if (fInitialize == NULL) {
00188         UnloadJackModule(fHandle);
00189         jack_error("symbol jack_internal_initialize cannot be found in %s", so_name);
00190         return -1;
00191     }
00192 
00193     return 0;
00194 }
00195 
00196 JackLoadableInternalClient1::JackLoadableInternalClient1(JackServer* server, JackSynchro* table, const char* object_data)
00197         : JackLoadableInternalClient(server, table)
00198 {
00199     strncpy(fObjectData, object_data, JACK_LOAD_INIT_LIMIT);
00200 }
00201 
00202 JackLoadableInternalClient2::JackLoadableInternalClient2(JackServer* server, JackSynchro* table, const JSList*  parameters)
00203         : JackLoadableInternalClient(server, table)
00204 {
00205     fParameters = parameters;
00206 }
00207 
00208 JackLoadableInternalClient::~JackLoadableInternalClient()
00209 {
00210     if (fFinish != NULL) {
00211         fFinish(fProcessArg);
00212     }
00213     if (fHandle != NULL) {
00214         UnloadJackModule(fHandle);
00215     }
00216 }
00217 
00218 int JackLoadableInternalClient1::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
00219 {
00220     int res = -1;
00221 
00222     if (JackInternalClient::Open(server_name, name, uuid, options, status) == 0) {
00223         if (fInitialize((jack_client_t*)this, fObjectData) == 0) {
00224             res = 0;
00225         } else {
00226             JackInternalClient::Close();
00227             fFinish = NULL;
00228         }
00229     }
00230 
00231     return res;
00232 }
00233 
00234 int JackLoadableInternalClient2::Open(const char* server_name, const char* name, int uuid, jack_options_t options, jack_status_t* status)
00235 {
00236     int res = -1;
00237 
00238     if (JackInternalClient::Open(server_name, name, uuid, options, status) == 0) {
00239         if (fInitialize((jack_client_t*)this, fParameters) == 0) {
00240             res = 0;
00241         } else {
00242             JackInternalClient::Close();
00243             fFinish = NULL;
00244         }
00245     }
00246 
00247     return res;
00248 }
00249 
00250 } // end of namespace
00251