let agenda = ref (Array.make 31 []);;

let ctl = create ();;

let execute_query fd =
  begin
    let output v =
      let out_channel = out_channel_of_descr fd in
      output_value out_channel v;
      Pervasives.flush out_channel in
    try
      match input_value (Unix.in_channel_of_descr fdwith
        Get_day n ->
          read_protect ctl output (Day !agenda.(n))
      | Get_agenda ->
          read_protect ctl output (Agenda !agenda)
      | Add_event (name,day,start,finish,info) ->
          let add_event () =
            let event = (name,{start=startfinish=finishinfo=info}) in
            !agenda.(day) <- event::!agenda.(day) ;
            output Unit in
          write_protect ctl add_event ()
      | Delete_event (name,day) ->
          let delete_event () =
            !agenda.(day) <- List.remove_assoc name !agenda.(day);
            output Unit in
          write_protect ctl delete_event ()
    with
      e -> output (Exception e)
  end;
  close fd;;