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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use ndata::dataobject::*;
use std::thread;
use std::panic;
use std::fs;
use std::path::Path;
use std::fs::File;
use ndata::dataarray::*;
//use ndata::sharedmutex::SharedMutex;
use std::time::Duration;
//use state::Storage;
use std::sync::RwLock;
//use std::sync::Once;

use crate::command::*;
use crate::datastore::*;

use crate::flowlang::system::time::time;
use crate::flowlang::file::read_properties::read_properties;
use crate::flowlang::file::write_properties::write_properties;

// FIXME - The code in this file makes the assumption in several places that the process was launched from the root directory. That assumption should only be made once, in the event that no root directory is specified, by whatever initializes the flowlang DataStore.

pub type EventHook = fn(&str,&str,DataObject);

//static START: Once = Once::new();
//pub static EVENTHOOKS:Storage<RwLock<Vec<EventHook>>> = Storage::new();
pub static mut EVENTHOOKS:RwLock<Option<Vec<EventHook>>> = RwLock::new(Some(Vec::new()));

pub fn run() {
  let system = init_globals();
  
  // Start Timers
  thread::spawn(timer_loop);
  
  // Start Events
  thread::spawn(event_loop);
  
  let dur = Duration::from_millis(500);
  while system.get_boolean("running") {
    thread::sleep(dur);
  }
}

pub fn remove_timer(tid:&str) -> bool {
  let system = DataStore::globals().get_object("system");
  let mut timers = system.get_object("timers");
  if timers.has(tid) {
    timers.remove_property(tid);
    return true;
  }
  false
}

pub fn add_timer(tid:&str, mut tdata:DataObject) {
  let system = DataStore::globals().get_object("system");
  let mut timers = system.get_object("timers");    
  let start = tdata.get_int("start");
  let start = time()+to_millis(start, tdata.get_string("startunit"));
  let interval = tdata.get_int("interval");
  let interval = to_millis(interval, tdata.get_string("intervalunit"));
  tdata.put_int("startmillis", start);
  tdata.put_int("intervalmillis", interval);
  timers.put_object(&tid, tdata);
}

pub fn add_event_hook(hook:EventHook) {
  unsafe { 
    let map = &mut EVENTHOOKS.write().unwrap();
    let map = map.as_mut().unwrap();
    map.push(hook);    
  }
}

pub fn remove_event_listener(id:&str) -> bool {
  let mut b = false;
  let system = DataStore::globals().get_object("system");
  let events = system.get_object("events");
  for (_k1, v1) in events.objects(){
    for (_k2, v2) in v1.object().objects(){
      for k3 in v2.object().keys(){
        if k3 == id { 
          v2.object().remove_property(&k3); 
          b = true;
        }
      }
    }
  }
  b
}

pub fn add_event_listener(id:&str, app:&str, event:&str, cmdlib:&str, cmdid:&str) {
  //println!("Adding event listener {}, {}, {}, {}, {}", id, app, event, cmdlib, cmdid);
  let system = DataStore::globals().get_object("system");
  let mut events = system.get_object("events");
  let mut bot;
  let mut list;
  if events.has(app) {
    bot = events.get_object(app);
  }
  else {
    bot = DataObject::new();
    events.put_object(app, bot.clone());
  }
  if bot.has(event) {
    list = bot.get_object(event);
  }
  else {
    list = DataObject::new();
    bot.put_object(event, list.clone());
  }
  let mut cmd = DataObject::new();
  cmd.put_string("lib", cmdlib);
  cmd.put_string("cmd", cmdid);
  list.put_object(id, cmd);
}

pub fn fire_event(app:&str, event:&str, data:DataObject) {
  let mut o = DataObject::new();
  o.put_string("app", &app);
  o.put_string("event", &event);
  o.put_object("data", data);
  DataStore::globals().get_object("system").get_array("fire").push_object(o);
}

pub fn event_loop() {
  let system = DataStore::globals().get_object("system");
  let mut events = system.get_object("events");
  let fire = system.get_array("fire");
  let dur = Duration::from_millis(100);
  while system.get_boolean("running") {
    let mut b = true;
    for oprop in fire.objects(){
      let o = oprop.object();
      fire.remove_data(oprop);
      b = false;
      
      let app = o.get_string("app");
      let event = o.get_string("event");
      let data = o.get_object("data");
      
      if !events.has(&app) { events.put_object(&app, DataObject::new()); }
      let mut bot = events.get_object(&app);
      if !bot.has(&event) { bot.put_object(&event, DataObject::new()); }
      else {
        let list = bot.get_object(&event);
        for (_, e) in list.objects() {
          let e = e.object();
          let lib = e.get_string("lib");
          let id = e.get_string("cmd");
          let data = data.clone();
          thread::spawn(move || {
            let command = Command::new(&lib, &id);
            let  _ = command.execute(data);
          });
        }
      }
      unsafe {
        for hook in EVENTHOOKS.read().unwrap().as_ref().unwrap().iter() {
          hook(&app, &event, o.clone());
        }
      }
    }
        
    if b { thread::sleep(dur); }
  }
}

