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)
00065 {
00066     int shared_engine, shared_client, shared_graph, result;
00067     JackLog("JackLibClient::Open %s\n", name);
00068 
00069     // Open server/client channel
00070     if (fChannel->Open(name, this) < 0) {
00071         jack_error("Cannot connect to the server");
00072         goto error;
00073     }
00074 
00075     // Start receiving notifications
00076     if (fChannel->Start() < 0) {
00077         jack_error("Cannot start channel");
00078         goto error;
00079     }
00080 
00081     // Require new client
00082     fChannel->ClientNew(name, &shared_engine, &shared_client, &shared_graph, &result);
00083     if (result < 0) {
00084         jack_error("Cannot open %s client", name);
00085         goto error;
00086     }
00087 
00088     try {
00089         // Map shared memory segments
00090         JackLibGlobals::fGlobals->fEngineControl = shared_engine;
00091         JackLibGlobals::fGlobals->fGraphManager = shared_graph;
00092         fClientControl = shared_client;
00093         jack_verbose = GetEngineControl()->fVerbose;
00094     } catch (int n) {
00095         jack_error("Map shared memory segments exception %d", n);
00096         goto error;
00097     }
00098 
00099     SetupDriverSync(false);
00100 
00101 /* TODO : solve WIN32 thread Kill issue
00102 #ifndef WIN32
00103     // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process    
00104         if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
00105         jack_error("Cannot ConnectSemaphore %s client", name);
00106         goto error;
00107     }
00108 #endif
00109 */
00110         // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process    
00111         if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
00112         jack_error("Cannot ConnectSemaphore %s client", name);
00113         goto error;
00114     }
00115 
00116     JackLog("JackLibClient::Open name = %s refnum = %ld\n", name, fClientControl->fRefNum);
00117         
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 JackNotifyChannelInterface::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 JackNotifyChannelInterface::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 Wed Jan 10 11:42:45 2007 for Jackdmp by  doxygen 1.4.5