Jack2 1.9.6

JackExternalClient.cpp

00001 /*
00002   Copyright (C) 2001-2003 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 "JackExternalClient.h"
00022 #include "JackClientControl.h"
00023 #include "JackGlobals.h"
00024 #include "JackChannel.h"
00025 #include "JackError.h"
00026 
00027 namespace Jack
00028 {
00029 
00030 JackExternalClient::JackExternalClient(): fClientControl(NULL)
00031 {}
00032 
00033 JackExternalClient::~JackExternalClient()
00034 {}
00035 
00036 int JackExternalClient::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
00037 {
00038     int result = -1;
00039     jack_log("JackExternalClient::ClientNotify ref = %ld name = %s notify = %ld", refnum, name, notify);
00040     fChannel.ClientNotify(refnum, name, notify, sync, message, value1, value2, &result);
00041     return result;
00042 }
00043 
00044 int JackExternalClient::Open(const char* name, int pid, int refnum, int* shared_client)
00045 {
00046     try {
00047 
00048         if (fChannel.Open(name) < 0) {
00049             jack_error("Cannot connect to client name = %s\n", name);
00050             return -1;
00051         }
00052         
00053         // Use "placement new" to allocate object in shared memory
00054         JackShmMemAble* shared_mem = static_cast<JackShmMemAble*>(JackShmMem::operator new(sizeof(JackClientControl)));
00055         shared_mem->Init();
00056         fClientControl = new(shared_mem) JackClientControl(name, pid, refnum);
00057       
00058         if (!fClientControl) {
00059             jack_error("Cannot allocate client shared memory segment");
00060             return -1;
00061         }
00062        
00063         *shared_client = shared_mem->GetShmIndex();
00064         jack_log("JackExternalClient::Open name = %s index = %ld base = %x", name, shared_mem->GetShmIndex(), shared_mem->GetShmAddress());
00065         return 0;
00066 
00067     } catch (std::exception e) {
00068         return -1;
00069     }
00070 }
00071 
00072 int JackExternalClient::Close()
00073 {
00074     fChannel.Close();
00075     if (fClientControl) {
00076         fClientControl->~JackClientControl();
00077         JackShmMem::operator delete(fClientControl);
00078     }
00079     return 0;
00080 }
00081 
00082 JackClientControl* JackExternalClient::GetClientControl() const
00083 {
00084     return fClientControl;
00085 }
00086 
00087 } // end of namespace