| 30 |
ashish |
1 |
(*
|
|
|
2 |
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
or more contributor license agreements. See the NOTICE file
|
|
|
4 |
distributed with this work for additional information
|
|
|
5 |
regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
to you under the Apache License, Version 2.0 (the
|
|
|
7 |
"License"); you may not use this file except in compliance
|
|
|
8 |
with the License. You may obtain a copy of the License at
|
|
|
9 |
|
|
|
10 |
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
|
|
|
12 |
Unless required by applicable law or agreed to in writing,
|
|
|
13 |
software distributed under the License is distributed on an
|
|
|
14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
KIND, either express or implied. See the License for the
|
|
|
16 |
specific language governing permissions and limitations
|
|
|
17 |
under the License.
|
|
|
18 |
*)
|
|
|
19 |
|
|
|
20 |
open Thrift
|
|
|
21 |
|
|
|
22 |
module T = Transport
|
|
|
23 |
|
|
|
24 |
class t host port=
|
|
|
25 |
object (self)
|
|
|
26 |
inherit T.t
|
|
|
27 |
val mutable chans = None
|
|
|
28 |
method isOpen = chans != None
|
|
|
29 |
method opn =
|
|
|
30 |
try
|
|
|
31 |
let addr = (let {Unix.h_addr_list=x} = Unix.gethostbyname host in x.(0)) in
|
|
|
32 |
chans <- Some(Unix.open_connection (Unix.ADDR_INET (addr,port)))
|
|
|
33 |
with
|
|
|
34 |
Unix.Unix_error (e,fn,_) -> raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port)^" because: "^fn^":"^(Unix.error_message e))))
|
|
|
35 |
| _ -> raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port))))
|
|
|
36 |
|
|
|
37 |
method close =
|
|
|
38 |
match chans with
|
|
|
39 |
None -> ()
|
|
|
40 |
| Some(inc,out) -> (Unix.shutdown_connection inc;
|
|
|
41 |
close_in inc;
|
|
|
42 |
chans <- None)
|
|
|
43 |
method read buf off len = match chans with
|
|
|
44 |
None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
|
|
|
45 |
| Some(i,o) ->
|
|
|
46 |
try
|
|
|
47 |
really_input i buf off len; len
|
|
|
48 |
with
|
|
|
49 |
Unix.Unix_error (e,fn,_) -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port)^" because: "^fn^":"^(Unix.error_message e))))
|
|
|
50 |
| _ -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port))))
|
|
|
51 |
method write buf off len = match chans with
|
|
|
52 |
None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
|
|
|
53 |
| Some(i,o) -> output o buf off len
|
|
|
54 |
method flush = match chans with
|
|
|
55 |
None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
|
|
|
56 |
| Some(i,o) -> flush o
|
|
|
57 |
end
|
|
|
58 |
|
|
|
59 |
|