FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/SessionClientEventService.cpp at master · UXScripts/rstudio · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
UXScripts
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionClientEventService.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
351 lines (298 loc) · 10.5 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionClientEventService.cpp
Copy path
File metadata and controls
351 lines (298 loc) · 10.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* SessionClientEventService.cpp
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
"
SessionClientEventService.hpp
"
#
include
<
algorithm
>
#
include
<
boost/function.hpp
>
#
include
<
core/BoostThread.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
core/Error.hpp
>
#
include
<
core/BoostErrors.hpp
>
#
include
<
core/Thread.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
core/http/Request.hpp
>
#
include
<
session/SessionOptions.hpp
>
#
include
<
session/SessionHttpConnectionListener.hpp
>
#
include
"
SessionClientEventQueue.hpp
"
using
namespace
core
;
namespace
session
{
namespace
{
const
int
kLastChanceWaitSeconds
=
4
;
bool
hasEventIdLessThanOrEqualTo
(
const
json::Value& event,
int
targetId)
{
const
json::Object& eventJSON = event.
get_obj
();
int
eventId = eventJSON.
find
(
"
id
"
)->
second
.
get_int
();
return
eventId <= targetId;
}
}
//
anonymous namespace
ClientEventService&
clientEventService
()
{
static
ClientEventService instance ;
return
instance ;
}
Error
ClientEventService::start
(
const
std::string& clientId)
{
//
set our clientid
setClientId
(clientId,
false
);
//
block all signals for launch of background thread (will cause it
//
to never receive signals)
core::system::SignalBlocker signalBlocker;
Error error = signalBlocker.
blockAll
();
if
(error)
return
error ;
//
launch the service thread
try
{
using
boost::bind;
boost::thread
serviceThread
(
bind
(&ClientEventService::run,
this
));
serviceThread_ = serviceThread.
move
();
return
Success
();
}
catch
(
const
boost::thread_resource_error& e)
{
return
Error
(
boost::thread_error::ec_from_exception
(e),
ERROR_LOCATION
);
}
}
void
ClientEventService::stop
()
{
try
{
if
(serviceThread_.
joinable
())
{
serviceThread_.
interrupt
();
//
wait for for the service thread to stop
if
(!serviceThread_.
timed_join
(
boost::posix_time::seconds
(
kLastChanceWaitSeconds
+
1
)))
{
LOG_WARNING_MESSAGE
(
"
ClientEventService didn't stop on its own
"
);
}
serviceThread_.
detach
();
}
}
catch
(
const
boost::thread_interrupted& e)
{
//
the main thread is the one who calls stop() and it should
//
NEVER be interrupted for any reason
LOG_WARNING_MESSAGE
(
"
thread interrupted during stop
"
);
}
}
void
ClientEventService::setClientId
(
const
std::string& clientId,
bool
clearEvents)
{
LOCK_MUTEX
(mutex_)
{
clientId_ = clientId.
c_str
();
//
avoid ref count
if
(clearEvents)
clientEvents_.
clear
();
}
END_LOCK_MUTEX
if
(clearEvents)
clientEventQueue
().
clear
();
}
std::string
ClientEventService::clientId
()
{
LOCK_MUTEX
(mutex_)
{
return
std::string
(clientId_.
c_str
()) ;
//
avoid ref-count
}
END_LOCK_MUTEX
//
keep compiler happy
return
std::string
();
}
void
ClientEventService::erasePreviouslyDeliveredEvents
(
int
lastClientEventIdSeen)
{
LOCK_MUTEX
(mutex_)
{
clientEvents_.
erase
(
std::remove_if
(clientEvents_.
begin
(),
clientEvents_.
end
(),
boost::bind
(hasEventIdLessThanOrEqualTo,
_1,
lastClientEventIdSeen)),
clientEvents_.
end
());
}
END_LOCK_MUTEX
}
bool
ClientEventService::havePendingClientEvents
()
{
LOCK_MUTEX
(mutex_)
{
return
!clientEvents_.
empty
();
}
END_LOCK_MUTEX
//
keep compiler happy
return
false
;
}
void
ClientEventService::addClientEvent
(
const
json::Object& eventObject)
{
LOCK_MUTEX
(mutex_)
{
clientEvents_.
push_back
(eventObject);
}
END_LOCK_MUTEX
}
void
ClientEventService::setClientEventResult
(
core::json::JsonRpcResponse* pResponse)
{
LOCK_MUTEX
(mutex_)
{
pResponse->
setResult
(clientEvents_);
}
END_LOCK_MUTEX
}
void
ClientEventService::run
()
{
try
{
//
default time durations
using
namespace
boost
::posix_time
;
time_duration maxRequestSec =
seconds
(
50
);
time_duration batchDelay =
milliseconds
(
20
);
time_duration maxTotalBatchDelay =
seconds
(
2
);
//
make much shorter for desktop mode
if
(
session::options
().
programMode
() ==
kSessionProgramModeDesktop
)
{
batchDelay =
milliseconds
(
2
);
maxTotalBatchDelay =
milliseconds
(
10
);
}
//
get alias to client event queue
ClientEventQueue& clientEventQueue =
session::clientEventQueue
();
//
initialize state
int
nextEventId =
0
;
//
accept loop
bool
stopServer =
false
;
while
(!stopServer || clientEventQueue.
hasEvents
())
{
boost::shared_ptr<HttpConnection> ptrConnection ;
try
{
//
wait for up to 1 second for a connection
long
secondsToWait = stopServer ?
kLastChanceWaitSeconds
:
1
;
ptrConnection =
httpConnectionListener
().
eventsConnectionQueue
().
dequeConnection
(
boost::posix_time::seconds
(secondsToWait));
//
if we didn't get one then check for interruption requested
//
and then continue waiting
if
(!ptrConnection)
{
if
(stopServer)
{
//
This was our last chance. There are still some events
//
left in the queue, but we waited and nobody came.
break
;
}
//
check for interruption and set stopServer flag if we were
if
(
boost::this_thread::interruption_requested
())
throw
boost::thread_interrupted
();
//
accept next request (assuming we weren't interrupted)
continue
;
}
}
catch
(
const
boost::thread_interrupted&)
{
stopServer =
true
;
continue
;
}
//
parse the json rpc request
json::JsonRpcRequest request;
Error error =
json::parseJsonRpcRequest
(ptrConnection->
request
().
body
(),
&request);
if
(error)
{
ptrConnection->
sendJsonRpcError
(error);
continue
;
}
//
send an error back if this request came from the wrong client
if
(request.
clientId
!=
clientId
())
{
Error error =
Error
(json::errc::InvalidClientId,
ERROR_LOCATION
);
ptrConnection->
sendJsonRpcError
(error);
continue
;
}
//
get the last event id seen by the client
int
lastClientEventIdSeen = -
1
;
Error paramError =
json::readParam
(request.
params
,
0
,
&lastClientEventIdSeen);
if
(paramError)
{
ptrConnection->
sendJsonRpcError
(paramError);
continue
;
}
//
remove all events already seen by the client from our internal list
erasePreviouslyDeliveredEvents
(lastClientEventIdSeen);
//
sync next event id to client (required so that when we resume
//
from a suspend we provide client event ids in line with the
//
client's expectations -- if we started with zero then the client
//
would never see any events!)
nextEventId =
std::max
(nextEventId, lastClientEventIdSeen +
1
);
//
check for events (and wait a specified internal if there are none)
try
{
//
wait for the specified maximum time
if
(
havePendingClientEvents
() || clientEventQueue.
hasEvents
() ||
clientEventQueue.
waitForEvent
(maxRequestSec))
{
//
...got at least one event
//
wait for additional events that occur in rapid succession
//
but don't wait for more than the specified maximum seconds
boost::system_time maxBatchDelayTime =
boost::get_system_time
() + maxTotalBatchDelay;
while
( clientEventQueue.
waitForEvent
(batchDelay) &&
(
boost::get_system_time
() < maxBatchDelayTime) )
{
}
}
}
catch
(
const
boost::thread_interrupted& e)
{
//
set flag so we terminate on the next accept loop iteration
stopServer =
true
;
//
NOTE: even if we are interrupted we still want to allow the
//
response to be sent (e.g. need to send the client either
//
an empty list of events back or perhaps even the quit event!)
}
//
if this is the correct client then remove events from the
//
queue and send them. otherwise, send an InvalidClientId error
//
to this client. the currently active client will then pickup the
//
events on the next iteration of the accept loop
if
(request.
clientId
==
clientId
())
{
//
deque the events
std::vector<ClientEvent> events;
clientEventQueue.
remove
(&events);
//
convert to json and add event id
for
(std::vector<ClientEvent>::const_iterator
it = events.
begin
(); it != events.
end
(); ++it)
{
json::Object event ;
it->
asJsonObject
(nextEventId++, &event);
addClientEvent
(event);
}
//
send them (pass false for kEventsPending b/c responses from the
//
event service shouldn't interact with automatic event service
//
starting/re-starting)
json::JsonRpcResponse response;
setClientEventResult
(&response);
response.
setField
(
kEventsPending
,
"
false
"
);
ptrConnection->
sendJsonRpcResponse
(response);
}
else
{
Error
error
(json::errc::InvalidClientId,
ERROR_LOCATION
);
ptrConnection->
sendJsonRpcError
(error);
}
}
}
CATCH_UNEXPECTED_EXCEPTION
}
}
//
namespace session
Back
|
FazBrowse Home
|
New Git URL