The previous code was messy all over the place. This one seems to work, although I'm not sure whether it can keep the services securely sandboxed as intented and won't run out of memory at some point.
Revised code:
-----

#lang racket
; tested in Linux
(define target-mode #f); #f/ "service-1"/ "service-2"

(define (service-switcher)
  (define current-mode #f)
  (define cust (make-custodian))
  (define (system/output command)
    (define (system/exit-code+output command)
      (let* ([out (open-output-string)]
             [control (process/ports out #f #f command)]
             [p (list-ref control 4)])
        (p 'wait)
        (values (p 'exit-code)
                (get-output-string out))))
    (let-values ([(e out) (system/exit-code+output command)])
      out))
  (define (switch-mode)
    (set! cust (make-custodian))
    (parameterize ((current-custodian cust)
                   (current-namespace (make-base-namespace)))
      (current-subprocess-custodian-mode 'kill)
      (subprocess-group-enabled #t)
      (cond ((equal? target-mode #f)
             (begin (custodian-shutdown-all cust)
                    (set! current-mode #f)
                    (newline)(display "Service disabled")(newline)))
            ((and (equal? target-mode "service-1"))
             (begin (set! current-mode "service-1")
(newline)(display "Service mode changed to Service-1")(newline)
                    (thread (lambda () (system/output "gedit")))))
            ((and (equal? target-mode "service-2"))
(begin (newline)(display "Service mode changed to Service-2")(newline)
                    (set! current-mode "service-2")
(thread (lambda () (system/output "gcalctool"))))))))
  (define (loop)
    (if (equal? current-mode target-mode)
        (begin (display ".")
               (sleep 1)
               (loop))
        (begin (custodian-shutdown-all cust)
               (switch-mode)
               (loop))))
   (thread (lambda () (loop)))
  (display "Service switcher started.")(newline))


;; TESTING

(service-switcher)

(define awhile 5)

(sleep awhile)
(set! target-mode "service-1")
(sleep awhile)
(set! target-mode #f)
(sleep awhile)
(set! target-mode "service-2")
(sleep awhile)
(set! target-mode "service-1")
(sleep awhile)
(set! target-mode #f)
(sleep awhile)
(newline)(display "Test finished (server still running on the background. Press CTRL+K to stop it)")(newline)

------


jukka.tuomi...@finndesign.fi kirjoitti 2015-12-12 11:40:
Hi all,

I want to make a custodian-protected, remotely-controlled service
switcher but there's something wrong with my code. The behaviour isn't
reliable and it leaves traces behind filling the memory eventually.
I've included the code below.

TBH, I may have a less optimal approach to it in the first place, so
I'd appreciate any better suggestions.
A little background: As part of Liitin (liitin.org) IoT development
and dog-fooding, I've decided to make my personal A.I. project running
on a remotely-controllable Liitin node as a "Iot Service". Any nodes
fired up will be automagically visible on my Liitin desktop. Any A.I.
node or the user node can be behind different firewalls without open
ports. Neither do they need to know each other's whereabouts. One A.I.
node is to work as an interactive service whereas the rest will take
part in incubating higher intelligence through genetic programming. I
have a simple IoT communication channel in place, but now I want a
reliable means to switch the service mode remotely. I don't want any
ill-behaved A.I. processes running wild either, so I want to make sure
I can shut down the processes remotely when I want to, so the services
will need to be sandboxed. Foremost this is an IoT dog-fooding
excercise, trying to make any Liitin SW or HW node as easy and secure
to use as possible for the basic users, now only taking it to one
extreme.

Finally to the switcher source code...

br, jukka

---

--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to