blob: ab2b04046673fba0cc8d6f22d63d7892ae5a716d [file] [log] [blame]
Christopher Piro094823a2007-07-18 00:26:12 +00001-module(server).
2
3-include("thrift.hrl").
Christopher Piro5b3a8f72007-08-01 22:27:37 +00004-include("thrift_logger.hrl").
Christopher Piro094823a2007-07-18 00:26:12 +00005-include("transport/tSocket.hrl").
6-include("protocol/tBinaryProtocol.hrl").
7
8-include("server/tErlServer.hrl").
9-include("transport/tErlAcceptor.hrl").
10
Christopher Piro93a06642007-09-18 06:23:33 +000011-include("calculator_thrift.hrl").
Christopher Piro094823a2007-07-18 00:26:12 +000012
Christopher Piro5f5fdf32007-07-25 22:41:00 +000013-export([start/0, start/1, stop/1, ping/0, add/2, calculate/2, getStruct/1, zip/0]).
Christopher Piro094823a2007-07-18 00:26:12 +000014
Christopher Piro5b3a8f72007-08-01 22:27:37 +000015debug(Format, Data) ->
16 error_logger:info_msg(Format, Data).
17
Christopher Piro094823a2007-07-18 00:26:12 +000018ping() ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000019 debug("ping()",[]),
20 ok.
Christopher Piro094823a2007-07-18 00:26:12 +000021
22add(N1, N2) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000023 debug("add(~p,~p)",[N1,N2]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000024 N1+N2.
Christopher Piro094823a2007-07-18 00:26:12 +000025
26calculate(Logid, Work) ->
27 { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 },
Christopher Piro5b3a8f72007-08-01 22:27:37 +000028 debug("calculate(~p, {~p,~p,~p})", [Logid, Op, Num1, Num2]),
Christopher Piro094823a2007-07-18 00:26:12 +000029 case Op of
Christopher Piro5f5fdf32007-07-25 22:41:00 +000030 ?tutorial_ADD -> Num1 + Num2;
31 ?tutorial_SUBTRACT -> Num1 - Num2;
32 ?tutorial_MULTIPLY -> Num1 * Num2;
33
34 ?tutorial_DIVIDE when Num2 == 0 ->
35 throw(#invalidOperation{what=Op, why="Cannot divide by 0"});
36 ?tutorial_DIVIDE ->
37 Num1 div Num2;
38
39 _Else ->
40 throw(#invalidOperation{what=Op, why="Invalid operation"})
41
Christopher Piro094823a2007-07-18 00:26:12 +000042 end.
43
44getStruct(Key) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000045 debug("getStruct(~p)", [Key]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000046 #sharedStruct{key=Key, value="RARG"}.
Christopher Piro094823a2007-07-18 00:26:12 +000047
48zip() ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000049 debug("zip", []),
50 ok.
Christopher Piro094823a2007-07-18 00:26:12 +000051
52%%
53
54start() ->
Christopher Piro5f5fdf32007-07-25 22:41:00 +000055 start(9090).
56
57start(Port) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000058 thrift_logger:install([#thrift_logger_state{
59 force_one_line = false, %% should we collapse all errors to one line?
60 term_width = 110, %% if so, crop output at this width
61 omit = [oop_new, req_processed] %% don't show these kinds of infos
62 }]),
63
Christopher Piro5f5fdf32007-07-25 22:41:00 +000064 Handler = ?MODULE,
Christopher Piro93a06642007-09-18 06:23:33 +000065 Processor = calculator_thrift,
Christopher Piro094823a2007-07-18 00:26:12 +000066
67 TF = tBufferedTransportFactory:new(),
68 PF = tBinaryProtocolFactory:new(),
69
70 ServerTransport = tErlAcceptor,
71 ServerFlavor = tErlServer,
72
73 Server = oop:start_new(ServerFlavor, [Port, Handler, Processor, ServerTransport, TF, PF]),
74
Christopher Piro5b3a8f72007-08-01 22:27:37 +000075 case ?R0(Server, effectful_serve) of
76 ok -> Server;
77 Error -> Error
78 end.
Christopher Piro094823a2007-07-18 00:26:12 +000079
80stop(Server) ->
81 ?C0(Server, stop),
82 ok.