From: Jérémie D. <Ba...@us...> - 2011-03-17 17:33:40
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "krobot". The branch, master has been updated via 0ef1a91b2e2b76850c49e886509582c29fedca10 (commit) via e45ba7e950de7c8e47e1fb22fe65fe5d70f5c9d0 (commit) via 79daa52c3f1ca70c351dc1e962fdadf65572034a (commit) via e349b0939341e36863161e6ec90fb2e4456aaf54 (commit) via 4507dff68656ad0300ed2138a58f694f7889ee1f (commit) from f5ee1deaeab0135564b27e85f3b72ca253d45831 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 0ef1a91b2e2b76850c49e886509582c29fedca10 Merge: e349b0939341e36863161e6ec90fb2e4456aaf54 e45ba7e950de7c8e47e1fb22fe65fe5d70f5c9d0 Author: Jérémie Dimino <je...@di...> Date: Thu Mar 17 18:32:38 2011 +0100 Merge commit 'e45ba7e950de7c8e47e1fb22fe65fe5d70f5c9d0' commit e45ba7e950de7c8e47e1fb22fe65fe5d70f5c9d0 Author: Jérémie Dimino <je...@di...> Date: Thu Mar 17 18:30:45 2011 +0100 [info] add modules Krobot_service and Krobot_init commit 79daa52c3f1ca70c351dc1e962fdadf65572034a Author: Jérémie Dimino <je...@di...> Date: Thu Mar 17 10:06:01 2011 +0100 [info] use the environment variable DBUS_KROBOT_BUS_ADDRESS to get the address of the krobot bus commit e349b0939341e36863161e6ec90fb2e4456aaf54 Author: Jérémie Dimino <je...@di...> Date: Thu Mar 17 09:58:41 2011 +0100 Revert "[info] do not send back messages comming from the can in the driver" This reverts commit 4507dff68656ad0300ed2138a58f694f7889ee1f. commit 4507dff68656ad0300ed2138a58f694f7889ee1f Author: Jérémie Dimino <je...@di...> Date: Thu Mar 17 00:37:16 2011 +0100 [info] do not send back messages comming from the can in the driver ----------------------------------------------------------------------- Changes: diff --git a/info/control2011/_oasis b/info/control2011/_oasis index 9e15987..ad461ed 100644 --- a/info/control2011/_oasis +++ b/info/control2011/_oasis @@ -28,7 +28,9 @@ Library krobot Modules: Krobot_can, Krobot_bus, - Krobot_message + Krobot_message, + Krobot_service, + Krobot_init Library "krobot-can" FindlibName: can diff --git a/info/control2011/krobot-api.odocl b/info/control2011/krobot-api.odocl index eb2e004..c80c72a 100644 --- a/info/control2011/krobot-api.odocl +++ b/info/control2011/krobot-api.odocl @@ -1,7 +1,9 @@ # OASIS_START -# DO NOT EDIT (digest: f5cc533adaa505f8bedbd5bafe08f3f0) +# DO NOT EDIT (digest: 979bfe64930f512bd800c4336aa992aa) src/lib/Krobot_can src/lib/Krobot_bus src/lib/Krobot_message +src/lib/Krobot_service +src/lib/Krobot_init src/can/Krobot_can_bus # OASIS_STOP diff --git a/info/control2011/setup.ml b/info/control2011/setup.ml index 51de5db..aa37ba5 100644 --- a/info/control2011/setup.ml +++ b/info/control2011/setup.ml @@ -1,9 +1,9 @@ (* setup.ml generated for the first time by OASIS v0.2.0~alpha1 *) (* OASIS_START *) -(* DO NOT EDIT (digest: 3c3d5a057069def7d5ccec6bb9cbfeaa) *) +(* DO NOT EDIT (digest: c4a37702729314af3884a7f930148eae) *) (* - Regenerated by OASIS v0.2.0~alpha1 + Regenerated by OASIS v0.2.0 Visit http://oasis.forge.ocamlcore.org for more information and documentation about functions used in this file. *) @@ -5101,7 +5101,13 @@ let setup_t = }, { lib_modules = - ["Krobot_can"; "Krobot_bus"; "Krobot_message"]; + [ + "Krobot_can"; + "Krobot_bus"; + "Krobot_message"; + "Krobot_service"; + "Krobot_init" + ]; lib_internal_modules = []; lib_findlib_parent = None; lib_findlib_name = Some "krobot"; @@ -5299,7 +5305,7 @@ let setup_t = schema_data = PropList.Data.create (); plugin_data = []; }; - version = "0.2.0~alpha1"; + version = "0.2.0"; };; let setup () = BaseSetup.setup setup_t;; diff --git a/info/control2011/src/driver/krobot_driver.ml b/info/control2011/src/driver/krobot_driver.ml index 2883b33..fe7af7f 100644 --- a/info/control2011/src/driver/krobot_driver.ml +++ b/info/control2011/src/driver/krobot_driver.ml @@ -12,17 +12,15 @@ open Lwt open Lwt_react +let device = ref "slcan0" + +let () = Krobot_init.arg "-device" (Arg.Set_string device) "<device> The device to use" + lwt () = - if Array.length Sys.argv <> 2 then begin - print_endline "usage: krobot-driver <interface>"; - exit 2; - end; + lwt bus = Krobot_init.init_service "Driver" in (* Open the CAN bus. *) - lwt can = Krobot_can_bus.open_can Sys.argv.(1) in - - (* Open the D-Bus connection. *) - lwt bus = Krobot_bus.get () in + lwt can = Krobot_can_bus.open_can !device in (* D-Bus --> CAN *) E.keep (E.map_s (Krobot_can_bus.send can) (Krobot_can.recv bus)); diff --git a/info/control2011/src/interfaces/krobot_interface_service.obus b/info/control2011/src/interfaces/krobot_interface_service.obus new file mode 100644 index 0000000..6f5e940 --- /dev/null +++ b/info/control2011/src/interfaces/krobot_interface_service.obus @@ -0,0 +1,20 @@ +(* + * krobot_interface_service.obus + * ----------------------------- + * Copyright : (c) 2011, Jeremie Dimino <je...@di...> + * Licence : BSD3 + * + * This file is a part of [kro]bot. + *) + +(** Interface for krobot services. *) +interface fr.krobot.Service { + (** Make the service to exit. *) + method kill : () -> () + with { + org.freedesktop.DBus.Method.NoReply = "true" + } + + signal log : (message : string) + (** Signal emitted when the service emit a log message. *) +} diff --git a/info/control2011/src/lib/krobot.mllib b/info/control2011/src/lib/krobot.mllib index b7f4d81..a197c9c 100644 --- a/info/control2011/src/lib/krobot.mllib +++ b/info/control2011/src/lib/krobot.mllib @@ -1,6 +1,8 @@ # OASIS_START -# DO NOT EDIT (digest: b3bccfeb9433d08b24a1724cd101e42d) +# DO NOT EDIT (digest: 28c0fb0dd216f4e31f545770f08d4c64) Krobot_can Krobot_bus Krobot_message +Krobot_service +Krobot_init # OASIS_STOP diff --git a/info/control2011/src/lib/krobot_bus.ml b/info/control2011/src/lib/krobot_bus.ml index 18a1352..f6e6112 100644 --- a/info/control2011/src/lib/krobot_bus.ml +++ b/info/control2011/src/lib/krobot_bus.ml @@ -7,11 +7,27 @@ * This file is a part of [kro]bot. *) +let section = Lwt_log.Section.make "bus" + type t = OBus_bus.t external of_bus : OBus_bus.t -> t = "%identity" external to_bus : t -> OBus_bus.t = "%identity" -let bus = lazy(OBus_bus.of_addresses [OBus_address.make "unix" [("abstract", "krobot")]]) +let address = ref ( + match try Some(Sys.getenv "DBUS_KROBOT_BUS_ADDRESS") with Not_found -> None with + | Some addr -> + addr + | None -> + "unix:abstract=krobot" +) + +let bus = lazy( + try_lwt + OBus_bus.of_addresses (OBus_address.of_string !address) + with exn -> + lwt () = Lwt_log.error_f ~section ~exn "failed to connect to the krobot bus at address '%s'" !address in + raise_lwt exn +) let get () = Lazy.force bus diff --git a/info/control2011/src/lib/krobot_bus.mli b/info/control2011/src/lib/krobot_bus.mli index 3fe3946..4c94893 100644 --- a/info/control2011/src/lib/krobot_bus.mli +++ b/info/control2011/src/lib/krobot_bus.mli @@ -17,3 +17,7 @@ external to_bus : t -> OBus_bus.t = "%identity" val get : unit -> t Lwt.t (** [get ()] returns the krobot bus. *) + +(**/**) + +val address : string ref diff --git a/info/control2011/src/lib/krobot_init.ml b/info/control2011/src/lib/krobot_init.ml new file mode 100644 index 0000000..65ddc81 --- /dev/null +++ b/info/control2011/src/lib/krobot_init.ml @@ -0,0 +1,140 @@ +(* + * krobot_init.ml + * -------------- + * Copyright : (c) 2011, Jeremie Dimino <je...@di...> + * Licence : BSD3 + * + * This file is a part of [kro]bot. + *) + +open Lwt +open Lwt_react + +let section = Lwt_log.Section.make "init" + +(* +-----------------------------------------------------------------+ + | Misc | + +-----------------------------------------------------------------+ *) + +let () = + Printexc.register_printer + (function + | Unix.Unix_error(error, func, "") -> + Some(Printf.sprintf "%s: %s" func (Unix.error_message error)) + | Unix.Unix_error(error, func, arg) -> + Some(Printf.sprintf "%s(%S): %s" func arg (Unix.error_message error)) + | _ -> + None) + +let program_name = Filename.basename Sys.executable_name +let usage = Printf.sprintf "%s <options>" program_name + +(* +-----------------------------------------------------------------+ + | Command line arguments | + +-----------------------------------------------------------------+ *) + +let arguments = ref [] +let arg key spec doc = arguments := (key, spec, doc) :: !arguments + +let () = arg "-dbus-address" (Arg.Set_string Krobot_bus.address) "<address> The address to connect to the krobot bus" + +(* +-----------------------------------------------------------------+ + | Programs | + +-----------------------------------------------------------------+ *) + +let init_program name = + Arg.parse (Arg.align !arguments) (fun _ -> raise (Arg.Bad "unknown option")) usage; + lwt () = Lwt_log.info ~section "openning the robot bus" in + try_lwt + Krobot_bus.get () + with exn -> + exit 1 + +(* +-----------------------------------------------------------------+ + | Services | + +-----------------------------------------------------------------+ *) + +open Krobot_interface_service.Fr_krobot_Service + +let init_service name = + let no_fork = ref false and kill = ref false in + arg "-no-fork" (Arg.Set no_fork) " Run in foreground"; + arg "-kill" (Arg.Set kill) " Kill any running instance and exit"; + Arg.parse (Arg.align !arguments) (fun _ -> raise (Arg.Bad "unknown option")) usage; + + lwt () = Lwt_log.info ~section "openning the robot bus" in + lwt bus = try_lwt Krobot_bus.get () with exn -> exit 1 in + + let dbus_name = Printf.sprintf "fr.krobot.Service.%s" name in + + (* Kill other instances. *) + lwt () = + if !kill then begin + try_lwt + lwt owner = OBus_bus.get_name_owner (Krobot_bus.to_bus bus) dbus_name in + lwt () = Lwt_log.info_f ~section "killing the running %s service" name in + Krobot_service.kill (Krobot_service.of_proxy (OBus_proxy.make (OBus_peer.make (Krobot_bus.to_bus bus) owner) ["fr"; "krobot"; "Service"])) + with OBus_bus.Name_has_no_owner _ -> + return () + finally + exit 0 + end else + return () + in + + (* Exit the program when we lost the name. *) + lwt () = + OBus_signal.connect (OBus_bus.name_lost (Krobot_bus.to_bus bus)) + >|= E.map (fun lost_name -> if dbus_name = lost_name then exit 0) + >|= E.keep + in + + (* Daemonize or not. *) + lwt () = + if !no_fork then + Lwt_log.notice_f ~section "starting %s in foreground mode" name + else begin + lwt () = Lwt_log.notice_f ~section "starting %s in daemon mode" name in + Lwt_daemon.daemonize (); + return () + end + in + + (* Create the service object and export it. *) + let obj = OBus_object.make ~interfaces:[make { m_kill = (fun obj () -> exit 0) }] ["fr"; "krobot"; "Service"] in + OBus_object.attach obj (); + OBus_object.export (Krobot_bus.to_bus bus) obj; + + (* Send logs over D-Bus. *) + let dbus_logger = + Lwt_log.make + (fun section level lines -> + if level > Lwt_log.Info then + let buf = Buffer.create 42 in + let lines = + List.map + (fun line -> + Buffer.clear buf; + Lwt_log.render ~buffer:buf ~template:"$(level)@$(name)[$(section)]: $(message)" ~section ~level ~message:line; + Buffer.contents buf) + lines + in + OBus_signal.emit s_log obj (String.concat "\n" lines) + else + return ()) + return + in + + Lwt_log.default := Lwt_log.broadcast [!Lwt_log.default; dbus_logger]; + + (* Register the program on the bus. *) + lwt () = + OBus_bus.request_name (Krobot_bus.to_bus bus) ~allow_replacement:true ~replace_existing:true dbus_name >>= function + | `Primary_owner -> + return () + | _ -> + lwt () = Lwt_log.error_f ~section "cannot obtain the name %S on D-Bus" dbus_name in + exit 1 + in + + return bus diff --git a/info/control2011/src/lib/krobot_init.mli b/info/control2011/src/lib/krobot_init.mli new file mode 100644 index 0000000..38457cb --- /dev/null +++ b/info/control2011/src/lib/krobot_init.mli @@ -0,0 +1,24 @@ +(* + * krobot_init.mli + * --------------- + * Copyright : (c) 2011, Jeremie Dimino <je...@di...> + * Licence : BSD3 + * + * This file is a part of [kro]bot. + *) + +(** Initialization *) + +val arg : Arg.key -> Arg.spec -> Arg.doc -> unit + (** [arg key spec doc] registers the given command line argument. *) + +val init_program : string -> Krobot_bus.t Lwt.t + (** [init_program name] initialises a program. A program always run + in foreground. *) + +val init_service : string -> Krobot_bus.t Lwt.t + (** [init_service name] initialises a service. A service may run in + foreground or in background according to command line arguments. + This module ensure that only one instance of the service is + running at a time. It also export a simple interface over + D-Bus. *) diff --git a/info/control2011/src/lib/krobot_service.ml b/info/control2011/src/lib/krobot_service.ml new file mode 100644 index 0000000..784664d --- /dev/null +++ b/info/control2011/src/lib/krobot_service.ml @@ -0,0 +1,60 @@ +(* + * krobot_service.ml + * ----------------- + * Copyright : (c) 2011, Jeremie Dimino <je...@di...> + * Licence : BSD3 + * + * This file is a part of [kro]bot. + *) + +include OBus_proxy.Private + +open Lwt +open Lwt_react +open Krobot_interface_service.Fr_krobot_Service + +let all bus = + OBus_proxy.make + (OBus_peer.anonymous (Krobot_bus.to_bus bus)) + ["fr"; "krobot"; "Service"] + +let make bus name = + if name = "" then + all bus + else + OBus_proxy.make + (OBus_peer.make + (Krobot_bus.to_bus bus) + (Printf.sprintf "fr.krobot.Service.%s" name)) + ["fr"; "krobot"; "Service"] + +let name service = + let s = OBus_proxy.name service in + let i = String.rindex s '.' in + String.sub s (i + 1) (String.length s - (i + 1)) + +let list bus = + lwt names = OBus_bus.list_names (Krobot_bus.to_bus bus) in + return + (List.map + (fun name -> + OBus_proxy.make + (OBus_peer.make (Krobot_bus.to_bus bus) name) + ["fr"; "krobot"; "Service"]) + (List.sort + String.compare + (List.filter + (fun name -> + String.sub name 0 (String.rindex name '.') = "fr.krobot.Service") + names))) + +let monitor bus = + lwt e = OBus_signal.connect (OBus_bus.name_owner_changed (Krobot_bus.to_bus bus)) in + lwt l = list bus in + return (S.hold l (E.map_s (fun _ -> list bus) e)) + +let kill service = + OBus_method.call_no_reply m_kill service () + +let log service = + OBus_signal.make s_log service diff --git a/info/control2011/src/lib/krobot_service.mli b/info/control2011/src/lib/krobot_service.mli new file mode 100644 index 0000000..e04fac4 --- /dev/null +++ b/info/control2011/src/lib/krobot_service.mli @@ -0,0 +1,34 @@ +(* + * krobot_service.mli + * ------------------ + * Copyright : (c) 2011, Jeremie Dimino <je...@di...> + * Licence : BSD3 + * + * This file is a part of [kro]bot. + *) + +include OBus_proxy.Private + +val make : Krobot_bus.t -> string -> t + (** [make bus name pid] creates a service proxy from its name. *) + +val all : Krobot_bus.t -> t + (** Proxy that represent all running services. You can use it to + receive logs from all services. *) + +val name : t -> string + (** Returns the name of the given service. *) + +val list : Krobot_bus.t -> t list Lwt.t + (** List all available services on the bus. *) + +val monitor : Krobot_bus.t -> t list React.signal Lwt.t + (** Returns the signal holding the sorted list of services on the + bus. *) + +val kill : t -> unit Lwt.t + (** [kill service] kills the given service. *) + +val log : t -> string OBus_signal.t + (** [log service] is the D-Bus signal which occurs when [service] + emit a log message. *) hooks/post-receive -- krobot |