JackLibClient.cpp

00001 /*
00002 Copyright (C) 2004-2006 Grame  
00003 
00004 This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008 
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details.
00013 
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include "JackLibClient.h"
00021 #include "JackTime.h"
00022 #include "JackLibGlobals.h"
00023 #include "JackGlobals.h"
00024 #include "JackChannel.h"
00025 
00026 namespace Jack
00027 {
00028 
00029 // Used for external C API (JackAPI.cpp)
00030 JackGraphManager* GetGraphManager()
00031 {
00032     assert(JackLibGlobals::fGlobals->fGraphManager);
00033     return JackLibGlobals::fGlobals->fGraphManager;
00034 }
00035 
00036 JackEngineControl* GetEngineControl()
00037 {
00038     assert(JackLibGlobals::fGlobals->fEngineControl);
00039     return JackLibGlobals::fGlobals->fEngineControl;
00040 }
00041 
00042 JackSynchro** GetSynchroTable()
00043 {
00044     assert(JackLibGlobals::fGlobals);
00045     return JackLibGlobals::fGlobals->fSynchroTable;
00046 }
00047 
00048 //-------------------
00049 // Client management
00050 //-------------------
00051 
00052 JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
00053 {
00054     JackLog("JackLibClient::JackLibClient table = %x\n", table);
00055     fChannel = JackGlobals::MakeClientChannel();
00056 }
00057 
00058 JackLibClient::~JackLibClient()
00059 {
00060     JackLog("JackLibClient::~JackLibClient\n");
00061     delete fChannel;
00062 }
00063 
00064 int JackLibClient::Open(const char* name, jack_options_t options, jack_status_t* status)
00065 {
00066     int shared_engine, shared_client, shared_graph, result;
00067     JackLog("JackLibClient::Open %s\n", name);
00068         
00069     // Open server/client channel
00070         char name_res[JACK_CLIENT_NAME_SIZE]; 
00071     if (fChannel->Open(name, name_res, this, options, status) < 0) {
00072         jack_error("Cannot connect to the server");
00073         goto error;
00074     }
00075 
00076     // Start receiving notifications
00077     if (fChannel->Start() < 0) {
00078         jack_error("Cannot start channel");
00079         goto error;
00080     }
00081 
00082     // Require new client
00083     fChannel->ClientOpen(name_res, &shared_engine, &shared_client, &shared_graph, &result);
00084     if (result < 0) {
00085         jack_error("Cannot open %s client", name_res);
00086         goto error;
00087     }
00088 
00089     try {
00090         // Map shared memory segments
00091         JackLibGlobals::fGlobals->fEngineControl = shared_engine;
00092         JackLibGlobals::fGlobals->fGraphManager = shared_graph;
00093         fClientControl = shared_client;
00094         jack_verbose = GetEngineControl()->fVerbose;
00095     } catch (int n) {
00096         jack_error("Map shared memory segments exception %d", n);
00097         goto error;
00098     }
00099 
00100     SetupDriverSync(false);
00101 
00102 /* TODO : solve WIN32 thread Kill issue
00103 #ifndef WIN32
00104     // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process    
00105         if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
00106         jack_error("Cannot ConnectSemaphore %s client", name);
00107         goto error;
00108     }
00109 #endif
00110 */
00111         // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process    
00112         if (!fSynchroTable[fClientControl->fRefNum]->Connect(name_res)) {
00113         jack_error("Cannot ConnectSemaphore %s client", name_res);
00114         goto error;
00115     }
00116 
00117     JackLog("JackLibClient::Open name = %s refnum = %ld\n", name_res, fClientControl->fRefNum);
00118     return 0;
00119 
00120 error:
00121     fChannel->Stop();
00122     fChannel->Close();
00123     return -1;
00124 }
00125 
00126 // Notifications received from the server
00127 // TODO this should be done once for all clients in the process, when a shared notification channel
00128 // will be shared by all clients...
00129 int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
00130 {
00131     int res = 0;
00132 
00133     // Done all time
00134     switch (notify) {
00135 
00136         case kAddClient:
00137             JackLog("JackClient::AddClient name = %s, ref = %ld \n", name, refnum);
00138             // the synchro must be usable in I/O mode when several clients live in the same process
00139             res = fSynchroTable[refnum]->Connect(name) ? 0 : -1;
00140             break;
00141 
00142         case kRemoveClient:
00143             JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", name, refnum);
00144             if (strcmp(GetClientControl()->fName, name) != 0)
00145                 res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
00146             break;
00147         }
00148 
00149     return res;
00150 }
00151 
00152 JackGraphManager* JackLibClient::GetGraphManager() const
00153 {
00154     assert(JackLibGlobals::fGlobals->fGraphManager);
00155     return JackLibGlobals::fGlobals->fGraphManager;
00156 }
00157 
00158 JackEngineControl* JackLibClient::GetEngineControl() const
00159 {
00160     assert(JackLibGlobals::fGlobals->fEngineControl);
00161     return JackLibGlobals::fGlobals->fEngineControl;
00162 }
00163 
00164 JackClientControl* JackLibClient::GetClientControl() const
00165 {
00166     return fClientControl;
00167 }
00168 
00169 } // end of namespace
00170 
00171 
00172 

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1