JackSocketClientChannel.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 Lesser General Public License as published by
00006   the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00013   
00014   You should have received a copy of the GNU Lesser General Public License
00015   along with this program; if not, write to the Free Software 
00016   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #include "JackSocketClientChannel.h"
00021 #include "JackRequest.h"
00022 #include "JackClient.h"
00023 #include "JackGlobals.h"
00024 
00025 namespace Jack
00026 {
00027 
00028 JackSocketClientChannel::JackSocketClientChannel()
00029 {
00030     fThread = JackGlobals::MakeThread(this);
00031     fNotificationSocket = NULL;
00032     fClient = NULL;
00033 }
00034 
00035 JackSocketClientChannel::~JackSocketClientChannel()
00036 {
00037     delete fThread;
00038     delete fNotificationSocket;
00039 }
00040 
00041 int JackSocketClientChannel::Open(const char* name, JackClient* obj)
00042 {
00043     JackLog("JackSocketClientChannel::Open name = %s\n", name);
00044 
00045     if (fRequestSocket.Connect(jack_server_dir, 0) < 0) {
00046         jack_error("Cannot connect to server socket");
00047         goto error;
00048     }
00049 
00050     if (fNotificationListenSocket.Bind(jack_client_dir, name, 0) < 0) {
00051         jack_error("Cannot bind socket");
00052         goto error;
00053     }
00054 
00055     fClient = obj;
00056     return 0;
00057 
00058 error:
00059     fRequestSocket.Close();
00060     fNotificationListenSocket.Close();
00061     return -1;
00062 }
00063 
00064 void JackSocketClientChannel::Close()
00065 {
00066     fRequestSocket.Close();
00067     fNotificationListenSocket.Close();
00068     if (fNotificationSocket)
00069         fNotificationSocket->Close();
00070 }
00071 
00072 int JackSocketClientChannel::Start()
00073 {
00074     JackLog("JackSocketClientChannel::Start\n");
00075     if (fThread->Start() != 0) {
00076         jack_error("Cannot start Jack client listener");
00077         return -1;
00078     } else {
00079         return 0;
00080     }
00081 }
00082 
00083 void JackSocketClientChannel::Stop()
00084 {
00085     JackLog("JackSocketClientChannel::Stop\n");
00086     fThread->Kill();
00087 }
00088 
00089 void JackSocketClientChannel::ServerSyncCall(JackRequest* req, JackResult* res, int* result)
00090 {
00091     if (req->Write(&fRequestSocket) < 0) {
00092         jack_error("Could not write request type = %ld", req->fType);
00093         *result = -1;
00094         return ;
00095     }
00096 
00097     if (res->Read(&fRequestSocket) < 0) {
00098         jack_error("Could not read result type = %ld", req->fType);
00099         *result = -1;
00100         return ;
00101     }
00102 
00103     *result = res->fResult;
00104 }
00105 
00106 void JackSocketClientChannel::ServerAsyncCall(JackRequest* req, JackResult* res, int* result)
00107 {
00108     if (req->Write(&fRequestSocket) < 0) {
00109         jack_error("Could not write request type = %ld", req->fType);
00110         *result = -1;
00111     } else {
00112         *result = 0;
00113     }
00114 }
00115 
00116 void JackSocketClientChannel::ClientNew(const char* name, int* shared_engine, int* shared_client, int* shared_graph, int* result)
00117 {
00118     JackClientNewRequest req(name);
00119     JackClientNewResult res;
00120     ServerSyncCall(&req, &res, result);
00121     *shared_engine = res.fSharedEngine;
00122     *shared_client = res.fSharedClient;
00123     *shared_graph = res.fSharedGraph;
00124 }
00125 
00126 void JackSocketClientChannel::ClientClose(int refnum, int* result)
00127 {
00128     JackClientCloseRequest req(refnum);
00129     JackResult res;
00130     ServerAsyncCall(&req, &res, result);
00131 }
00132 
00133 void JackSocketClientChannel::ClientActivate(int refnum, int* result)
00134 {
00135     JackActivateRequest req(refnum);
00136     JackResult res;
00137     ServerSyncCall(&req, &res, result);
00138 }
00139 
00140 void JackSocketClientChannel::ClientDeactivate(int refnum, int* result)
00141 {
00142     JackDeactivateRequest req(refnum);
00143     JackResult res;
00144     ServerSyncCall(&req, &res, result);
00145 }
00146 
00147 void JackSocketClientChannel::PortRegister(int refnum, const char* name, unsigned int flags, unsigned int buffer_size, jack_port_id_t* port_index, int* result)
00148 {
00149     JackPortRegisterRequest req(refnum, name, "audio", flags, buffer_size);
00150     JackPortRegisterResult res;
00151     ServerSyncCall(&req, &res, result);
00152     *port_index = res.fPortIndex;
00153 }
00154 
00155 void JackSocketClientChannel::PortUnRegister(int refnum, jack_port_id_t port_index, int* result)
00156 {
00157     JackPortUnRegisterRequest req(refnum, port_index);
00158     JackResult res;
00159     ServerSyncCall(&req, &res, result);
00160 }
00161 
00162 void JackSocketClientChannel::PortConnect(int refnum, const char* src, const char* dst, int* result)
00163 {
00164     JackPortConnectNameRequest req(refnum, src, dst);
00165     JackResult res;
00166     ServerSyncCall(&req, &res, result);
00167 }
00168 
00169 void JackSocketClientChannel::PortDisconnect(int refnum, const char* src, const char* dst, int* result)
00170 {
00171     JackPortDisconnectNameRequest req(refnum, src, dst);
00172     JackResult res;
00173     ServerSyncCall(&req, &res, result);
00174 }
00175 
00176 void JackSocketClientChannel::PortConnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
00177 {
00178     JackPortConnectRequest req(refnum, src, dst);
00179     JackResult res;
00180     ServerSyncCall(&req, &res, result);
00181 }
00182 
00183 void JackSocketClientChannel::PortDisconnect(int refnum, jack_port_id_t src, jack_port_id_t dst, int* result)
00184 {
00185     JackPortDisconnectRequest req(refnum, src, dst);
00186     JackResult res;
00187     ServerSyncCall(&req, &res, result);
00188 }
00189 
00190 void JackSocketClientChannel::SetBufferSize(jack_nframes_t buffer_size, int* result)
00191 {
00192     JackSetBufferSizeRequest req(buffer_size);
00193     JackResult res;
00194     ServerSyncCall(&req, &res, result);
00195 }
00196 
00197 void JackSocketClientChannel::SetFreewheel(int onoff, int* result)
00198 {
00199     JackSetFreeWheelRequest req(onoff);
00200     JackResult res;
00201     ServerSyncCall(&req, &res, result);
00202 }
00203 
00204 void JackSocketClientChannel::ReleaseTimebase(int refnum, int* result)
00205 {
00206     JackReleaseTimebaseRequest req(refnum);
00207     JackResult res;
00208     ServerSyncCall(&req, &res, result);
00209 }
00210 
00211 void JackSocketClientChannel::SetTimebaseCallback(int refnum, int conditional, int* result)
00212 {
00213     JackSetTimebaseCallbackRequest req(refnum, conditional);
00214     JackResult res;
00215     ServerSyncCall(&req, &res, result);
00216 }
00217 
00218 bool JackSocketClientChannel::Init()
00219 {
00220     JackLog("JackSocketClientChannel::Init \n");
00221     fNotificationSocket = fNotificationListenSocket.Accept();
00222     // No more needed
00223     fNotificationListenSocket.Close();
00224 
00225     if (!fNotificationSocket) {
00226         jack_error("JackSocketClientChannel: cannot establish notication socket");
00227         return false;
00228     } else {
00229         return true;
00230     }
00231 }
00232 
00233 bool JackSocketClientChannel::Execute()
00234 {
00235     JackClientNotification event;
00236     JackResult res;
00237 
00238     if (event.Read(fNotificationSocket) < 0) {
00239         fNotificationSocket->Close();
00240         jack_error("JackSocketClientChannel read fail");
00241         goto error;
00242     }
00243 
00244     res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fValue);
00245 
00246     if (event.fSync) {
00247         if (res.Write(fNotificationSocket) < 0) {
00248             fNotificationSocket->Close();
00249             jack_error("JackSocketClientChannel write fail");
00250             goto error;
00251         }
00252     }
00253     return true;
00254 
00255 error:
00256     fClient->ShutDown();
00257     return false;
00258 }
00259 
00260 } // end of namespace
00261 
00262 
00263 
00264 
00265 

Generated on Wed Jan 10 11:42:46 2007 for Jackdmp by  doxygen 1.4.5