blob: 647cc2474079574ebd454950ab3325bc7c1b3615 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
2%% Licensed to the Apache Software Foundation (ASF) under one
3%% or more contributor license agreements. See the NOTICE file
4%% distributed with this work for additional information
5%% regarding copyright ownership. The ASF licenses this file
6%% to you under the Apache License, Version 2.0 (the
7%% "License"); you may not use this file except in compliance
8%% with the License. You may obtain a copy of the License at
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
19
David Reiss8a162a52008-06-11 01:03:16 +000020-module(server).
21
22-include("calculator_thrift.hrl").
23
24-export([start/0, start/1, handle_function/2,
25 stop/1, ping/0, add/2, calculate/2, getStruct/1, zip/0]).
26
27debug(Format, Data) ->
28 error_logger:info_msg(Format, Data).
29
30ping() ->
31 debug("ping()",[]),
32 ok.
33
34add(N1, N2) ->
35 debug("add(~p,~p)",[N1,N2]),
36 N1+N2.
37
38calculate(Logid, Work) ->
walter-weinmannfcb2f5a2017-09-05 15:20:37 +020039 { Op, Num1, Num2 } = { Work#'Work'.op, Work#'Work'.num1, Work#'Work'.num2 },
David Reiss8a162a52008-06-11 01:03:16 +000040 debug("calculate(~p, {~p,~p,~p})", [Logid, Op, Num1, Num2]),
41 case Op of
walter-weinmannfcb2f5a2017-09-05 15:20:37 +020042 ?TUTORIAL_OPERATION_ADD -> Num1 + Num2;
43 ?TUTORIAL_OPERATION_SUBTRACT -> Num1 - Num2;
44 ?TUTORIAL_OPERATION_MULTIPLY -> Num1 * Num2;
David Reiss8a162a52008-06-11 01:03:16 +000045
walter-weinmannfcb2f5a2017-09-05 15:20:37 +020046 ?TUTORIAL_OPERATION_DIVIDE when Num2 == 0 ->
47 throw(#'InvalidOperation'{whatOp=Op, why="Cannot divide by 0"});
48 ?TUTORIAL_OPERATION_DIVIDE ->
Anthony F. Molinaro10a87432011-02-16 05:54:17 +000049 Num1 div Num2;
David Reiss8a162a52008-06-11 01:03:16 +000050
Anthony F. Molinaro10a87432011-02-16 05:54:17 +000051 _Else ->
walter-weinmannfcb2f5a2017-09-05 15:20:37 +020052 throw(#'InvalidOperation'{whatOp=Op, why="Invalid operation"})
David Reiss8a162a52008-06-11 01:03:16 +000053 end.
54
55getStruct(Key) ->
56 debug("getStruct(~p)", [Key]),
walter-weinmannfcb2f5a2017-09-05 15:20:37 +020057 #'SharedStruct'{key=Key, value="RARG"}.
David Reiss8a162a52008-06-11 01:03:16 +000058
59zip() ->
60 debug("zip", []),
61 ok.
62
63%%
64
65start() ->
walter-weinmann53106162017-09-18 20:18:50 +020066 start(9090).
David Reiss8a162a52008-06-11 01:03:16 +000067
68start(Port) ->
69 Handler = ?MODULE,
70 thrift_socket_server:start([{handler, Handler},
71 {service, calculator_thrift},
72 {port, Port},
73 {name, tutorial_server}]).
74
75stop(Server) ->
76 thrift_socket_server:stop(Server).
77
78handle_function(Function, Args) when is_atom(Function), is_tuple(Args) ->
79 case apply(?MODULE, Function, tuple_to_list(Args)) of
80 ok -> ok;
81 Reply -> {reply, Reply}
82 end.