fn timer_loop() {
  let system = DataStore::globals().get_object("system");
  let dur = Duration::from_millis(1000);
  while system.get_boolean("running") {
    let now = time();
    let mut timers = system.get_object("timers");
    for (id, timer) in timers.objects() {
      let mut timer = timer.object();
      let when = timer.get_int("startmillis");
      if when <= now {
        let cmdid = timer.get_string("cmd");
        let db = timer.get_string("cmddb");
        if Command::exists(&db, &cmdid) {
          timers.remove_property(&id);
          let params = timer.get_object("params");
          let repeat = timer.get_boolean("repeat");
          let mut ts = timers.clone();
          thread::spawn(move || {
            let cmd = Command::new(&db, &cmdid);
            let _x = cmd.execute(params).unwrap();
              
            if repeat {
              let next = now + timer.get_int("intervalmillis");
              timer.put_int("startmillis", next);
              ts.put_object(&id, timer);
            }
          });
        }
        else { 
          println!("Event command {}:{} not found, removing", &db, &cmdid); 
          timers.remove_property(&id);
        }
      }
    }
    
    thread::sleep(dur);
  }
}

pub fn load_library(j:&str) {
  let store = DataStore::new();
  let system = DataStore::globals().get_object("system");
  let path = store.root.join(j).join("meta.json");
  if path.exists() {
    let s = fs::read_to_string(&path).unwrap();
    let mut o2 = DataObject::from_string(&s);

    let mut readers = DataArray::new();
    let mut writers = DataArray::new();
    if o2.has("readers") { 
    for r in o2.get_array("readers").objects() { readers.push_string(&(r.string())); }
    }
    if o2.has("writers") { 
    for w in o2.get_array("writers").objects() { writers.push_string(&(w.string())); }
    }
    o2.put_array("readers", readers);
    o2.put_array("writers", writers);
    o2.put_string("id", j);

    let mut libraries = system.get_object("libraries");
    libraries.put_object(j, o2);
  }
}

pub fn save_config(config: DataObject) {
  let mut config = config.deep_copy();
  if !config.has("security") { config.put_string("security", "on"); }
  else { 
    let b = config.get_boolean("security");
    if b { config.put_string("security", "on"); } 
    else { config.put_string("security", "off"); } 
  }
  write_properties("config.properties".to_string(), config);
}

pub fn load_config() -> DataObject {
  println!("Loading appserver configuration");
  let mut b = false;
  let mut config;
  if Path::new("config.properties").exists() {
    config = read_properties("config.properties".to_string());
  }
  else { config = DataObject::new(); b = true; }
  
  if !config.has("security") { config.put_boolean("security", true); b = true; }
  else { 
    let b = config.get_string("security") == "on";
    config.put_boolean("security", b); 
    if !b { println!("Warning! Security is OFF!"); }
  }
  
  if !config.has("http_address") { config.put_string("http_address", "0.0.0.0"); b = true; }
  if !config.has("http_port") { config.put_string("http_port", "0"); }
  
  if config.has("sessiontimeoutmillis") { 
    let d = config.get_property("sessiontimeoutmillis");
    if !d.is_int() { 
      let session_timeout = d.string().parse::<i64>().unwrap(); 
      config.clone().put_int("sessiontimeoutmillis", session_timeout);
    }
  }
  else { 
    let session_timeout = 900000; 
    config.clone().put_int("sessiontimeoutmillis", session_timeout);
    b = true;
  }
  
  if !config.has("apps") {
    // FIXME - scan for app.properties files
    config.put_string("apps", "app,dev,peer,security");
    b = true;
  }
  
  if !config.has("default_app") {
    config.put_string("default_app", "app");
    b = true;
  }
  
  if !config.has("machineid") {
    config.put_string("machineid", "MY_DEVICE");
    b = true;
  }
  
  if b { save_config(config.clone()); }
  
  let mut system = DataStore::globals().get_object("system");
  system.put_object("config", config.clone());
  config
}

