David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 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 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 29 | method opn = |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 30 | try |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 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))) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 33 | with |
iproctor | 94112d6 | 2007-08-17 21:34:15 +0000 | [diff] [blame] | 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)))) |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 36 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 37 | method close = |
| 38 | match chans with |
| 39 | None -> () |
| 40 | | Some(inc,out) -> (Unix.shutdown_connection inc; |
| 41 | close_in inc; |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 42 | chans <- None) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 43 | method read buf off len = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 44 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 45 | | Some(i,o) -> |
| 46 | try |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 47 | really_input i buf off len; len |
iproctor | 94112d6 | 2007-08-17 21:34:15 +0000 | [diff] [blame] | 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)))) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 51 | method write buf off len = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 52 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 53 | | Some(i,o) -> output o buf off len |
| 54 | method flush = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 55 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 56 | | Some(i,o) -> flush o |
| 57 | end |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 58 | |
| 59 | |