Thrift: OCaml TSocket fix
Summary: Now closes input channel on close. Also, transport exceptions are cleaner.
Reviewed by: mcslee
Test plan: Yes
Revert plan: yes
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665198 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/ocaml/src/Makefile b/lib/ocaml/src/Makefile
index 723402b..20b0986 100644
--- a/lib/ocaml/src/Makefile
+++ b/lib/ocaml/src/Makefile
@@ -2,6 +2,6 @@
RESULT = thrift
LIBS = unix threads
THREADS = yes
-all: native-code-library byte-code-library top
+all: native-code-library debug-code-library top
OCAMLMAKEFILE = ../OCamlMakefile
include $(OCAMLMAKEFILE)
diff --git a/lib/ocaml/src/TBinaryProtocol.ml b/lib/ocaml/src/TBinaryProtocol.ml
index 748423f..fa84e71 100644
--- a/lib/ocaml/src/TBinaryProtocol.ml
+++ b/lib/ocaml/src/TBinaryProtocol.ml
@@ -114,7 +114,7 @@
let ver = self#readI32 in
if (ver land version_mask != version_1) then
(print_int ver;
- raise (P.TProtocolExn (P.BAD_VERSION, "Missing version identifier")))
+ raise (P.E (P.BAD_VERSION, "Missing version identifier")))
else
let s = self#readString in
let mt = P.message_type_of_i (ver land 0xFF) in
diff --git a/lib/ocaml/src/TChannelTransport.ml b/lib/ocaml/src/TChannelTransport.ml
index 89ae352..5407a8e 100644
--- a/lib/ocaml/src/TChannelTransport.ml
+++ b/lib/ocaml/src/TChannelTransport.ml
@@ -3,14 +3,18 @@
class t (i,o) =
object (self)
+ val mutable opened = true
inherit Transport.t
- method isOpen = true
+ method isOpen = opened
method opn = ()
- method close = ()
+ method close = close_in i; opened <- false
method read buf off len =
- try
- really_input i buf off len; len
- with _ -> T.raise_TTransportExn ("TChannelTransport: Could not read "^(string_of_int len)) T.UNKNOWN
+ if opened then
+ try
+ really_input i buf off len; len
+ with _ -> raise (T.E (T.UNKNOWN, ("TChannelTransport: Could not read "^(string_of_int len))))
+ else
+ raise (T.E (T.NOT_OPEN, "TChannelTransport: Channel was closed"))
method write buf off len = output o buf off len
method flush = flush o
end
diff --git a/lib/ocaml/src/TServer.ml b/lib/ocaml/src/TServer.ml
index a4dcc44..5fb8089 100644
--- a/lib/ocaml/src/TServer.ml
+++ b/lib/ocaml/src/TServer.ml
@@ -17,8 +17,7 @@
let trans = new TChannelTransport.t (inp,out) in
let proto = new TBinaryProtocol.t (trans :> Transport.t) in
try
- while proc#process proto proto do () done;
- ()
+ while proc#process proto proto do () done; ()
with e -> ()) (Unix.ADDR_INET (Unix.inet_addr_of_string "127.0.0.1",port))
diff --git a/lib/ocaml/src/TServerSocket.ml b/lib/ocaml/src/TServerSocket.ml
index ac98b08..9e79706 100644
--- a/lib/ocaml/src/TServerSocket.ml
+++ b/lib/ocaml/src/TServerSocket.ml
@@ -11,11 +11,12 @@
Unix.listen s 256
method close =
match sock with
- Some s -> Unix.shutdown s Unix.SHUTDOWN_ALL; Unix.close s; sock <- None
+ Some s -> Unix.shutdown s Unix.SHUTDOWN_ALL; Unix.close s;
+ sock <- None
| _ -> ()
method acceptImpl =
match sock with
Some s -> let (fd,_) = Unix.accept s in
new TChannelTransport.t (Unix.in_channel_of_descr fd,Unix.out_channel_of_descr fd)
- | _ -> Transport.raise_TTransportExn "ServerSocket: Not listening but tried to accept" Transport.NOT_OPEN
+ | _ -> raise (Transport.E (Transport.NOT_OPEN,"TServerSocket: Not listening but tried to accept"))
end
diff --git a/lib/ocaml/src/TSocket.ml b/lib/ocaml/src/TSocket.ml
index c02f1eb..c74864a 100644
--- a/lib/ocaml/src/TSocket.ml
+++ b/lib/ocaml/src/TSocket.ml
@@ -9,23 +9,28 @@
method isOpen = chans != None
method opn =
try
- chans <- Some(Unix.open_connection (Unix.ADDR_INET ((Unix.inet_addr_of_string host),port)))
+ let addr = (let {Unix.h_addr_list=x} = Unix.gethostbyname host in x.(0)) in
+ chans <- Some(Unix.open_connection (Unix.ADDR_INET (addr,port)))
with _ ->
- T.raise_TTransportExn
- ("Could not connect to "^host^":"^(string_of_int port))
- T.NOT_OPEN
- method close = match chans with None -> () | Some(inc,_) -> (Unix.shutdown_connection inc; chans <- None)
+ raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port))))
+
+ method close =
+ match chans with
+ None -> ()
+ | Some(inc,out) -> (Unix.shutdown_connection inc;
+ close_in inc;
+ chans <- None)
method read buf off len = match chans with
- None -> T.raise_TTransportExn "Socket not open" T.NOT_OPEN
+ None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
| Some(i,o) ->
try
really_input i buf off len; len
- with _ -> T.raise_TTransportExn ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port)) T.UNKNOWN
+ with _ -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port))))
method write buf off len = match chans with
- None -> T.raise_TTransportExn "Socket not open" T.NOT_OPEN
+ None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
| Some(i,o) -> output o buf off len
method flush = match chans with
- None -> T.raise_TTransportExn "Socket not open" T.NOT_OPEN
+ None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
| Some(i,o) -> flush o
end
diff --git a/lib/ocaml/src/Thrift.ml b/lib/ocaml/src/Thrift.ml
index 8ff5fa9..92c015b 100644
--- a/lib/ocaml/src/Thrift.ml
+++ b/lib/ocaml/src/Thrift.ml
@@ -9,11 +9,6 @@
method set_message s = message <- s
end;;
-exception TExn of t_exn;;
-
-
-
-
module Transport =
struct
type exn_type =
@@ -23,19 +18,7 @@
| TIMED_OUT
| END_OF_FILE;;
- class exn =
- object
- inherit t_exn
- val mutable typ = UNKNOWN
- method get_type = typ
- method set_type t = typ <- t
- end
- exception TTransportExn of exn
- let raise_TTransportExn message typ =
- let e = new exn in
- e#set_message message;
- e#set_type typ;
- raise (TTransportExn e)
+ exception E of exn_type * string
class virtual t =
object (self)
@@ -49,10 +32,7 @@
while !got < len do
ret := self#read buf (off+(!got)) (len - (!got));
if !ret <= 0 then
- let e = new exn in
- e#set_message "Cannot read. Remote side has closed.";
- raise (TTransportExn e)
- else ();
+ raise (E (UNKNOWN, "Cannot read. Remote side has closed."));
got := !got + !ret
done;
!got
@@ -260,7 +240,7 @@
| SIZE_LIMIT
| BAD_VERSION
- exception TProtocolExn of exn_type * string;;
+ exception E of exn_type * string;;
end;;
@@ -280,7 +260,7 @@
end
-
+(* Ugly *)
module Application_Exn =
struct
type typ=
@@ -336,7 +316,7 @@
let read (iprot : Protocol.t) =
let msg = ref "" in
let typ = ref 0 in
- iprot#readStructBegin;
+ ignore iprot#readStructBegin;
(try
while true do
let (name,ft,id) =iprot#readFieldBegin in