blob: 0d5c1a9131f11b63e51ca3249bc796f1e1fd17e0 [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
10-include("calculator.hrl").
11
Christopher Piro5f5fdf32007-07-25 22:41:00 +000012
13-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
15ping() ->
16 io:format("ping()~n",[]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000017 nil.
Christopher Piro094823a2007-07-18 00:26:12 +000018
19add(N1, N2) ->
20 io:format("add(~p,~p)~n",[N1,N2]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000021 N1+N2.
Christopher Piro094823a2007-07-18 00:26:12 +000022
23calculate(Logid, Work) ->
24 { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 },
25 io:format("calculate(~p, {~p,~p,~p})~n", [Logid, Op, Num1, Num2]),
26 case Op of
Christopher Piro5f5fdf32007-07-25 22:41:00 +000027 ?tutorial_ADD -> Num1 + Num2;
28 ?tutorial_SUBTRACT -> Num1 - Num2;
29 ?tutorial_MULTIPLY -> Num1 * Num2;
30
31 ?tutorial_DIVIDE when Num2 == 0 ->
32 throw(#invalidOperation{what=Op, why="Cannot divide by 0"});
33 ?tutorial_DIVIDE ->
34 Num1 div Num2;
35
36 _Else ->
37 throw(#invalidOperation{what=Op, why="Invalid operation"})
38
Christopher Piro094823a2007-07-18 00:26:12 +000039 end.
40
41getStruct(Key) ->
42 io:format("getStruct(~p)~n", [Key]),
Christopher Piro5f5fdf32007-07-25 22:41:00 +000043 #sharedStruct{key=Key, value="RARG"}.
Christopher Piro094823a2007-07-18 00:26:12 +000044
45zip() ->
46 io:format("zip~n").
47
48%%
49
50start() ->
Christopher Piro5f5fdf32007-07-25 22:41:00 +000051 start(9090).
52
53start(Port) ->
54 Handler = ?MODULE,
Christopher Piro094823a2007-07-18 00:26:12 +000055 Processor = calculator,
Christopher Piro094823a2007-07-18 00:26:12 +000056
57 TF = tBufferedTransportFactory:new(),
58 PF = tBinaryProtocolFactory:new(),
59
60 ServerTransport = tErlAcceptor,
61 ServerFlavor = tErlServer,
62
63 Server = oop:start_new(ServerFlavor, [Port, Handler, Processor, ServerTransport, TF, PF]),
64
65 ?R0(Server, effectful_serve),
66
67 Server.
68
69stop(Server) ->
70 ?C0(Server, stop),
71 ok.