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
|
#![feature(box_syntax)]
#![feature(fs_try_exists)]
#![feature(if_let_guard)]
pub mod condition;
pub mod demo;
pub mod helper;
pub mod interface;
pub mod savestate;
pub mod schedule;
use crate::schedule::schedule_dynamic;
use chrono::NaiveDateTime;
use condition::ConditionFind;
use crossbeam_channel::Sender;
use helper::Overlaps;
use karlcommon::{ClientboundPacket, ProtoError, Schedule, ServerboundPacket, Task};
use log::{debug, error, info};
use std::{
collections::HashMap,
sync::{atomic::AtomicU32, RwLock},
};
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.parse_env("LOG")
.init();
info!("logging");
if let Err(e) = savestate::load() {
error!("load failed: {}", e);
}
#[cfg(target_os = "windows")]
log::warn!("Windows is not supported, please do not report any bugs for this platform. Use a free operating system instead.");
#[cfg(target_os = "macos")]
log::warn!("Mac OS is not supported, please do not report any bugs for this platform. Use a free operating system instead.");
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs_f64(0.1));
schedule_dynamic();
});
// demo::load_demo();
interface::start();
loop {
std::thread::sleep(std::time::Duration::from_secs_f64(100.0));
}
}
lazy_static::lazy_static! {
static ref TASKS: RwLock<HashMap<u64, Task>> = RwLock::new(HashMap::new());
static ref CLIENTS: RwLock<HashMap<u32, Sender<ClientboundPacket>>> = RwLock::new(HashMap::new());
static ref CLIENT_ID_COUNTER: AtomicU32 = AtomicU32::new(0);
}
fn broadcast_invalidation() {
CLIENTS
.write()
.unwrap()
.values()
.for_each(|r| drop(r.send(ClientboundPacket::InvalidateState)));
}
pub fn handle_packet(client: u32, packet: ServerboundPacket, responder: Sender<ClientboundPacket>) {
// std::thread::sleep(std::time::Duration::from_millis(75)); // for testing clients with latency
match packet {
ServerboundPacket::Sync => {
drop(responder.send(ClientboundPacket::Sync));
}
ServerboundPacket::ListTasks => {
drop(responder.send(ClientboundPacket::TaskList(
TASKS.read().unwrap().values().map(|e| e.clone()).collect(),
)));
}
ServerboundPacket::UpdateTask(t) => {
TASKS.write().unwrap().insert(t.id, t);
broadcast_invalidation();
savestate::save();
}
ServerboundPacket::RemoveTask(i) => {
if TASKS.write().unwrap().remove(&i).is_some() {
broadcast_invalidation();
} else {
drop(responder.send(ClientboundPacket::Error(ProtoError::UnknownTask)));
}
savestate::save();
}
ServerboundPacket::Handshake { version } => {
debug!("{client}: version {version}");
}
ServerboundPacket::ListInstances { range, task, limit } => {
let t = match TASKS.read().unwrap().get(&task).cloned() {
Some(t) => t,
None => {
return drop(responder.send(ClientboundPacket::Error(ProtoError::UnknownTask)))
}
};
let mut ocs = vec![];
match t.schedule {
Schedule::Never => (),
Schedule::Dynamic { .. } => (), // TODO
Schedule::Static(r) => {
if range.overlaps(r.clone()) {
ocs.push(Some(r.start)..Some(r.end))
}
}
Schedule::Condition(o) => {
let mut time =
NaiveDateTime::from_timestamp_opt(range.start.unwrap_or(0), 0).unwrap();
let end_time = range
.end
.map(|e| NaiveDateTime::from_timestamp_opt(e, 0).unwrap());
for _ in 0..limit {
let start =
o.find(condition::Edge::Start, condition::Direction::Forward, time);
let end = o.find(condition::Edge::End, condition::Direction::Forward, time);
ocs.push(start.map(|e| e.timestamp())..end.map(|e| e.timestamp()));
if let Some(s) = end {
if let Some(e) = end_time {
if s > e {
break;
}
}
time = s;
} else {
break;
}
}
}
}
drop(responder.send(ClientboundPacket::InstanceList(ocs)));
}
}
}
|