starlane_core/message/
resource.rs1use core::cell::Cell;
2use core::option::Option;
3use core::result::Result;
4use core::result::Result::{Err, Ok};
5use std::collections::HashSet;
6use std::iter::FromIterator;
7
8use serde::{Deserialize, Serialize};
9use tokio::sync::oneshot;
10use tokio::time::Duration;
11
12use starlane_resources::{RemoteDataSrc, ResourceCreate, ResourceIdentifier, ResourceSelector, SkewerCase, Version};
13use starlane_resources::data::{BinSrc, DataSet};
14use starlane_resources::message::{Fail, MessageFrom, MessageId, MessageTo};
15
16use crate::error::Error;
17use crate::frame::{MessagePayload, Reply, SimpleReply, StarMessage, StarMessagePayload};
18use crate::message::ProtoStarMessage;
19use crate::resource::{ResourceId, ResourceKey, ResourceRecord, ResourceType};
20use crate::star::{StarCommand, StarSkel};
21use crate::util;
22
23pub fn reverse(to: MessageTo) -> MessageFrom {
26 MessageFrom::Resource(to)
27}
28
29#[derive(Clone)]
30pub struct Delivery<M>
31where
32 M: Clone,
33{
34 skel: StarSkel,
35 star_message: StarMessage,
36 pub payload: M,
37}
38
39impl<M> Delivery<M>
40where
41 M: Clone + Send + Sync + 'static,
42{
43 pub fn new(payload: M, star_message: StarMessage, skel: StarSkel) -> Self {
44 Delivery {
45 payload,
46 star_message: star_message,
47 skel: skel,
48 }
49 }
50}
51
52impl<M> Delivery<M>
53where
54 M: Clone + Send + Sync + 'static,
55{
56 pub fn result(&self, result: Result<Reply, Fail>) {
57 match result {
58 Ok(reply) => {
59 self.reply(reply);
60 }
61 Err(fail) => {
62 self.fail(fail);
63 }
64 }
65 }
66
67 pub fn result_ok<T>(&self, result: Result<T, Error>) {
68 match result {
69 Ok(_) => {
70 self.reply(Reply::Empty);
71 }
72 Err(fail) => {
73 self.fail(fail.into());
74 }
75 }
76 }
77
78 pub fn result_rx<T>(self, mut rx: oneshot::Receiver<Result<T, Error>>)
79 where
80 T: Send + Sync + 'static,
81 {
82 tokio::spawn(async move {
83 match tokio::time::timeout(Duration::from_secs(15), rx).await {
84 Ok(Ok(Ok(_))) => {
85 self.reply(Reply::Empty);
86 }
87 Ok(Ok(Err(fail))) => {
88 self.fail(fail.into());
89 }
90 Ok(Err(_)) => {
91 self.fail(Fail::Timeout);
92 }
93 Err(_) => {
94 self.fail(Fail::ChannelRecvErr);
95 }
96 }
97 });
98 }
99
100 pub fn ok(&self) {
101 self.reply(Reply::Empty);
102 }
103
104 pub fn reply(&self, reply: Reply) {
105 let proto = self
106 .star_message
107 .reply(StarMessagePayload::Reply(SimpleReply::Ok(reply)));
108 self.skel.messaging_api.send(proto);
109 }
110
111 pub fn fail(&self, fail: Fail) {
112 let proto = self
113 .star_message
114 .reply(StarMessagePayload::Reply(SimpleReply::Fail(fail)));
115 self.skel.messaging_api.send(proto);
116 }
117}
118
119#[derive(Clone, Serialize, Deserialize)]
134pub enum ActorMessage {}
135
136pub struct DeliverySelector{
137 selections: Vec<DeliverySelection>
138}
139
140pub enum DeliverySelection{
141 Any
142}
143
144impl DeliverySelector {
145 pub fn any() ->Self {
146 Self {
147 selections: vec![DeliverySelection::Any]
148 }
149 }
150}
151