r1293@opsdev009 (orig r69993): cpiro | 2007-11-14 22:26:26 -0800
[amiethrift.git] / lib / erl / src / transport / tSocket.erl
blobf002fd8e83a1ba3cc011e970ada1532342ec83d8
1 %%% Copyright (c) 2007- Facebook
2 %%% Distributed under the Thrift Software License
3 %%%
4 %%% See accompanying file LICENSE or visit the Thrift site at:
5 %%% http://developers.facebook.com/thrift/
7 -module(tSocket).
9 -include("oop.hrl").
11 -include("thrift.hrl").
12 -include("transport/tTransportException.hrl").
13 -include("transport/tSocket.hrl").
15 -behavior(oop).
17 -export([attr/4, super/0, inspect/1]).
19 -export([new/0, new/1, new/2,
20 effectful_setHandle/2, effectful_open/1,
21 isOpen/1, effectful_write/2, read/2, effectful_close/1]).
23 %%%
24 %%% define attributes
25 %%% 'super' is required unless ?MODULE is a base class
26 %%%
28 ?DEFINE_ATTR(super);
29 ?DEFINE_ATTR(host);
30 ?DEFINE_ATTR(port);
31 ?DEFINE_ATTR(handle).
33 %%%
34 %%% behavior callbacks
35 %%%
37 %%% super() -> SuperModule = atom()
38 %%% | none
40 super() ->
41 tTransport.
43 %%% inspect(This) -> string()
45 inspect(This) ->
46 ?FORMAT_ATTR(host) ++ ", " ++
47 ?FORMAT_ATTR(port) ++ ", " ++
48 ?FORMAT_ATTR(handle).
50 %%%
51 %%% class methods
52 %%%
54 new(Host, Port) ->
55 Super = (super()):new(),
56 #?MODULE{super=Super, host=Host, port=Port, handle=nil}.
58 new(Host) ->
59 new(Host, 9090).
61 new() ->
62 new("localhost", 9090).
64 %%%
65 %%% instance methods
66 %%%
68 effectful_setHandle(This, Handle) ->
69 {ok, oop:set(This, handle, Handle)}.
71 effectful_open(This) ->
72 Host = oop:get(This, host),
73 Port = oop:get(This, port),
74 Options = [binary, {packet, 0}, {active, false}],
76 case gen_tcp:connect(Host, Port, Options) of
77 {error, _} ->
78 exit(tTransportException:new(
79 ?tTransportException_NOT_OPEN,
80 "Could not connect to " ++ Host ++ ":" ++ Port)
82 {ok, Socket} ->
83 effectful_setHandle(This, Socket)
84 end.
86 isOpen(This) ->
87 oop:get(This, handle) /= nil.
89 effectful_write(This, Str) ->
90 Handle = oop:get(This, handle),
91 Val = gen_tcp:send(Handle, Str),
93 %% DEBUG
94 %% error_logger:info_msg("tSocket: wrote ~p~n", [Str]),
96 %% error_logger:info_msg("WRITE |~p| (~p)", [Str,Val]),
98 case Val of
99 {error, _} ->
100 throw(tTransportException:new(?tTransportException_NOT_OPEN, "in write"));
101 ok ->
102 {ok, This}
103 end.
105 read(This, Sz) ->
106 Handle = oop:get(This, handle),
107 case gen_tcp:recv(Handle, Sz) of
108 {ok, []} ->
109 Host = oop:get(This, host),
110 Port = oop:get(This, port),
111 throw(tTransportException:new(?tTransportException_UNKNOWN, "TSocket: Could not read " ++ Sz ++ "bytes from " ++ Host ++ ":" ++ Port));
112 {ok, Data} ->
113 %% DEBUG
114 ?INFO("tSocket: read ~p", [Data]),
115 Data;
116 {error, Error} ->
117 exit(tTransportException:new(?tTransportException_NOT_OPEN, "in tSocket:read/2: gen_tcp:recv"))
118 end.
120 effectful_close(This) ->
121 case oop:get(This, handle) of
122 nil ->
123 {ok, This};
124 Handle ->
125 gen_tcp:close(Handle),
126 {ok, oop:set(This, handle, nil)}
127 end.