blob: ee5d93674048ec431d57458be2b39b90afcd8102 [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;
23use lib '../gen-perl';
24use Thrift::Socket;
25use Thrift::Server;
Kengo Sekid1ccd0a2019-10-01 16:15:44 -070026use Thrift::ServerSocket;
T Jake Luciani0c5c2342009-11-12 03:01:33 +000027use tutorial::Calculator;
28
29package CalculatorHandler;
30use base qw(tutorial::CalculatorIf);
31
32sub new {
33 my $classname = shift;
34 my $self = {};
35
36 return bless($self,$classname);
37}
38
39
40sub ping
41{
42 print "ping()\n";
43}
44
45sub add
46{
47 my($self, $n1, $n2) = @_;
48 printf("add(%d,%d)\n", $n1, $n2);
49 return $n1 + $n2;
50}
51
52sub calculate
53{
54 my($self, $logid, $work) = @_;
55 my $op = $work->{op};
56 my $num1 = $work->{num1};
57 my $num2 = $work->{num2};
58 printf("calculate(%d, %d %d %d)\n", $logid, $num1, $num2, $op);
59
60 my $val;
61
62 if ($op == tutorial::Operation::ADD) {
63 $val = $num1 + $num2;
64 } elsif ($op == tutorial::Operation::SUBTRACT) {
65 $val = $num1 - $num2;
66 } elsif ($op == tutorial::Operation::MULTIPLY) {
67 $val = $num1 * $num2;
68 } elsif ($op == tutorial::Operation::DIVIDE) {
69 if ($num2 == 0)
70 {
Dean Hamstead8a130f62018-10-17 18:48:42 +110071 my $x = tutorial::InvalidOperation->new();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020072 $x->whatOp($op);
T Jake Luciani0c5c2342009-11-12 03:01:33 +000073 $x->why('Cannot divide by 0');
74 die $x;
75 }
76 $val = $num1 / $num2;
77 } else {
Dean Hamstead8a130f62018-10-17 18:48:42 +110078 my $x = tutorial::InvalidOperation->new();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020079 $x->whatOp($op);
T Jake Luciani0c5c2342009-11-12 03:01:33 +000080 $x->why('Invalid operation');
81 die $x;
82 }
83
Dean Hamstead8a130f62018-10-17 18:48:42 +110084 my $log = shared::SharedStruct->new();
T Jake Luciani0c5c2342009-11-12 03:01:33 +000085 $log->key($logid);
86 $log->value(int($val));
87 $self->{log}->{$logid} = $log;
88
89 return $val;
90}
91
92sub getStruct
93{
94 my($self, $key) = @_;
95 printf("getStruct(%d)\n", $key);
96 return $self->{log}->{$key};
97}
98
99sub zip
100{
101 my($self) = @_;
102 print "zip()\n";
103}
104
105
106
107eval {
Dean Hamstead8a130f62018-10-17 18:48:42 +1100108 my $handler = CalculatorHandler->new();
109 my $processor = tutorial::CalculatorProcessor->new($handler);
110 my $serversocket = Thrift::ServerSocket->new(9090);
111 my $forkingserver = Thrift::ForkingServer->new($processor, $serversocket);
T Jake Luciani0c5c2342009-11-12 03:01:33 +0000112 print "Starting the server...\n";
113 $forkingserver->serve();
114 print "done.\n";
115}; if ($@) {
116 if ($@ =~ m/TException/ and exists $@->{message}) {
117 my $message = $@->{message};
118 my $code = $@->{code};
119 my $out = $code . ':' . $message;
120 die $out;
121 } else {
122 die $@;
123 }
124}
125