Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame] | 1 | -module(calculatorHandler). |
| 2 | |
| 3 | -include("thrift/thrift.hrl"). |
| 4 | -include("thrift/transport/tSocket.hrl"). |
| 5 | -include("thrift/protocol/tBinaryProtocol.hrl"). |
| 6 | -include("thrift/server/tServer.hrl"). |
| 7 | -include("thrift/transport/tServerSocket.hrl"). |
| 8 | |
| 9 | -include("gen-erl/calculator.hrl"). |
| 10 | %-include("gen-erl/shared_types.hrl"). |
| 11 | |
| 12 | %-include("gen-erl/tutorial_types.hrl"). % TODO(cpiro): o rly? |
| 13 | |
| 14 | -export([start/0, ping/0, add/2, calculate/2, getStruct/1, zip/0]). |
| 15 | |
| 16 | %%% def initialize() |
| 17 | %%% @log = {} |
| 18 | %%% end |
| 19 | |
| 20 | % TODO: voids take only ok as return? |
| 21 | |
| 22 | ping() -> |
| 23 | io:format("ping()~n",[]), |
| 24 | {ok, nil}. |
| 25 | |
| 26 | add(N1, N2) -> |
| 27 | io:format("add(~p,~p)~n",[N1,N2]), |
| 28 | {ok, N1+N2}. |
| 29 | |
| 30 | calculate(Logid, Work) -> |
| 31 | { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 }, |
| 32 | io:format("calculate(~p, {~p,~p,~p})~n", [Logid, Op, Num1, Num2]), |
| 33 | case Op of |
| 34 | ?ADD -> {ok, Num1 + Num2}; |
| 35 | ?SUBTRACT -> {ok, Num1 - Num2}; |
| 36 | ?MULTIPLY -> {ok, Num1 * Num2}; |
| 37 | ?DIVIDE -> |
| 38 | if Num2 == 0 -> {error, #invalidOperation{what=Op, why="Cannot divide by 0"}}; |
| 39 | true -> {ok, Num1 / Num2} |
| 40 | end; |
| 41 | true -> |
| 42 | {error, #invalidOperation{what=Op, why="Invalid operation"}} |
| 43 | end. |
| 44 | |
| 45 | getStruct(Key) -> |
| 46 | io:format("getStruct(~p)~n", [Key]), |
| 47 | {ok, get(Key)}. |
| 48 | |
| 49 | zip() -> |
| 50 | io:format("zip~n"). |
| 51 | |
| 52 | start() -> |
| 53 | Transport = tServerSocket:new(9090), |
| 54 | Server = tServer:new(calculator, ?MODULE, Transport), |
| 55 | io:format("Starting the server...~n", []), |
| 56 | ?M0(Server, serve), |
| 57 | io:format("done.~n", []), % won't ever reach, rookie beotch |
| 58 | ok. |
| 59 | |
| 60 | %%% handler = CalculatorHandler.new() |
| 61 | %%% processor = Calculator::Processor.new(handler) |
| 62 | %%% transport = TServerSocket.new(9090) |
| 63 | %%% transportFactory = TBufferedTransportFactory.new() |
| 64 | %%% server = TSimpleServer.new(processor, transport, transportFactory) |
| 65 | %%% |
| 66 | %%% puts "Starting the server..." |
| 67 | %%% server.serve() |
| 68 | %%% puts "done." |