]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
22 | use strict; | |
23 | use lib '../gen-perl'; | |
24 | use Thrift::Socket; | |
25 | use Thrift::Server; | |
26 | use Thrift::ServerSocket; | |
27 | use tutorial::Calculator; | |
28 | ||
29 | package CalculatorHandler; | |
30 | use base qw(tutorial::CalculatorIf); | |
31 | ||
32 | sub new { | |
33 | my $classname = shift; | |
34 | my $self = {}; | |
35 | ||
36 | return bless($self,$classname); | |
37 | } | |
38 | ||
39 | ||
40 | sub ping | |
41 | { | |
42 | print "ping()\n"; | |
43 | } | |
44 | ||
45 | sub add | |
46 | { | |
47 | my($self, $n1, $n2) = @_; | |
48 | printf("add(%d,%d)\n", $n1, $n2); | |
49 | return $n1 + $n2; | |
50 | } | |
51 | ||
52 | sub 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 | { | |
71 | my $x = tutorial::InvalidOperation->new(); | |
72 | $x->whatOp($op); | |
73 | $x->why('Cannot divide by 0'); | |
74 | die $x; | |
75 | } | |
76 | $val = $num1 / $num2; | |
77 | } else { | |
78 | my $x = tutorial::InvalidOperation->new(); | |
79 | $x->whatOp($op); | |
80 | $x->why('Invalid operation'); | |
81 | die $x; | |
82 | } | |
83 | ||
84 | my $log = shared::SharedStruct->new(); | |
85 | $log->key($logid); | |
86 | $log->value(int($val)); | |
87 | $self->{log}->{$logid} = $log; | |
88 | ||
89 | return $val; | |
90 | } | |
91 | ||
92 | sub getStruct | |
93 | { | |
94 | my($self, $key) = @_; | |
95 | printf("getStruct(%d)\n", $key); | |
96 | return $self->{log}->{$key}; | |
97 | } | |
98 | ||
99 | sub zip | |
100 | { | |
101 | my($self) = @_; | |
102 | print "zip()\n"; | |
103 | } | |
104 | ||
105 | ||
106 | ||
107 | eval { | |
108 | 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); | |
112 | 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 |