diff options
| -rw-r--r-- | sources/thibaut/Makefile | 9 | ||||
| -rw-r--r-- | sources/thibaut/client.ml | 63 |
2 files changed, 70 insertions, 2 deletions
diff --git a/sources/thibaut/Makefile b/sources/thibaut/Makefile index 6eb3c92..c963280 100644 --- a/sources/thibaut/Makefile +++ b/sources/thibaut/Makefile @@ -3,18 +3,23 @@ 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 +SRCS=simulator.ml trace.ml gentrace.ml pacemaker.ml globals.ml compattrace.ml tracestats.ml mesh.ml graph.ml client.ml BUILDDIR=build DEPEND=.depend LIBS=str unix LIBSOPT=$(addsuffix .cmxa, $(LIBS)) -.PHONY: gentrace.opt simulator.opt tracestats.opt all clean +.PHONY: gentrace.opt simulator.opt tracestats.opt all clean client all: gentrace.opt simulator.opt tracestats.opt $(BUILDDIR): mkdir -p $(BUILDDIR) +client: $(BUILDDIR) $(BUILDDIR)/client + +$(BUILDDIR)/client: client.cmx + $(OCAMLOPT) -o $@ $(OCAMLFLAGS) $(LIBSOPT) client.cmx + tracestats.opt: $(BUILDDIR) $(BUILDDIR)/tracestats $(BUILDDIR)/tracestats: tracestats.cmx diff --git a/sources/thibaut/client.ml b/sources/thibaut/client.ml new file mode 100644 index 0000000..9ec0309 --- /dev/null +++ b/sources/thibaut/client.ml @@ -0,0 +1,63 @@ +open Unix + +let local_port = ref 54321 +let max_length = 1000 +let config_file = ref "client.conf" +let version = "0.1" +let server = ref false + +let arg_list = Arg.align [ + "--lport", Arg.Set_int local_port, " <n> local port"; + "--conf", Arg.Set_string config_file, " <filename> configuration file"; + "--server", Arg.Set server, " launch the client as a server" +] + +let usage = Printf.sprintf "usage: %s [OPTIONS]" Sys.argv.(0) + +let _ = + Printf.printf "Pacemaker client v%s\n%!" version; + Arg.parse arg_list (fun _ -> ()) usage; + + let conf_ic = + try + open_in !config_file + with + 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 + + let local_socket = socket PF_INET SOCK_DGRAM 0 in + let my_address = (gethostbyname(gethostname())).h_addr_list.(0) in + + let local_address = ADDR_INET( + my_address, + !local_port + ) in + + let distant_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) + + done |
