Skip to content

Commit

Permalink
add a solved version of devices/crunch
Browse files Browse the repository at this point in the history
  • Loading branch information
avsm committed Sep 21, 2011
1 parent df05022 commit 8378eae
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
65 changes: 65 additions & 0 deletions examples/devices/crunch_solved/filesystem_static.ml
@@ -0,0 +1,65 @@
(* This file has been autogenerated by mir-crunch *)
module Internal = struct
let file_chunks = function
| "bar" | "/bar" -> Some ["67890\n" ]
| "foo" | "/foo" -> Some ["12345\n" ]
| _ -> None

let file_list = ["foo"; "bar"; ]
let size = function
|"foo" |"/foo" -> Some 6L
|"bar" |"/bar" -> Some 6L
|_ -> None

end

let name="myblock"

open Lwt

exception Error of string

let iter_s fn = Lwt_list.iter_s fn Internal.file_list

let size name = return (Internal.size name)

let read name =
match Internal.file_chunks name with
|None -> return None
|Some c ->
let chunks = ref c in
return (Some (Lwt_stream.from (fun () ->
match !chunks with
|hd :: tl ->
chunks := tl;
return (Some (Bitstring.bitstring_of_string hd))
|[] -> return None
)))

let create vbd : OS.Devices.kv_ro Lwt.t =
return (object
method iter_s fn = iter_s fn
method read name = read name
method size name = size name
end)

let _ =
let plug = Lwt_mvar.create_empty () in
let unplug = Lwt_mvar.create_empty () in
let provider = object(self)
method id = name
method plug = plug
method unplug = unplug
method create ~deps ~cfg id =
Lwt.bind (create id) (fun kv ->
let entry = OS.Devices.({
provider=self;
id=self#id;
depends=[];
node=KV_RO kv }) in
return entry
)
end in
OS.Devices.new_provider provider;
OS.Main.at_enter (fun () -> Lwt_mvar.put plug {OS.Devices.p_id=name; p_dep_ids=[]; p_cfg=[]})

2 changes: 2 additions & 0 deletions examples/devices/crunch_solved/kv_fs.mir
@@ -0,0 +1,2 @@
Server.main
Block.SimpleKV
17 changes: 17 additions & 0 deletions examples/devices/crunch_solved/server.ml
@@ -0,0 +1,17 @@
open Lwt
open Printf

let main () =
printf "Plugging device\n%!";
lwt kv_ro = OS.Devices.with_kv_ro "myblock" return in
printf "Reading file foo\n%!";
match_lwt kv_ro#read "foo" with
|Some s ->
printf "File contents:\n%!";
Lwt_stream.iter (fun b ->
printf "%s%!" (Bitstring.string_of_bitstring b);
) s
|None ->
printf "File not found\n%!";
return ()

1 change: 1 addition & 0 deletions examples/devices/crunch_solved/static/bar
@@ -0,0 +1 @@
67890
1 change: 1 addition & 0 deletions examples/devices/crunch_solved/static/foo
@@ -0,0 +1 @@
12345

0 comments on commit 8378eae

Please sign in to comment.