diff options
| author | thibauth <thibauth@30fcff6e-8de6-41c7-acce-77ff6d1dd07b> | 2011-07-12 16:17:05 +0000 |
|---|---|---|
| committer | thibauth <thibauth@30fcff6e-8de6-41c7-acce-77ff6d1dd07b> | 2011-07-12 16:17:05 +0000 |
| commit | abbdf4a889307d549757c4385f54edcd45cb8fc2 (patch) | |
| tree | 9eee48690c0cd5bd461f28c7ff7582e15afa2c39 /sources | |
| parent | 1860aeb4ad0b4f29c2dbb12978d611ccb1c9447b (diff) | |
| download | pacemaker-abbdf4a889307d549757c4385f54edcd45cb8fc2.tar.gz | |
System interaction is hopefully finished in the client.
git-svn-id: https://scm.gforge.inria.fr/svn/pacemaker@42 30fcff6e-8de6-41c7-acce-77ff6d1dd07b
Diffstat (limited to 'sources')
| -rw-r--r-- | sources/thibaut/Makefile | 4 | ||||
| -rw-r--r-- | sources/thibaut/client.ml | 108 | ||||
| -rw-r--r-- | sources/thibaut/clientGlobals.ml | 15 |
3 files changed, 95 insertions, 32 deletions
diff --git a/sources/thibaut/Makefile b/sources/thibaut/Makefile index c963280..88a23d9 100644 --- a/sources/thibaut/Makefile +++ b/sources/thibaut/Makefile @@ -3,7 +3,7 @@ OCAMLOPT=ocamlopt OCAMLDEP=ocamldep INCLUDES= OCAMLFLAGS=$(INCLUDES) -annot -SRCS=simulator.ml trace.ml gentrace.ml pacemaker.ml globals.ml compattrace.ml tracestats.ml mesh.ml graph.ml client.ml +SRCS=simulator.ml trace.ml gentrace.ml pacemaker.ml globals.ml compattrace.ml tracestats.ml mesh.ml graph.ml client.ml clientGlobals.ml BUILDDIR=build DEPEND=.depend LIBS=str unix @@ -18,7 +18,7 @@ $(BUILDDIR): client: $(BUILDDIR) $(BUILDDIR)/client $(BUILDDIR)/client: client.cmx - $(OCAMLOPT) -o $@ $(OCAMLFLAGS) $(LIBSOPT) client.cmx + $(OCAMLOPT) -o $@ $(OCAMLFLAGS) $(LIBSOPT) clientGlobals.cmx client.cmx tracestats.opt: $(BUILDDIR) $(BUILDDIR)/tracestats diff --git a/sources/thibaut/client.ml b/sources/thibaut/client.ml index 9ec0309..39aea36 100644 --- a/sources/thibaut/client.ml +++ b/sources/thibaut/client.ml @@ -1,63 +1,111 @@ open Unix +open ClientGlobals -let local_port = ref 54321 -let max_length = 1000 +let port = ref 54321 +let max_length = 10000 let config_file = ref "client.conf" let version = "0.1" let server = ref false +let id_file = ref "id.conf" +let generate = ref false let arg_list = Arg.align [ - "--lport", Arg.Set_int local_port, " <n> local port"; + "--port", Arg.Set_int port, " <n> listening port"; "--conf", Arg.Set_string config_file, " <filename> configuration file"; - "--server", Arg.Set server, " launch the client as a server" + "--server", Arg.Set server, " launch the client as a server"; + "--id", Arg.Set_string id_file, " <filename> id file"; + "--genid", Arg.Set generate, " generate the id file" ] let usage = Printf.sprintf "usage: %s [OPTIONS]" Sys.argv.(0) +let process_message s = + let message = Marshal.from_string s 0 in + () + + let _ = Printf.printf "Pacemaker client v%s\n%!" version; Arg.parse arg_list (fun _ -> ()) usage; - + + let my_address = (gethostbyname(gethostname())).h_addr_list.(0) in + + let _ = + if !generate then begin + let id_oc = + try + open_out !id_file + with + Sys_error s -> Printf.eprintf "The file '%s' could not be opened, %s\n%!" + !id_file s; exit 1 + in + Random.self_init (); + Printf.fprintf id_oc "%d" (Random.int 500000000); + Printf.printf "Id generated in file %s\n%!" !id_file; + close_out id_oc; + exit 0 + end + in + + let id_ic = + try + open_in !id_file + with + Sys_error s -> Printf.eprintf "The file '%s' could not be opened, %s\n%!" + !id_file s; exit 1 + in + + let my_id = Scanf.fscanf id_ic "%d" (fun x -> x) in + let conf_ic = try open_in !config_file with - Sys_error s -> Printf.eprintf "The file '%s' could not be opened, %s\n" + Sys_error s -> Printf.eprintf "The file '%s' could not be opened, %s\n%!" !config_file s; exit 1 in let server_address, server_port = - Scanf.fscanf conf_ic "%s\n%d" (fun a b -> a,b) in + Scanf.fscanf conf_ic "%s\n%d" (fun a b -> a,b) + in - let local_socket = socket PF_INET SOCK_DGRAM 0 in - let my_address = (gethostbyname(gethostname())).h_addr_list.(0) in + let _ = close_in conf_ic; close_in id_ic in - let local_address = ADDR_INET( + let socket = socket PF_INET SOCK_DGRAM 0 in + + let address = ADDR_INET( my_address, - !local_port + !port ) in - let distant_address = ADDR_INET( + let server_address = ADDR_INET( inet_addr_of_string server_address, server_port ) in - bind local_socket local_address; - - while true do - - let a,b,c = select [local_socket] [] [] 1. in - match a with - | [] -> () - | t::q -> - let buff = String.create max_length in - let len, addr = recvfrom t buff 0 max_length [] in - let addr_string = match addr with - | ADDR_INET(iaddr,port) -> Printf.sprintf "%s:%d" - (string_of_inet_addr iaddr) port - | _ -> "" - in - Printf.printf "Received from %s: %s\n%!" addr_string - (String.sub buff 0 len) - + let _ = try + bind socket address + with + | Unix_error(e,s1,s2) -> + Printf.eprintf "Could not bind socket on port %d: %s in %s %s\n%!" + !port (error_message e) s1 s2; + exit 1 + in + + while true do + let a,b,c = select [socket] [] [] 1. in + match a with + | [] -> () + | t::q -> + let buff = String.create max_length in + let len, addr = recvfrom t buff 0 max_length [] in + let addr_string = match addr with + | ADDR_INET(iaddr,port) -> Printf.sprintf "%s:%d" + (string_of_inet_addr iaddr) port + | _ -> "" + in + process_message (String.sub buff 0 len); + Printf.printf "Received from %s: %s\n%!" addr_string + (String.sub buff 0 len) + done diff --git a/sources/thibaut/clientGlobals.ml b/sources/thibaut/clientGlobals.ml new file mode 100644 index 0000000..be72604 --- /dev/null +++ b/sources/thibaut/clientGlobals.ml @@ -0,0 +1,15 @@ +open Unix + +type user_id = int + +type message_content = + | Ping + | Mesh + | AskRoot + | AskPeers of sockaddr + | Accepted of user_id + +type message = { + id : user_id; + content : message_content; +} |
