Fix networked setting request
parent: tbd commit: 4b5330d
1 | use ; |
2 | |
3 | use FutureExt; |
4 | use |
5 | , | AuthenticatedPayload
6 | , |
7 | , | Instance
8 | , | GiteratedMessage
9 | |
10 | AnyObject, GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse, |
11 | , |
12 | , | ObjectBackend
13 | , |
14 | , |
15 | , |
16 | ; |
17 | use Value; |
18 | use ; |
19 | |
20 | use crate:: |
21 | GiteratedOperationHandler, ObjectMeta, ObjectOperationPair, ObjectValuePair, OperationMeta, |
22 | OperationWrapper, SettingMeta, StackOperationState, ValueMeta, |
23 | ; |
24 | |
25 | /// Temporary name for the next generation of Giterated stack |
26 | |
27 | |
28 | operation_handlers: , |
29 | value_getters: , |
30 | setting_getters: , |
31 | metadata: RuntimeMetadata, |
32 | |
33 | |
34 | |
35 | |
36 | &mut self, |
37 | builder: , |
38 | |
39 | for in builder.operation_handlers |
40 | let tree = self.get_or_create_tree; |
41 | |
42 | tree.push; |
43 | |
44 | |
45 | for in builder.value_getters |
46 | assert!; |
47 | |
48 | |
49 | for in builder.setting_getters |
50 | assert!; |
51 | |
52 | |
53 | self.metadata.append; |
54 | |
55 | self |
56 | |
57 | |
58 | |
59 | if self.operation_handlers.contains_key |
60 | self.operation_handlers.get_mut .unwrap |
61 | else |
62 | self.operation_handlers |
63 | .insert; |
64 | |
65 | self.operation_handlers.get_mut .unwrap |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | elements: , |
72 | |
73 | |
74 | |
75 | |
76 | Self |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | self.elements.push; |
83 | |
84 | |
85 | pub async |
86 | &self, |
87 | object: & , |
88 | operation: & , |
89 | operation_state: &StackOperationState, |
90 | |
91 | for handler in self.elements.iter |
92 | match handler.handle .await |
93 | Ok => return Ok, |
94 | Err => match err |
95 | => | Operation
96 | return Err |
97 | |
98 | => return Err, | Internal
99 | _ => |
100 | continue; |
101 | |
102 | , |
103 | |
104 | |
105 | |
106 | Err |
107 | |
108 | |
109 | |
110 | /// Stores runtime metadata for all in-use Giterated protocol types. |
111 | |
112 | |
113 | objects: , |
114 | operations: , |
115 | values: , |
116 | settings: , |
117 | |
118 | |
119 | /// Defines a type that is a valid Giterated runtime state. |
120 | /// |
121 | /// This allows for extraction of state in handlers, based on a |
122 | /// [`FromOperationState<S>`] impl on (what is in this case) [`Self`]. |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | operation_handlers: , |
129 | value_getters: , |
130 | setting_getters: , |
131 | metadata: RuntimeMetadata, |
132 | state: S, |
133 | |
134 | |
135 | |
136 | |
137 | Self |
138 | operation_handlers: Default default, |
139 | value_getters: Default default, |
140 | setting_getters: Default default, |
141 | metadata: Default default, |
142 | state, |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | /// Insert an operation handler into the runtime builder. |
149 | /// |
150 | /// # Type Registration |
151 | /// Inserting the handler will automatically, if required, register the operation type of the |
152 | /// handler. It will **not** register the object type automatically. |
153 | |
154 | |
155 | O: GiteratedObject + 'static, |
156 | D: + 'static + Clone, |
157 | H: + 'static + Clone + Send + Sync, |
158 | : Send + Sync, | Failure
159 | : Send + Sync, | Success
160 | |
161 | let object_name = handler.object_name .to_string; |
162 | let operation_name = handler.operation_name .to_string; |
163 | |
164 | let wrapped = new; |
165 | |
166 | let pair = ObjectOperationPair |
167 | object_name, |
168 | operation_name, |
169 | ; |
170 | |
171 | self.operation_handlers.insert; |
172 | |
173 | self.metadata.; |
174 | |
175 | self |
176 | |
177 | |
178 | /// Register a [`GiteratedObject`] type with the runtime. |
179 | /// |
180 | /// # Type Registration |
181 | /// This will register the provided object type. |
182 | |
183 | self.metadata.; |
184 | |
185 | // Insert handler so ObjectRequest is handled properly |
186 | let handler = move |_object: &Instance, |
187 | operation: ObjectRequest, |
188 | _state: S, |
189 | _operation_state: StackOperationState, |
190 | stack: | |
191 | async move |
192 | for in stack.metadata.objects.iter |
193 | if .is_ok |
194 | return Ok; |
195 | |
196 | |
197 | |
198 | Err |
199 | |
200 | .boxed_local |
201 | ; |
202 | |
203 | self.operation; |
204 | |
205 | self |
206 | |
207 | |
208 | /// Register a [`Setting`] type with the runtime. |
209 | /// |
210 | /// # Type Registration |
211 | /// This will register the provided setting type. |
212 | |
213 | self.metadata.; |
214 | |
215 | self |
216 | |
217 | |
218 | /// Register a [`GiteratedObjectValue<O>`] type with the runtime, providing |
219 | /// its associated handler for [`GetValue`]. |
220 | /// |
221 | /// # Type Registration |
222 | /// This will register the provided [`GiteratedObjectValue`] type for its matching / specified |
223 | /// object type. It will **not** register the object type automatically. |
224 | |
225 | |
226 | O: GiteratedObject + 'static, |
227 | V: + 'static + Clone, |
228 | F: + Clone + 'static + Send + Sync, |
229 | |
230 | let object_name = handler.object_name .to_string; |
231 | let value_name = value_name .to_string; |
232 | |
233 | let wrapped = new; |
234 | |
235 | let handler_object_name = object_name.clone; |
236 | let handler_value_name = value_name.clone; |
237 | |
238 | // Insert handler so GetValue is handled properly |
239 | let _handler = move |object: &O, |
240 | operation: , |
241 | _state: S, |
242 | operation_state: StackOperationState, |
243 | stack: | |
244 | let stack = stack.clone; |
245 | let object_name = handler_object_name; |
246 | let value_name = handler_value_name; |
247 | let object = object.clone; |
248 | async move |
249 | for in stack.value_getters.iter |
250 | if target.object_kind != object_name |
251 | continue; |
252 | |
253 | |
254 | if target.value_kind != value_name |
255 | continue; |
256 | |
257 | |
258 | return match getter |
259 | .handle |
260 | &, |
261 | & |
262 | value_name: operation.value_name, |
263 | ty: Default default, |
264 | as , |
265 | &operation_state, |
266 | |
267 | .await |
268 | Ok => Ok, |
269 | Err => Err |
270 | => Operation, | Operation
271 | => Internal, | Internal
272 | => Unhandled, | Unhandled
273 | , |
274 | |
275 | |
276 | |
277 | Err |
278 | |
279 | .boxed_local |
280 | ; |
281 | |
282 | assert! |
283 | .value_getters |
284 | .insert |
285 | ObjectValuePair |
286 | object_kind: object_name, |
287 | value_kind: value_name |
288 | , |
289 | wrapped |
290 | |
291 | .is_none; |
292 | |
293 | self.metadata.; |
294 | |
295 | self |
296 | |
297 | |
298 | /// Register a handler for [`GetSetting`] for it's associated object type. |
299 | |
300 | |
301 | O: GiteratedObject + 'static, |
302 | F: + Clone + 'static + Send + Sync, |
303 | |
304 | let object_name = handler.object_name .to_string; |
305 | |
306 | let wrapped = new; |
307 | |
308 | assert!; |
309 | |
310 | self |
311 | |
312 | |
313 | |
314 | |
315 | |
316 | let object_name = object_name .to_string; |
317 | |
318 | let object_meta = ObjectMeta |
319 | name: object_name.clone, |
320 | from_str: Box new, |
321 | any_is_same: Box new, |
322 | ; |
323 | |
324 | if self.objects.insert .is_some |
325 | trace! |
326 | "Registration of object {} overwrote previous registration.", |
327 | object_name |
328 | ; |
329 | else |
330 | trace! |
331 | |
332 | |
333 | |
334 | |
335 | &mut self, |
336 | |
337 | let object_name = object_name .to_string; |
338 | let operation_name = operation_name .to_string; |
339 | |
340 | if self |
341 | .operations |
342 | .insert |
343 | ObjectOperationPair |
344 | object_name: object_name.clone, |
345 | operation_name: operation_name.clone, |
346 | , |
347 | OperationMeta |
348 | name: operation_name, |
349 | object_kind: object_name, |
350 | deserialize: Box new |
351 | Ok |
352 | as |
353 | , |
354 | any_is_same: Box new, |
355 | serialize_success: Box new |
356 | let to_serialize = any. .unwrap; |
357 | to_vec |
358 | , |
359 | serialize_error: Box new |
360 | let to_serialize = any. .unwrap; |
361 | to_vec |
362 | , |
363 | , |
364 | |
365 | .is_some |
366 | |
367 | trace! |
368 | "Registration of object operation {}<{}> overwrote previous registration.", |
369 | , | operation_name
370 | object_name |
371 | ; |
372 | else |
373 | trace! |
374 | "Registration of object operation {}<{}>.", |
375 | , | operation_name
376 | object_name |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | O: GiteratedObject + 'static, |
383 | V: + 'static, |
384 | > |
385 | &mut self, |
386 | |
387 | let object_name = object_name .to_string; |
388 | let value_name = value_name .to_string; |
389 | let value_name_for_get = value_name .to_string; |
390 | |
391 | if self |
392 | .values |
393 | .insert |
394 | ObjectValuePair |
395 | object_kind: object_name.clone, |
396 | value_kind: value_name.clone, |
397 | , |
398 | ValueMeta |
399 | name: value_name.clone, |
400 | deserialize: Box new, |
401 | serialize: Box new |
402 | let value = value. .unwrap; |
403 | |
404 | Ok |
405 | , |
406 | typed_get: Box new |
407 | Box new |
408 | value_name: value_name_for_get.clone, |
409 | ty: Default default, |
410 | |
411 | , |
412 | is_get_value_typed: Box new, |
413 | , |
414 | |
415 | .is_some |
416 | |
417 | trace! |
418 | "Registration of value <{}>::{} overwrote previous registration.", |
419 | , | object_name
420 | value_name |
421 | ; |
422 | else |
423 | trace! |
424 | "Registration of value <{}>::{}.", |
425 | , | object_name
426 | value_name |
427 | ; |
428 | |
429 | |
430 | |
431 | |
432 | let setting_name = name .to_string; |
433 | |
434 | if self |
435 | .settings |
436 | .insert |
437 | setting_name.clone, |
438 | SettingMeta |
439 | name: setting_name, |
440 | deserialize: Box new, |
441 | serialize: Box new, |
442 | , |
443 | |
444 | .is_some |
445 | |
446 | trace! |
447 | "Registration of setting {} overwrote previous registration.", |
448 | name |
449 | ; |
450 | else |
451 | trace!; |
452 | |
453 | |
454 | |
455 | |
456 | self.objects.extend; |
457 | self.operations.extend; |
458 | self.values.extend; |
459 | self.settings.extend; |
460 | |
461 | |
462 | |
463 | /// Handles a giterated network message, returning either a raw success |
464 | /// payload or a serialized error payload. |
465 | pub async |
466 | &self, |
467 | message: AuthenticatedPayload, |
468 | operation_state: &StackOperationState, |
469 | |
470 | let message: = message.into_message; |
471 | |
472 | // Deserialize the object, also getting the object type's name |
473 | let = |
474 | let mut result = None; |
475 | |
476 | for in self.metadata.objects.iter |
477 | if let Ok = |
478 | result = Some; |
479 | break; |
480 | |
481 | |
482 | |
483 | result |
484 | |
485 | .ok_or_else?; |
486 | |
487 | trace! |
488 | "Handling network message {}::<{}>", |
489 | message.operation, |
490 | object_type |
491 | ; |
492 | |
493 | if message.operation == "get_value" |
494 | // Special case |
495 | let operation: GetValue = from_slice .unwrap; |
496 | |
497 | return self |
498 | .network_get_value |
499 | .await; |
500 | else if message.operation == "get_setting" |
501 | let operation: GetSetting = from_slice .unwrap; |
502 | info!; |
503 | let setting_meta = self |
504 | .metadata |
505 | .settings |
506 | .get |
507 | .ok_or_else?; |
508 | info!; |
509 | let raw_result = self |
510 | .get_setting |
511 | .await; |
512 | info!; |
513 | return match raw_result |
514 | Ok => |
515 | info!; |
516 | // Success is the setting type, serialize it |
517 | let serialized = .unwrap; |
518 | |
519 | Ok |
520 | |
521 | Err => Err |
522 | => | Operation
523 | info!; |
524 | // We know how to resolve this type |
525 | let failure: GetSettingError = *failure.downcast .unwrap; |
526 | |
527 | Operation |
528 | |
529 | => Internal, | Internal
530 | => Unhandled, | Unhandled
531 | , |
532 | ; |
533 | |
534 | |
535 | let target = ObjectOperationPair |
536 | object_name: object_type.clone, |
537 | operation_name: message.operation.clone, |
538 | ; |
539 | |
540 | // Resolve the target operations from the handlers table |
541 | let handler = self |
542 | .operation_handlers |
543 | .get |
544 | .ok_or_else?; |
545 | |
546 | trace! |
547 | "Resolved operation handler for network message {}::<{}>", |
548 | message.operation, |
549 | object_type |
550 | ; |
551 | |
552 | // Deserialize the operation |
553 | let meta = self |
554 | .metadata |
555 | .operations |
556 | .get |
557 | .ok_or_else?; |
558 | |
559 | let operation = |
560 | .map_err?; |
561 | |
562 | trace! |
563 | "Deserialized operation for network message {}::<{}>", |
564 | message.operation, |
565 | object_type |
566 | ; |
567 | |
568 | trace! |
569 | "Calling handler for network message {}::<{}>", |
570 | message.operation, |
571 | object_type |
572 | ; |
573 | |
574 | // Get the raw result of the operation, where the return values are boxed. |
575 | let raw_result = handler.handle .await; |
576 | |
577 | trace! |
578 | "Finished handling network message {}::<{}>", |
579 | message.operation, |
580 | object_type |
581 | ; |
582 | |
583 | // Deserialize the raw result for the network |
584 | match raw_result |
585 | Ok => Ok |
586 | .map_err?, |
587 | Err => Err |
588 | => Operation | Operation
589 | |
590 | .map_err?, |
591 | , |
592 | => Internal, | Internal
593 | => Unhandled, | Unhandled
594 | , |
595 | |
596 | |
597 | |
598 | pub async |
599 | &self, |
600 | object: , |
601 | object_kind: String, |
602 | operation: GetValue, |
603 | operation_state: &StackOperationState, |
604 | |
605 | trace!; |
606 | |
607 | let value_meta = self |
608 | .metadata |
609 | .values |
610 | .get |
611 | object_kind: object_kind.clone, |
612 | value_kind: operation.value_name.clone, |
613 | |
614 | .ok_or_else?; |
615 | |
616 | for in self.value_getters.iter |
617 | if target.object_kind != object_kind |
618 | continue; |
619 | |
620 | |
621 | if target.value_kind != operation.value_name |
622 | continue; |
623 | |
624 | |
625 | return match getter |
626 | .handle |
627 | .await |
628 | |
629 | Ok => |
630 | // Serialize success, which is the value type itself |
631 | let serialized = |
632 | .map_err?; |
633 | |
634 | Ok |
635 | |
636 | Err => Err |
637 | => | Operation
638 | // Failure is sourced from GetValue operation, but this is hardcoded for now |
639 | let failure: GetValueError = *failure.downcast .unwrap; |
640 | |
641 | Operation |
642 | to_vec |
643 | .map_err?, |
644 | |
645 | |
646 | => Internal, | Internal
647 | => Unhandled, | Unhandled
648 | , |
649 | ; |
650 | |
651 | |
652 | Err |
653 | |
654 | |
655 | pub async |
656 | &self, |
657 | object: , |
658 | object_kind: String, |
659 | operation: GetSetting, |
660 | operation_state: &StackOperationState, |
661 | |
662 | trace! |
663 | "Handling network {}::get_setting for {}", |
664 | object_kind, |
665 | operation.setting_name |
666 | ; |
667 | |
668 | let setting_getter = self |
669 | .setting_getters |
670 | .get |
671 | .ok_or_else?; |
672 | |
673 | setting_getter |
674 | .handle |
675 | .await |
676 | |
677 | |
678 | pub async |
679 | &self, |
680 | _operation: SetSetting, |
681 | _operation_state: &StackOperationState, |
682 | |
683 | todo! |
684 | |
685 | |
686 | |
687 | use Debug; |
688 | |
689 | |
690 | |
691 | async |
692 | &self, |
693 | in_object: O, |
694 | operation_name: &str, |
695 | payload: D, |
696 | operation_state: &StackOperationState, |
697 | |
698 | |
699 | O: GiteratedObject + Debug + 'static, |
700 | D: + Debug + 'static, |
701 | |
702 | // Erase object and operation types. |
703 | let object = Box new as ; |
704 | let operation = Box new as ; |
705 | |
706 | // We need to determine the type of the object, iterate through all known |
707 | // object types and check if the &dyn Any we have is the same type as the |
708 | // object type. |
709 | let object_type = |
710 | let mut object_type = None; |
711 | |
712 | for in self.metadata.objects.iter |
713 | if |
714 | object_type = Some; |
715 | break; |
716 | |
717 | |
718 | |
719 | object_type |
720 | |
721 | .ok_or_else?; |
722 | |
723 | // We need to hijack get_value, set_setting, and get_setting. |
724 | if operation_name == "get_value" |
725 | let mut value_meta = None; |
726 | for in self.metadata.values.iter |
727 | if |
728 | value_meta = Some; |
729 | break; |
730 | |
731 | |
732 | |
733 | let value_meta = value_meta.ok_or_else?; |
734 | |
735 | let value_name = value_meta.name.clone; |
736 | |
737 | trace!; |
738 | |
739 | for in self.value_getters.iter |
740 | if target.object_kind != object_type |
741 | continue; |
742 | |
743 | |
744 | if target.value_kind != value_name |
745 | continue; |
746 | |
747 | |
748 | return match getter |
749 | .handle |
750 | .await |
751 | |
752 | Ok => Ok, |
753 | Err => Err |
754 | => | Operation
755 | Operation |
756 | |
757 | => Internal, | Internal
758 | => Unhandled, | Unhandled
759 | , |
760 | ; |
761 | |
762 | |
763 | return Err; |
764 | else if operation. |
765 | let get_setting: = operation.downcast .unwrap; |
766 | let _setting_name = get_setting.setting_name.clone; |
767 | |
768 | let _setting_meta = self |
769 | .metadata |
770 | .settings |
771 | .get |
772 | .ok_or_else?; |
773 | |
774 | let raw_result = self |
775 | .get_setting |
776 | .await; |
777 | |
778 | return match raw_result |
779 | Ok => |
780 | // Success is the setting type, serialize it |
781 | // let serialized = (setting_meta.serialize)(success).unwrap(); |
782 | |
783 | // Ok(serde_json::to_vec(&serialized).unwrap()) |
784 | Ok |
785 | |
786 | Err => Err |
787 | => | Operation
788 | // We know this is the right type |
789 | Operation |
790 | |
791 | => Internal, | Internal
792 | => Unhandled, | Unhandled
793 | , |
794 | ; |
795 | else if operation. |
796 | todo! |
797 | else if operation. |
798 | todo! |
799 | |
800 | |
801 | // Resolve the operation from the known operations table. |
802 | let operation_type = |
803 | let mut operation_type = None; |
804 | |
805 | for in self.metadata.operations.iter |
806 | // Skip elements that we know will not match |
807 | if target.object_name != object_type |
808 | continue; |
809 | |
810 | |
811 | if target.operation_name != operation_name |
812 | continue; |
813 | |
814 | |
815 | if |
816 | operation_type = Some; |
817 | break; |
818 | |
819 | |
820 | |
821 | operation_type |
822 | |
823 | .ok_or_else?; |
824 | |
825 | // Resolve the handler from our handler tree |
826 | let handler_tree = self |
827 | .operation_handlers |
828 | .get |
829 | .ok_or_else?; |
830 | |
831 | let raw_result = handler_tree |
832 | .handle |
833 | .await; |
834 | |
835 | // Convert the dynamic result back into its concrete type |
836 | match raw_result |
837 | Ok => Ok, |
838 | Err => Err |
839 | => Internal, | Internal
840 | => | Operation
841 | Operation |
842 | |
843 | => Unhandled, | Unhandled
844 | , |
845 | |
846 | |
847 | |
848 | async |
849 | &self, |
850 | object_str: &str, |
851 | _operation_state: &StackOperationState, |
852 | |
853 | // TODO: Authorization? |
854 | for in self.metadata.objects.iter |
855 | if let Ok = |
856 | return Ok |
857 | new_unchecked |
858 | ; |
859 | |
860 | |
861 | |
862 | Err |
863 | |
864 | |
865 |