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
#[macro_use]
extern crate wasmcloud_provider_core as codec;
use log::{info, trace};
mod server;
mod session;
use codec::{
capabilities::{CapabilityProvider, Dispatcher, NullDispatcher},
core::{OP_BIND_ACTOR, OP_HEALTH_REQUEST, OP_REMOVE_ACTOR, SYSTEM_ACTOR},
deserialize, serialize,
};
use crossbeam::channel::Sender;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
error::Error,
sync::{Arc, RwLock},
};
use wasmcloud_actor_core::{CapabilityConfiguration, HealthCheckResponse};
type MessageHandlerResult = Result<Vec<u8>, Box<dyn Error + Send + Sync + 'static>>;
#[allow(dead_code)]
const CAPABILITY_ID: &str = "wasmcloud:telnet";
pub(crate) const OP_SEND_TEXT: &str = "SendText";
pub(crate) const OP_SESSION_STARTED: &str = "SessionStarted";
pub(crate) const OP_RECEIVE_TEXT: &str = "ReceiveText";
#[cfg(not(feature = "static_plugin"))]
capability_provider!(TelnetProvider, TelnetProvider::new);
#[derive(Clone)]
pub struct TelnetProvider {
dispatcher: Arc<RwLock<Box<dyn Dispatcher>>>,
outbounds: Arc<RwLock<HashMap<String, Sender<String>>>>,
}
impl Default for TelnetProvider {
fn default() -> Self {
let _ = env_logger::try_init();
TelnetProvider {
dispatcher: Arc::new(RwLock::new(Box::new(NullDispatcher::new()))),
outbounds: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl TelnetProvider {
pub fn new() -> Self {
Self::default()
}
fn configure(&self, config: CapabilityConfiguration) -> MessageHandlerResult {
session::start_server(
std::fs::read_to_string(&config.values.get("MOTD").unwrap_or(&"".to_string()))?,
config
.values
.get("PORT")
.unwrap_or(&"3000".to_string())
.parse()
.unwrap(),
&config.module,
self.dispatcher.clone(),
self.outbounds.clone(),
);
Ok(vec![])
}
fn deconfigure(&self, _config: CapabilityConfiguration) -> MessageHandlerResult {
Ok(vec![])
}
fn send_text(&self, _actor: &str, msg: TelnetMessage) -> MessageHandlerResult {
let outbound = self.outbounds.read().unwrap()[&msg.session].clone();
outbound.send(msg.text).unwrap();
Ok(vec![])
}
fn health(&self) -> MessageHandlerResult {
Ok(serialize(HealthCheckResponse {
healthy: true,
message: "".to_string(),
})?)
}
}
impl CapabilityProvider for TelnetProvider {
fn configure_dispatch(
&self,
dispatcher: Box<dyn Dispatcher>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
info!("Dispatcher received.");
let mut lock = self.dispatcher.write().unwrap();
*lock = dispatcher;
Ok(())
}
fn handle_call(
&self,
actor: &str,
op: &str,
msg: &[u8],
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
trace!("Received host call from {}, operation - {}", actor, op);
match op {
OP_BIND_ACTOR if actor == SYSTEM_ACTOR => self.configure(deserialize(msg)?),
OP_REMOVE_ACTOR if actor == SYSTEM_ACTOR => self.deconfigure(deserialize(msg)?),
OP_HEALTH_REQUEST if actor == SYSTEM_ACTOR => self.health(),
OP_SEND_TEXT => self.send_text(actor, deserialize(msg)?),
_ => Err("bad dispatch".into()),
}
}
fn stop(&self) {
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TelnetMessage {
pub session: String,
pub text: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SessionStarted {
pub session: String,
}