Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | %%% Copyright (c) 2007- Facebook |
| 2 | %%% Distributed under the Thrift Software License |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 3 | %%% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 4 | %%% See accompanying file LICENSE or visit the Thrift site at: |
| 5 | %%% http://developers.facebook.com/thrift/ |
| 6 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 7 | -module(thrift_oop_server). |
| 8 | |
| 9 | -behaviour(gen_server). |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 10 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 11 | -include("oop.hrl"). |
| 12 | |
| 13 | -include("thrift.hrl"). |
| 14 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 15 | -include("transport/tTransportException.hrl"). |
| 16 | -include("protocol/tProtocolException.hrl"). |
| 17 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 18 | -export([ |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 19 | start_link/0, |
| 20 | stop/0 |
| 21 | ]). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 22 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 23 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). |
| 24 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 25 | -define(SERVER, ?MODULE). |
| 26 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 27 | %%% |
| 28 | %%% api |
| 29 | %%% |
| 30 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 31 | start_link() -> |
| 32 | gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). |
| 33 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 34 | stop() -> |
| 35 | gen_server:cast(?SERVER, stop). |
| 36 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 37 | %%% |
| 38 | %%% init |
| 39 | %%% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 40 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 41 | init({Class, Args}) -> |
| 42 | process_flag(trap_exit, true), |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 43 | try %% TODO use apply_if_defined |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 44 | State = apply(Class, new, Args), |
| 45 | ?INFO("thrift ~p:new(~s) = ~s", [Class, thrift_utils:unbrack(Args), oop:inspect(State)]), |
| 46 | {ok, State} |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 47 | catch |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 48 | E -> {stop, {new_failed, E}} |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 49 | end; |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 50 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 51 | init(_) -> |
| 52 | {stop, invalid_params}. |
| 53 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 54 | %%% |
| 55 | %%% call and cast |
| 56 | %%% |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 57 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 58 | handle_call(Request, From, State) -> handle_call_cast(call, Request, From, State). |
| 59 | handle_cast(stop, State) -> {stop, normal, State}; |
| 60 | handle_cast({Method, Args}, State) -> handle_call_cast(cast, {Method, Args}, undefined, State). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 61 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 62 | reply(call, Value, State) -> {reply, Value, State}; |
| 63 | reply(cast, _Value, State) -> {noreply, State}. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 64 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 65 | handle_call_cast(Type, Request, From, State) -> |
| 66 | %% ?INFO("~p: ~p", [?SERVER, oop:inspect(State)]), |
| 67 | %% ?INFO("handle_call(Request=~p, From=~p, State)", [Request, From]), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 68 | |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 69 | case Request of |
| 70 | {get, [Field]} -> |
| 71 | Value = oop:get(State, Field), |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 72 | reply(Type, Value, State); |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 73 | |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 74 | {set, [Field, Value]} -> |
| 75 | State1 = oop:set(State, Field, Value), |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 76 | reply(Type, Value, State1); |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 77 | |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 78 | {class, []} -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 79 | reply(Type, ?CLASS(State), State); |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 80 | |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 81 | {Method, Args} -> |
| 82 | handle_method(Type, State, Method, Args); |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 83 | |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 84 | _ -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 85 | ?ERROR("thrift no match for Request = ~p", [Request]), |
| 86 | {stop, server_error, State} |
| 87 | %% {reply, server_error, State} |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 88 | end. |
| 89 | |
| 90 | handle_method(Type, State, Method, Args) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 91 | Is_effectful = lists:prefix("effectful_", atom_to_list(Method)), |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 92 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 93 | try {Is_effectful, oop:call(State, Method, Args)} of |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 94 | {true, {Retval, State1}} -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 95 | reply(Type, Retval, State1); |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 96 | |
| 97 | {true, _MalformedReturn} -> |
| 98 | %% TODO(cpiro): bad match -- remove when we're done converting |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 99 | ?ERROR("oop:call(effectful_*,..,..) malformed return value ~p", |
| 100 | [_MalformedReturn]), |
| 101 | {stop, server_error, State}; |
| 102 | %% {noreply, State}; |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 103 | |
| 104 | {false, Retval} -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 105 | reply(Type, Retval, State) |
| 106 | |
| 107 | catch |
| 108 | exit:{thrift_exception, E} -> handle_exception(E, nothing); |
| 109 | exit:{{thrift_exception, E}, Stack} -> handle_exception(E, Stack); |
| 110 | exit:normal -> exit(normal); |
| 111 | exit:(X = {timeout, _}) -> exit(X); |
| 112 | exit:Other -> |
| 113 | exit(Other) |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 114 | end. |
| 115 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 116 | handle_exception(E, Stack) -> |
| 117 | %% ?ERROR("texception ~p", [E]), |
| 118 | case {oop:is_a(E, tException), Stack} of |
| 119 | {true, nothing} -> % good |
| 120 | exit({thrift_exception, E}); |
| 121 | {true, _} -> % good |
| 122 | E1 = tException:add_backtrace_element(E, Stack), |
| 123 | exit({thrift_exception, E1}); |
eletuchy | 53e5719 | 2008-02-27 18:38:42 +0000 | [diff] [blame] | 124 | {false, _} -> % shit |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 125 | ?ERROR("exception wasn't really a tException ~p", [E]), |
| 126 | exit(bum) |
| 127 | end. |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 128 | |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 129 | %%% |
| 130 | %%% info, terminate, and code_change |
| 131 | %%% |
| 132 | |
| 133 | handle_info({'EXIT', Pid, Except} = All, State) -> |
| 134 | case Except of |
| 135 | normal -> |
| 136 | {noreply, State}; |
| 137 | {normal, _} -> |
| 138 | {noreply, State}; |
| 139 | _unhandled -> |
Christopher Piro | 53b6ef6 | 2007-11-15 06:26:28 +0000 | [diff] [blame] | 140 | error_logger:format("unhandled exit ~p", [All]), |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 141 | {stop, All, State} |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 142 | end; |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 143 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 144 | handle_info(Info, State) -> |
Christopher Piro | de11d85 | 2007-11-18 02:10:20 +0000 | [diff] [blame] | 145 | ?INFO("~p", [Info]), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 146 | {noreply, State}. |
| 147 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 148 | terminate(Reason, State) -> |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 149 | ok. |
| 150 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 151 | code_change(OldVsn, State, Extra) -> |
| 152 | {ok, State}. |