Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | -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 Piro | 93a0664 | 2007-09-18 06:23:33 +0000 | [diff] [blame] | 10 | -include("calculator_thrift.hrl"). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 11 | |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 12 | -export([start/0, start/1, stop/1, ping/0, add/2, calculate/2, getStruct/1, zip/0]). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 13 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 14 | debug(Format, Data) -> |
| 15 | error_logger:info_msg(Format, Data). |
| 16 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 17 | ping() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 18 | debug("ping()",[]), |
| 19 | ok. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 20 | |
| 21 | add(N1, N2) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 22 | debug("add(~p,~p)",[N1,N2]), |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 23 | N1+N2. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 24 | |
| 25 | calculate(Logid, Work) -> |
| 26 | { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 }, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 27 | debug("calculate(~p, {~p,~p,~p})", [Logid, Op, Num1, Num2]), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 28 | case Op of |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 29 | ?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 Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 40 | end. |
| 41 | |
| 42 | getStruct(Key) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 43 | debug("getStruct(~p)", [Key]), |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 44 | #sharedStruct{key=Key, value="RARG"}. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 45 | |
| 46 | zip() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 47 | debug("zip", []), |
| 48 | ok. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 49 | |
| 50 | %% |
| 51 | |
| 52 | start() -> |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 53 | start(9090). |
| 54 | |
| 55 | start(Port) -> |
Christopher Piro | 8ca66b8 | 2008-01-15 12:04:09 +0000 | [diff] [blame] | 56 | thrift:start(), |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 57 | |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 58 | Handler = ?MODULE, |
Christopher Piro | 93a0664 | 2007-09-18 06:23:33 +0000 | [diff] [blame] | 59 | Processor = calculator_thrift, |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 60 | |
| 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 Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 69 | case ?R0(Server, effectful_serve) of |
| 70 | ok -> Server; |
| 71 | Error -> Error |
| 72 | end. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 73 | |
| 74 | stop(Server) -> |
| 75 | ?C0(Server, stop), |
| 76 | ok. |