blob: 151298731011df2acc9f483b425b7f553ba15184 [file] [log] [blame]
Dean Hamstead8a130f62018-10-17 18:48:42 +11001#!/usr/bin/env perl
T Jake Luciani0c5c2342009-11-12 03:01:33 +00002
3#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
22use strict;
Kengo Seki58efe892019-10-09 22:23:16 +090023use lib '../../lib/perl/lib';
24use lib 'gen-perl';
T Jake Luciani0c5c2342009-11-12 03:01:33 +000025use Thrift::Socket;
26use Thrift::Server;
Kengo Sekid1ccd0a2019-10-01 16:15:44 -070027use Thrift::ServerSocket;
T Jake Luciani0c5c2342009-11-12 03:01:33 +000028use tutorial::Calculator;
29
30package CalculatorHandler;
31use base qw(tutorial::CalculatorIf);
32
33sub new {
34 my $classname = shift;
35 my $self = {};
36
37 return bless($self,$classname);
38}
39
40
41sub ping
42{
43 print "ping()\n";
44}
45
46sub add
47{
48 my($self, $n1, $n2) = @_;
49 printf("add(%d,%d)\n", $n1, $n2);
50 return $n1 + $n2;
51}
52
53sub calculate
54{
55 my($self, $logid, $work) = @_;
56 my $op = $work->{op};
57 my $num1 = $work->{num1};
58 my $num2 = $work->{num2};
59 printf("calculate(%d, %d %d %d)\n", $logid, $num1, $num2, $op);
60
61 my $val;
62
63 if ($op == tutorial::Operation::ADD) {
64 $val = $num1 + $num2;
65 } elsif ($op == tutorial::Operation::SUBTRACT) {
66 $val = $num1 - $num2;
67 } elsif ($op == tutorial::Operation::MULTIPLY) {
68 $val = $num1 * $num2;
69 } elsif ($op == tutorial::Operation::DIVIDE) {
70 if ($num2 == 0)
71 {
Dean Hamstead8a130f62018-10-17 18:48:42 +110072 my $x = tutorial::InvalidOperation->new();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020073 $x->whatOp($op);
T Jake Luciani0c5c2342009-11-12 03:01:33 +000074 $x->why('Cannot divide by 0');
75 die $x;
76 }
77 $val = $num1 / $num2;
78 } else {
Dean Hamstead8a130f62018-10-17 18:48:42 +110079 my $x = tutorial::InvalidOperation->new();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020080 $x->whatOp($op);
T Jake Luciani0c5c2342009-11-12 03:01:33 +000081 $x->why('Invalid operation');
82 die $x;
83 }
84
Dean Hamstead8a130f62018-10-17 18:48:42 +110085 my $log = shared::SharedStruct->new();
T Jake Luciani0c5c2342009-11-12 03:01:33 +000086 $log->key($logid);
87 $log->value(int($val));
88 $self->{log}->{$logid} = $log;
89
90 return $val;
91}
92
93sub getStruct
94{
95 my($self, $key) = @_;
96 printf("getStruct(%d)\n", $key);
97 return $self->{log}->{$key};
98}
99
100sub zip
101{
102 my($self) = @_;
103 print "zip()\n";
104}
105
106
107
108eval {
Dean Hamstead8a130f62018-10-17 18:48:42 +1100109 my $handler = CalculatorHandler->new();
110 my $processor = tutorial::CalculatorProcessor->new($handler);
111 my $serversocket = Thrift::ServerSocket->new(9090);
112 my $forkingserver = Thrift::ForkingServer->new($processor, $serversocket);
T Jake Luciani0c5c2342009-11-12 03:01:33 +0000113 print "Starting the server...\n";
114 $forkingserver->serve();
115 print "done.\n";
116}; if ($@) {
117 if ($@ =~ m/TException/ and exists $@->{message}) {
118 my $message = $@->{message};
119 my $code = $@->{code};
120 my $out = $code . ':' . $message;
121 die $out;
122 } else {
123 die $@;
124 }
125}
126