Jack2  1.9.9
JackSocketClientChannel.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 */
19 
20 #include "JackSocketClientChannel.h"
21 #include "JackRequest.h"
22 #include "JackClient.h"
23 #include "JackGlobals.h"
24 #include "JackError.h"
25 
26 namespace Jack
27 {
28 
29 JackSocketClientChannel::JackSocketClientChannel()
30  :JackGenericClientChannel(), fThread(this)
31 {
32  fRequest = new JackClientSocket();
33  fNotificationSocket = NULL;
34 }
35 
36 JackSocketClientChannel::~JackSocketClientChannel()
37 {
38  delete fRequest;
39  delete fNotificationSocket;
40 }
41 
42 int JackSocketClientChannel::Open(const char* server_name, const char* name, int uuid, char* name_res, JackClient* obj, jack_options_t options, jack_status_t* status)
43 {
44  int result = 0;
45  jack_log("JackSocketClientChannel::Open name = %s", name);
46 
47  if (fRequest->Connect(jack_server_dir, server_name, 0) < 0) {
48  jack_error("Cannot connect to server socket");
49  goto error;
50  }
51 
52  // Check name in server
53  ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, true);
54  if (result < 0) {
55  int status1 = *status;
56  if (status1 & JackVersionError) {
57  jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
58  } else {
59  jack_error("Client name = %s conflits with another running client", name);
60  }
61  goto error;
62  }
63 
64  if (fNotificationListenSocket.Bind(jack_client_dir, name_res, 0) < 0) {
65  jack_error("Cannot bind socket");
66  goto error;
67  }
68 
69  fClient = obj;
70  return 0;
71 
72 error:
73  fRequest->Close();
74  fNotificationListenSocket.Close();
75  return -1;
76 }
77 
78 void JackSocketClientChannel::Close()
79 {
80  fRequest->Close();
81  fNotificationListenSocket.Close();
82  if (fNotificationSocket)
83  fNotificationSocket->Close();
84 }
85 
86 int JackSocketClientChannel::Start()
87 {
88  jack_log("JackSocketClientChannel::Start");
89  /*
90  To be sure notification thread is started before ClientOpen is called.
91  */
92  if (fThread.StartSync() != 0) {
93  jack_error("Cannot start Jack client listener");
94  return -1;
95  } else {
96  return 0;
97  }
98 }
99 
100 void JackSocketClientChannel::Stop()
101 {
102  jack_log("JackSocketClientChannel::Stop");
103  fThread.Kill();
104 }
105 
107 {
108  jack_log("JackSocketClientChannel::Init");
109  fNotificationSocket = fNotificationListenSocket.Accept();
110 
111  // No more needed
112  fNotificationListenSocket.Close();
113 
114  // Setup context
115  if (!jack_tls_set(JackGlobals::fNotificationThread, this)) {
116  jack_error("Failed to set thread notification key");
117  }
118 
119  if (!fNotificationSocket) {
120  jack_error("JackSocketClientChannel: cannot establish notication socket");
121  return false;
122  } else {
123  return true;
124  }
125 }
126 
127 bool JackSocketClientChannel::Execute()
128 {
130  JackResult res;
131 
132  if (event.Read(fNotificationSocket) < 0) {
133  jack_error("JackSocketClientChannel read fail");
134  goto error;
135  }
136 
137  res.fResult = fClient->ClientNotify(event.fRefNum, event.fName, event.fNotify, event.fSync, event.fMessage, event.fValue1, event.fValue2);
138 
139  if (event.fSync) {
140  if (res.Write(fNotificationSocket) < 0) {
141  jack_error("JackSocketClientChannel write fail");
142  goto error;
143  }
144  }
145  return true;
146 
147 error:
148  fNotificationSocket->Close();
149  fClient->ShutDown();
150  return false;
151 }
152 
153 } // end of namespace
154 
155 
156 
157 
158 
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:91
Result from the server.
Definition: JackRequest.h:129
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:107
ClientNotification.
Definition: JackRequest.h:1569