blob: f002fd8e83a1ba3cc011e970ada1532342ec83d8 [file] [log] [blame]
Christopher Piro094823a2007-07-18 00:26:12 +00001%%% Copyright (c) 2007- Facebook
2%%% Distributed under the Thrift Software License
Christopher Piro3121c822007-11-15 06:26:26 +00003%%%
Christopher Piro094823a2007-07-18 00:26:12 +00004%%% See accompanying file LICENSE or visit the Thrift site at:
5%%% http://developers.facebook.com/thrift/
6
7-module(tSocket).
8
9-include("oop.hrl").
10
11-include("thrift.hrl").
12-include("transport/tTransportException.hrl").
Christopher Piro094823a2007-07-18 00:26:12 +000013-include("transport/tSocket.hrl").
14
15-behavior(oop).
16
Christopher Piroc3c6c0c2007-08-01 23:42:12 +000017-export([attr/4, super/0, inspect/1]).
Christopher Piro094823a2007-07-18 00:26:12 +000018
Christopher Piro68940292007-10-02 00:35:12 +000019-export([new/0, new/1, new/2,
Christopher Piro3121c822007-11-15 06:26:26 +000020 effectful_setHandle/2, effectful_open/1,
21 isOpen/1, effectful_write/2, read/2, effectful_close/1]).
Christopher Piro094823a2007-07-18 00:26:12 +000022
23%%%
24%%% define attributes
25%%% 'super' is required unless ?MODULE is a base class
26%%%
27
28?DEFINE_ATTR(super);
29?DEFINE_ATTR(host);
30?DEFINE_ATTR(port);
31?DEFINE_ATTR(handle).
Christopher Piro3121c822007-11-15 06:26:26 +000032
Christopher Piro094823a2007-07-18 00:26:12 +000033%%%
34%%% behavior callbacks
35%%%
Christopher Piro3121c822007-11-15 06:26:26 +000036
Christopher Piro094823a2007-07-18 00:26:12 +000037%%% super() -> SuperModule = atom()
38%%% | none
39
40super() ->
41 tTransport.
42
43%%% inspect(This) -> string()
44
45inspect(This) ->
46 ?FORMAT_ATTR(host) ++ ", " ++
47 ?FORMAT_ATTR(port) ++ ", " ++
48 ?FORMAT_ATTR(handle).
49
50%%%
51%%% class methods
52%%%
53
54new(Host, Port) ->
55 Super = (super()):new(),
56 #?MODULE{super=Super, host=Host, port=Port, handle=nil}.
57
58new(Host) ->
59 new(Host, 9090).
60
61new() ->
62 new("localhost", 9090).
Christopher Piro3121c822007-11-15 06:26:26 +000063
Christopher Piro094823a2007-07-18 00:26:12 +000064%%%
65%%% instance methods
66%%%
67
68effectful_setHandle(This, Handle) ->
69 {ok, oop:set(This, handle, Handle)}.
70
Christopher Piro3121c822007-11-15 06:26:26 +000071effectful_open(This) ->
Christopher Piro094823a2007-07-18 00:26:12 +000072 Host = oop:get(This, host),
73 Port = oop:get(This, port),
Christopher Piro3121c822007-11-15 06:26:26 +000074 Options = [binary, {packet, 0}, {active, false}],
Christopher Piro094823a2007-07-18 00:26:12 +000075
76 case gen_tcp:connect(Host, Port, Options) of
Christopher Piro3121c822007-11-15 06:26:26 +000077 {error, _} ->
78 exit(tTransportException:new(
79 ?tTransportException_NOT_OPEN,
80 "Could not connect to " ++ Host ++ ":" ++ Port)
81 );
82 {ok, Socket} ->
83 effectful_setHandle(This, Socket)
Christopher Piro094823a2007-07-18 00:26:12 +000084 end.
85
86isOpen(This) ->
87 oop:get(This, handle) /= nil.
88
Christopher Pirob6dcd2b2007-10-13 01:11:46 +000089effectful_write(This, Str) ->
Christopher Piro094823a2007-07-18 00:26:12 +000090 Handle = oop:get(This, handle),
91 Val = gen_tcp:send(Handle, Str),
92
Christopher Piro68940292007-10-02 00:35:12 +000093 %% DEBUG
94 %% error_logger:info_msg("tSocket: wrote ~p~n", [Str]),
95
Christopher Piro5b3a8f72007-08-01 22:27:37 +000096 %% error_logger:info_msg("WRITE |~p| (~p)", [Str,Val]),
Christopher Piro3121c822007-11-15 06:26:26 +000097
Christopher Piro094823a2007-07-18 00:26:12 +000098 case Val of
Christopher Piro3121c822007-11-15 06:26:26 +000099 {error, _} ->
100 throw(tTransportException:new(?tTransportException_NOT_OPEN, "in write"));
101 ok ->
102 {ok, This}
Christopher Piro094823a2007-07-18 00:26:12 +0000103 end.
104
105read(This, Sz) ->
106 Handle = oop:get(This, handle),
107 case gen_tcp:recv(Handle, Sz) of
Christopher Piro3121c822007-11-15 06:26:26 +0000108 {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.
119
Christopher Piro094823a2007-07-18 00:26:12 +0000120effectful_close(This) ->
121 case oop:get(This, handle) of
Christopher Piro3121c822007-11-15 06:26:26 +0000122 nil ->
123 {ok, This};
124 Handle ->
125 gen_tcp:close(Handle),
126 {ok, oop:set(This, handle, nil)}
Christopher Piro094823a2007-07-18 00:26:12 +0000127 end.