Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
#!/usr/bin/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 tutorial::Calculator;
27
 
28
package CalculatorHandler;
29
use base qw(tutorial::CalculatorIf);
30
 
31
sub new {
32
    my $classname = shift;
33
    my $self      = {};
34
 
35
    return bless($self,$classname);
36
}
37
 
38
 
39
sub ping
40
{
41
  print "ping()\n";
42
}
43
 
44
sub add
45
{
46
  my($self, $n1, $n2) = @_;
47
  printf("add(%d,%d)\n", $n1, $n2);
48
  return $n1 + $n2;
49
}
50
 
51
sub calculate
52
{
53
  my($self, $logid, $work) = @_;
54
  my $op   = $work->{op};
55
  my $num1 = $work->{num1};
56
  my $num2 = $work->{num2};
57
  printf("calculate(%d, %d %d %d)\n", $logid, $num1, $num2, $op);
58
 
59
  my $val;
60
 
61
  if ($op == tutorial::Operation::ADD) {
62
    $val = $num1 + $num2;
63
  } elsif ($op == tutorial::Operation::SUBTRACT) {
64
    $val = $num1 - $num2;
65
  } elsif ($op == tutorial::Operation::MULTIPLY) {
66
    $val = $num1 * $num2;
67
  } elsif ($op == tutorial::Operation::DIVIDE) {
68
    if ($num2 == 0)
69
    {
70
      my $x = new tutorial::InvalidOperation;
71
      $x->what($op);
72
      $x->why('Cannot divide by 0');
73
      die $x;
74
    }
75
    $val = $num1 / $num2;
76
  } else {
77
    my $x = new tutorial::InvalidOperation;
78
    $x->what($op);
79
    $x->why('Invalid operation');
80
    die $x;
81
  }
82
 
83
  my $log = new shared::SharedStruct;
84
  $log->key($logid);
85
  $log->value(int($val));
86
  $self->{log}->{$logid} = $log;
87
 
88
  return $val;
89
}
90
 
91
sub getStruct
92
{
93
  my($self, $key) = @_;
94
  printf("getStruct(%d)\n", $key);
95
  return $self->{log}->{$key};
96
}
97
 
98
sub zip
99
{
100
  my($self) = @_;
101
  print "zip()\n";
102
}
103
 
104
 
105
 
106
eval {
107
  my $handler       = new CalculatorHandler;
108
  my $processor     = new tutorial::CalculatorProcessor($handler);
109
  my $serversocket  = new Thrift::ServerSocket(9090);
110
  my $forkingserver = new Thrift::ForkingServer($processor, $serversocket);
111
  print "Starting the server...\n";
112
  $forkingserver->serve();
113
  print "done.\n";
114
}; if ($@) {
115
  if ($@ =~ m/TException/ and exists $@->{message}) {
116
    my $message = $@->{message};
117
    my $code    = $@->{code};
118
    my $out     = $code . ':' . $message;
119
    die $out;
120
  } else {
121
    die $@;
122
  }
123
}
124