]> xenbits.xensource.com Git - xenclient/toolstack.git/commitdiff
Avoid repeating response_handlers that are reused; fix naming of generated response_h...
authorPrashanth Mundkur <prashanth.mundkur@citrix.com>
Wed, 3 Jun 2009 22:19:10 +0000 (15:19 -0700)
committerPrashanth Mundkur <prashanth.mundkur@citrix.com>
Tue, 23 Jun 2009 16:24:34 +0000 (09:24 -0700)
gen/rpc/codegen.ml

index e0c40e88c6b45fc6066aafb6baa1c56d09762ab5..fc7950a6f0159c0125872831729bbbe26da37fd6 100644 (file)
@@ -157,7 +157,6 @@ module Server = struct
                                                                let a, venv = gen_param ff venv otvn i p in
                                                                (a :: alist), venv, (i + 1)
                                                       ) ([], venv, 0) params in
-
                                (String.concat " " (List.map (fun v -> name_of_var v) (List.rev paramsv))), venv
                        )
                in
@@ -251,16 +250,22 @@ module Client = struct
 
        let client_modname s = Printf.sprintf "%s_client" (String.capitalize s.server_name)
 
-       let gen_resp_name rpc =
-               Printf.sprintf "jresp_%s" (gen_method_name rpc.rpc_request.request_name)
+       let gen_resp_name resp =
+               Printf.sprintf "jresp_%s" (gen_method_name resp.response_handler)
 
        let generate_client_sig ff s rpcs =
                fprintf ff "module type %s =@\n" (client_modname s);
                fprintf ff "@[<v 8>sig";
                fprintf ff "@,type rpc_id";
-               fprintf ff "@,type context";
-               fprintf ff "@,val get_new_rpc_id : unit -> rpc_id * Json.t";
-               List.iter (fun (rpc, resp) -> gen_handler_type ff resp) rpcs;
+               fprintf ff "@,type context@,";
+               fprintf ff "@,val get_new_rpc_id : unit -> rpc_id * Json.t@,";
+               ignore (List.fold_left (fun acc (rpc, resp) ->
+                                               if List.mem resp.response_handler acc then acc
+                                               else begin
+                                                       gen_handler_type ff resp;
+                                                       resp.response_handler :: acc
+                                               end
+                                      ) [] rpcs);
                fprintf ff "@]@\nend@\n@\n@?"
 
        let start_maker ff s =
@@ -272,11 +277,20 @@ module Client = struct
        let end_maker ff =
                fprintf ff "@]@\nend@\n@\n@?"
 
-       let generate_resp_handler ff modname c (rpc, resp) =
-               fprintf ff "@,@[<v 8>let %s resp =@," (gen_resp_name rpc);
+       let gen_resp_handler ff modname c (rpc, resp) =
+               fprintf ff "@,@[<v 8>let %s resp =@," (gen_resp_name resp);
                fprintf ff "%s.%s (%s resp)@]" modname resp.response_handler
                        (Type_conv.of_json resp.response_value.param_type)
 
+       let generate_resp_handlers ff modname c rpc_list =
+               ignore (List.fold_left (fun acc (rpc, resp) ->
+                                               if List.mem resp.response_handler acc then acc
+                                               else begin
+                                                       gen_resp_handler ff modname c (rpc, resp);
+                                                       resp.response_handler :: acc
+                                               end
+                                      ) [] rpc_list)
+
        let generate_rpc ff venv modname s rpc =
                let params = rpc.rpc_request.request_params in
                let args = List.map (fun p -> p.param_name) params in
@@ -314,7 +328,7 @@ module Client = struct
                fprintf ff "Jsonrpc.params = Json.Object (Array.of_list [ %s ])" args_str;
                (match rpc.rpc_response with
                 | None -> fprintf ff "@]@,}@]"
-                | Some _ -> fprintf ff "@]@,}, %s, %s@]" rpcvn (gen_resp_name rpc)
+                | Some resp -> fprintf ff "@]@,}, %s, %s@]" rpcvn (gen_resp_name resp)
                )
 end
 
@@ -351,7 +365,7 @@ let generate_client ff spec c =
        let modname = Client.start_maker ff c in
        if List.length rpc_list > 0 then
                fprintf ff "@,(* Response handling *)@,";
-       List.iter (Client.generate_resp_handler ff modname c) rpc_list;
+       Client.generate_resp_handlers ff modname c rpc_list;
        fprintf ff "@,";
        if List.length msg_list > 0 then
                fprintf ff "@,(* Requests and notifications *)@,";