blob: 44f33ad5200d6c008452e97a0e07456dd8deee81 [file] [log] [blame]
Christopher Piro094823a2007-07-18 00:26:12 +00001-module(server).
2
3-include("thrift.hrl").
4-include("transport/tSocket.hrl").
5-include("protocol/tBinaryProtocol.hrl").
6
7-include("server/tErlServer.hrl").
8-include("transport/tErlAcceptor.hrl").
9
Christopher Piro93a06642007-09-18 06:23:33 +000010-include("calculator_thrift.hrl").
Christopher Piro094823a2007-07-18 00:26:12 +000011
Christopher Piro5f5fdf32007-07-25 22:41:00 +000012-export([start/0, start/1, stop/1, ping/0, add/2, calculate/2, getStruct/1, zip/0]).
Christopher Piro094823a2007-07-18 00:26:12 +000013
Christopher Piro5b3a8f72007-08-01 22:27:37 +000014debug(Format, Data) ->
15 error_logger:info_msg(Format, Data).
16
Christopher Piro094823a2007-07-18 00:26:12 +000017ping() ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000018 debug("ping()",[]),
19 ok.
Christopher Piro094823a2007-07-18 00:26:12 +000020
21add(N1, N2) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000022 debug("add(~p,~p)",[N1,N2]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000023 N1+N2.
Christopher Piro094823a2007-07-18 00:26:12 +000024
25calculate(Logid, Work) ->
26 { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 },
Christopher Piro5b3a8f72007-08-01 22:27:37 +000027 debug("calculate(~p, {~p,~p,~p})", [Logid, Op, Num1, Num2]),
Christopher Piro094823a2007-07-18 00:26:12 +000028 case Op of
Christopher Piro5f5fdf32007-07-25 22:41:00 +000029 ?tutorial_ADD -> Num1 + Num2;
30 ?tutorial_SUBTRACT -> Num1 - Num2;
31 ?tutorial_MULTIPLY -> Num1 * Num2;
32
33 ?tutorial_DIVIDE when Num2 == 0 ->
34 throw(#invalidOperation{what=Op, why="Cannot divide by 0"});
35 ?tutorial_DIVIDE ->
36 Num1 div Num2;
37
38 _Else ->
39 throw(#invalidOperation{what=Op, why="Invalid operation"})
Christopher Piro094823a2007-07-18 00:26:12 +000040 end.
41
42getStruct(Key) ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000043 debug("getStruct(~p)", [Key]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000044 #sharedStruct{key=Key, value="RARG"}.
Christopher Piro094823a2007-07-18 00:26:12 +000045
46zip() ->
Christopher Piro5b3a8f72007-08-01 22:27:37 +000047 debug("zip", []),
48 ok.
Christopher Piro094823a2007-07-18 00:26:12 +000049
50%%
51
52start() ->
Christopher Piro5f5fdf32007-07-25 22:41:00 +000053 start(9090).
54
55start(Port) ->
Christopher Piro8ca66b82008-01-15 12:04:09 +000056 thrift:start(),
Christopher Piro5b3a8f72007-08-01 22:27:37 +000057
Christopher Piro5f5fdf32007-07-25 22:41:00 +000058 Handler = ?MODULE,
Christopher Piro93a06642007-09-18 06:23:33 +000059 Processor = calculator_thrift,
Christopher Piro094823a2007-07-18 00:26:12 +000060
61 TF = tBufferedTransportFactory:new(),
62 PF = tBinaryProtocolFactory:new(),
63
64 ServerTransport = tErlAcceptor,
65 ServerFlavor = tErlServer,
66
67 Server = oop:start_new(ServerFlavor, [Port, Handler, Processor, ServerTransport, TF, PF]),
68
Christopher Piro5b3a8f72007-08-01 22:27:37 +000069 case ?R0(Server, effectful_serve) of
70 ok -> Server;
71 Error -> Error
72 end.
Christopher Piro094823a2007-07-18 00:26:12 +000073
74stop(Server) ->
75 ?C0(Server, stop),
76 ok.