1#!/usr/bin/env perl
2
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 '../../lib/perl/lib';
24use lib 'gen-perl';
25use Thrift::Socket;
26use Thrift::Server;
27use Thrift::ServerSocket;
28use 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    {
72      my $x = tutorial::InvalidOperation->new();
73      $x->whatOp($op);
74      $x->why('Cannot divide by 0');
75      die $x;
76    }
77    $val = $num1 / $num2;
78  } else {
79    my $x = tutorial::InvalidOperation->new();
80    $x->whatOp($op);
81    $x->why('Invalid operation');
82    die $x;
83  }
84
85  my $log = shared::SharedStruct->new();
86  $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 {
109  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);
113  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
127