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