pub fn init_globals() -> DataObject {
  let mut globals = DataStore::globals();
  
  let mut system;
  let first_time;
  if globals.has("system") { system = globals.get_object("system"); first_time = false; }
  else {
    system = DataObject::new();
    globals.put_object("system", system.clone());
    first_time = true;
  }

  let config = load_config();
  
  if first_time {
    system.put_object("timers", DataObject::new());
    system.put_object("events", DataObject::new());
    system.put_array("fire", DataArray::new());
  }
  
  let s = config.get_string("apps");
  let s = s.trim().to_string();
  let sa = s.split(",");
  
  let mut apps = DataObject::new();
  let default_app;
  if config.has("default_app") { default_app = config.get_string("default_app"); }
  else { default_app = sa.to_owned().nth(0).unwrap().to_string(); }
  
  let libraries = DataObject::new();
  system.put_object("libraries", libraries.clone());
  
  for i in sa {
    let mut o = DataObject::new();
    o.put_string("id", i);
    let path_base = "runtime/".to_string()+i+"/";
    let path = path_base.to_owned()+"botd.properties";
    let p;
    let ppath = Path::new(&path);
    if ppath.exists(){ p = read_properties(path); } 
    else { 
      let _x = File::create(ppath).unwrap();
      p = DataObject::new(); 
    }
    o.put_object("runtime", p);
    let path = path_base+"app.properties";
    let p;
    let ppath = Path::new(&path);
    if ppath.exists(){ p = read_properties(path); } 
    else {
      let _x = File::create(ppath).unwrap();
      p = DataObject::new(); 
    }
    o.put_object("app", p.clone());
    apps.put_object(i, o);
    
    let s = p.get_string("libraries");
    let sa2 = s.split(",");
    for j in sa2 {
      if !libraries.has(j) { load_library(j); }
    }
  }
  
  system.put_string("default_app", &default_app);
  system.put_object("apps", apps);
  system.put_boolean("running", true);
  
  let store = DataStore::new();

  if first_time {
      system.put_object("sessions", DataObject::new());
      
      // Init Timers and Events
      for lib in libraries.clone().keys() {
        let controls = store.get_data(&lib, "controls").get_object("data").get_array("list");
        for ctldata in controls.objects() {
          let ctldata = ctldata.object();
          let ctlid = ctldata.get_string("id");
          let ctlname = ctldata.get_string("name");
          let ctl = store.get_data(&lib, &ctlid).get_object("data");
          if ctl.has("timer") {
            let ctimers = ctl.get_array("timer");
            for timer in ctimers.objects() {
              let timer = timer.object();
              let tname = timer.get_string("name");
              let tid = timer.get_string("id");
              let mut tdata = store.get_data(&lib, &tid).get_object("data");
              tdata.put_string("ctlname", &ctlname);
              tdata.put_string("name", &tname);
              if !tdata.has("start") { println!("Timer {}:{}:{} is not properly configured.", lib, ctlname, tname); }
              else { add_timer(&tid, tdata); }
            }
          }
          if ctl.has("event") {
            let cevents = ctl.get_array("event");
            for event in cevents.objects() {
              let event = event.object();
              let eid = event.get_string("id");
              let edata = store.get_data(&lib, &eid).get_object("data");
              if !edata.has("bot") { println!("Event listener {}:{}:{} is not properly configured.", lib, ctlname, event.get_string("name")); }
              else {
                let app = edata.get_string("bot");
                let ename = edata.get_string("event");
                let cmddb = edata.get_string("cmddb");
                let cmdid = edata.get_string("cmd");
                add_event_listener(&eid, &app, &ename, &cmddb, &cmdid);
              }
            }
          }
        }
      }
  }
  
  let p = store.root;
  for file in fs::read_dir(&p).unwrap() {
    let path = file.unwrap().path();
    if path.is_dir() {
      let name:String = path.file_name().unwrap().to_str().unwrap().to_string();
      if !libraries.has(&name) { load_library(&name); }
    }  
  }
  
  system
}

fn to_millis(i:i64, s:String) -> i64 {
  if s == "milliseconds" { return i; }
  let i = i * 1000;
  if s == "seconds" { return i; }
  let i = i * 60;
  if s == "minutes" { return i; }
  let i = i * 60;
  if s == "hours" { return i; }
  let i = i * 24;
  if s != "days" { panic!("Unknown time unit for timer ({})", &s); }
  
  i
}

pub fn lookup_command_id(app:String, cmd: String) -> (bool, String, String) {
  let system = DataStore::globals().get_object("system");
  let mut b = false;
  let mut ctldb = "".to_string();
  let mut id = "".to_string();
  let apps = system.get_object("apps");
  if apps.has(&app) {
    let appdata = apps.get_object(&app).get_object("app");
    ctldb = appdata.get_string("ctldb");
    let ctlid = appdata.get_string("ctlid");
    let store = DataStore::new();
    let ctllist = store.get_data(&ctldb, &ctlid).get_object("data");
    if ctllist.has("cmd") {
      let ctllist = ctllist.get_array("cmd");
      for ctl in ctllist.objects() {
        let ctl = ctl.object();
        let name = ctl.get_string("name");
        if name == cmd {
          b = true;
          id = ctl.get_string("id");
          break;
        }
      }
      }
  }
  (b, ctldb, id)
}