Debian Codegraph @

R/comm_manager.r

raw symbols SCIP
1 #' The CommManager
2 #'
3 #' Has methods able to register comms/targets and process comm messages
4 #'
5 #' @include logging.r class_unions.r
6 #' @export
7 CommManager <- setRefClass(
8 'CommManager',
9 fields = list(
10 send_response = 'function',
11 target_to_handler_map = 'list',
12 commid_to_comm = 'list',
13 parent_request = 'list'
14 ),
15 methods = list(
16 new_comm = function(target_name, comm_id = UUIDgenerate()) {
17 Comm$new(id = comm_id, target_name = target_name, comm_manager = .self)
18 },
19 register_target = function(target_name, handler_func) target_to_handler_map[[target_name]] <<- handler_func,
20 unregister_target = function(target_name, handler_func) target_to_handler_map[[target_name]] <<- NULL,
21 register_comm = function(comm) commid_to_comm[[comm$id]] <<- comm,
22 unregister_comm = function(comm) commid_to_comm[[comm$id]] <<- NULL,
23 is_comm_registered = function(comm) !is.null(commid_to_comm[[comm$id]]),
24 send_open = function(comm_id, target_name, data, metadata = list()) {
25 send_response('comm_open', parent_request, 'iopub', list(
26 metadata = metadata,
27 comm_id = comm_id,
28 target_name = target_name,
29 data = data
30 ))
31 },
32 send_msg = function(comm_id, target_name, data, metadata = list()) {
33 send_response('comm_msg', parent_request, 'iopub', list(
34 metadata = metadata,
35 comm_id = comm_id,
36 target_name = target_name,
37 data = data
38 ))
39 },
40 send_close = function(comm_id, target_name, data, metadata = list()) {
41 send_response('comm_close', parent_request, 'iopub', list(
42 metadata = metadata,
43 comm_id = comm_id,
44 target_name = target_name,
45 data = data
46 ))
47 },
48 make_comm_list = function(comm_list) {
49 all_comms <- list()
50 for (the_comm in comm_list) {
51 all_comms[[the_comm$id]] <- list(target_name = the_comm$target_name)
52 }
53 all_comms
54 },
55 #response:
56 #content = {
57 # # A dictionary of the comms, indexed by uuids.
58 # 'comms': {
59 # comm_id_1: {
60 # 'target_name': str,
61 # },
62 # comm_id_2: {
63 # 'target_name': str,
64 # },
65 # },
66 #}
67 on_comm_info_request = function(request) {
68 #If request$content$target_name is provided return all commids registered with that target_name
69 #Else target_name not in the request return all commids accross all targets
70 reply_msg <- list()
71 comms <- list()
72 if ('target_name' %in% names(request$content)) {
73 #reply with comms only for the specified target_name
74 target_name_requested <- request$content$target_name
75 filtered_comms <- Filter(function(x) x$target_name == target_name_requested, commid_to_comm)
76 comms[['comms']] <- make_comm_list(filtered_comms)
77 } else {
78 comms[['comms']] <- make_comm_list(commid_to_comm)
79 }
80 reply_msg[['content']] <- comms
81 send_response('comm_info_reply', request, 'shell', reply_msg)
82 },
83 #{
84 # 'comm_id' : 'u-u-i-d',
85 # 'target_name' : 'my_comm',
86 # 'data' : {}
87 # }
88 on_comm_open = function(request) {
89 parent_request <<- request
90 target_name <- request$content$target_name
91 comm_id <- request$content$comm_id
92
93 if (target_name %in% names(target_to_handler_map)) {
94 # create a comm object
95 comm <- new_comm(target_name, comm_id)
96 register_comm(comm)
97 data <- request$content$data
98
99 #invoke target handler
100 tryCatch({
101 target_to_handler_map[[target_name]](comm, data)
102 }, error = function(e) {
103 log_debug('error invoking the handler for target: %s', e)
104 })
105 } else {
106 log_debug('target_name not found in comm_open')
107 #reply with a comm_close message as target_name not found
108 send_close(comm_id, target_name, list())
109 }
110 },
111 #{
112 # 'comm_id' : 'u-u-i-d',
113 # 'data' : {}
114 #}
115 on_comm_msg = function(request) {
116 parent_request <<- request
117 comm_id <- request$content$comm_id
118
119 if (comm_id %in% names(commid_to_comm)) {
120 comm <- commid_to_comm[[comm_id]]
121
122 data <- request$content$data
123 tryCatch({
124 comm$handle_msg(data)
125 }, error = function(e) {
126 log_debug('error invoking comm handle msg: %s', e)
127 })
128 } else {
129 log_debug('comm_id not found in comm_msg')
130 }
131 },
132
133 #{
134 # 'comm_id' : 'u-u-i-d',
135 # 'data' : {}
136 #}
137 on_comm_close = function(request) {
138 parent_request <<- request
139 comm_id <- request$content$comm_id
140
141 if (comm_id %in% names(commid_to_comm)) {
142 comm <- commid_to_comm[[comm_id]]
143 tryCatch({
144 comm$handle_close()
145 }, error = function(e) {
146 log_debug('error invoking comm handle close: %s', e)
147 })
148 unregister_comm(comm)
149 } else {
150 log_debug('comm_id not found in comm_msg')
151 }
152 },
153 initialize = function(...) {
154 callSuper(...)
155 }
156 )
157 )
158
159 #' The Comm
160 #'
161 #' Has methods able to register and handle message callbacks
162 #'
163 #' @export
164 Comm <- setRefClass(
165 'Comm',
166 fields = list(
167 id = 'character',
168 target_name = 'character',
169 comm_manager = 'CommManager',
170 msg_callback = 'functionOrNULL',
171 close_callback = 'functionOrNULL'
172 ),
173 methods = list(
174 open = function(msg = list()) {
175 if (!comm_manager$is_comm_registered(.self)) {
176 comm_manager$register_comm(.self)
177 comm_manager$send_open(id, target_name, msg)
178 } else {
179 log_debug('Comm already opened!')
180 }
181 },
182 send = function(msg = list()) {
183 if (comm_manager$is_comm_registered(.self)) {
184 comm_manager$send_msg(id, target_name, msg)
185 } else {
186 log_debug('Comm is not opened. Cannot send!')
187 }
188 },
189 close = function(msg = list()) {
190 if (comm_manager$is_comm_registered(.self)) {
191 comm_manager$send_close(id, target_name, msg)
192 comm_manager$unregister_comm(.self)
193 } else {
194 log_debug('Comm is already closed!')
195 }
196 },
197 on_msg = function(a_msg_callback) {
198 msg_callback <<- a_msg_callback
199 },
200 on_close = function(a_close_callback) {
201 close_callback <<- a_close_callback
202 },
203 handle_msg = function(msg) {
204 if (!is.null(msg_callback)) {
205 msg_callback(msg)
206 }
207 },
208 handle_close = function() {
209 if (!is.null(close_callback)) {
210 close_callback()
211 }
212 }
213 )
214 )