Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 1 | -module(server). |
| 2 | |
| 3 | -include("thrift.hrl"). |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 4 | -include("thrift_logger.hrl"). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 5 | -include("transport/tSocket.hrl"). |
| 6 | -include("protocol/tBinaryProtocol.hrl"). |
| 7 | |
| 8 | -include("server/tErlServer.hrl"). |
| 9 | -include("transport/tErlAcceptor.hrl"). |
| 10 | |
Christopher Piro | 93a0664 | 2007-09-18 06:23:33 +0000 | [diff] [blame^] | 11 | -include("calculator_thrift.hrl"). |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 12 | |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 13 | -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] | 14 | |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 15 | debug(Format, Data) -> |
| 16 | error_logger:info_msg(Format, Data). |
| 17 | |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 18 | ping() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 19 | debug("ping()",[]), |
| 20 | ok. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 21 | |
| 22 | add(N1, N2) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 23 | debug("add(~p,~p)",[N1,N2]), |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 24 | N1+N2. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 25 | |
| 26 | calculate(Logid, Work) -> |
| 27 | { Op, Num1, Num2 } = { Work#work.op, Work#work.num1, Work#work.num2 }, |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 28 | debug("calculate(~p, {~p,~p,~p})", [Logid, Op, Num1, Num2]), |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 29 | case Op of |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 30 | ?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 Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 42 | end. |
| 43 | |
| 44 | getStruct(Key) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 45 | debug("getStruct(~p)", [Key]), |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 46 | #sharedStruct{key=Key, value="RARG"}. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 47 | |
| 48 | zip() -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 49 | debug("zip", []), |
| 50 | ok. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 51 | |
| 52 | %% |
| 53 | |
| 54 | start() -> |
Christopher Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 55 | start(9090). |
| 56 | |
| 57 | start(Port) -> |
Christopher Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 58 | 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 Piro | 5f5fdf3 | 2007-07-25 22:41:00 +0000 | [diff] [blame] | 64 | Handler = ?MODULE, |
Christopher Piro | 93a0664 | 2007-09-18 06:23:33 +0000 | [diff] [blame^] | 65 | Processor = calculator_thrift, |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 66 | |
| 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 Piro | 5b3a8f7 | 2007-08-01 22:27:37 +0000 | [diff] [blame] | 75 | case ?R0(Server, effectful_serve) of |
| 76 | ok -> Server; |
| 77 | Error -> Error |
| 78 | end. |
Christopher Piro | 094823a | 2007-07-18 00:26:12 +0000 | [diff] [blame] | 79 | |
| 80 | stop(Server) -> |
| 81 | ?C0(Server, stop), |
| 82 | ok. |