#
# Autogenerated by Thrift Compiler (0.11.0)
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#

require 'thrift'
require 'calculator_types'

module Calculator
  class Client
    include ::Thrift::Client

    def add(num1, num2)
      send_add(num1, num2)
      return recv_add()
    end

    def send_add(num1, num2)
      send_message('add', Add_args, :num1 => num1, :num2 => num2)
    end

    def recv_add()
      result = receive_message(Add_result)
      return result.success unless result.success.nil?
      raise result.err unless result.err.nil?
      raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'add failed: unknown result')
    end

  end

  class Processor
    include ::Thrift::Processor

    def process_add(seqid, iprot, oprot)
      args = read_args(iprot, Add_args)
      result = Add_result.new()
      begin
        result.success = @handler.add(args.num1, args.num2)
      rescue ::InvalidOperation => err
        result.err = err
      end
      write_result(result, oprot, 'add', seqid)
    end

  end

  # HELPER FUNCTIONS AND STRUCTURES

  class Add_args
    include ::Thrift::Struct, ::Thrift::Struct_Union
    NUM1 = 1
    NUM2 = 2

    FIELDS = {
      NUM1 => {:type => ::Thrift::Types::I32, :name => 'num1'},
      NUM2 => {:type => ::Thrift::Types::I32, :name => 'num2'}
    }

    def struct_fields; FIELDS; end

    def validate
    end

    ::Thrift::Struct.generate_accessors self
  end

  class Add_result
    include ::Thrift::Struct, ::Thrift::Struct_Union
    SUCCESS = 0
    ERR = 1

    FIELDS = {
      SUCCESS => {:type => ::Thrift::Types::I32, :name => 'success'},
      ERR => {:type => ::Thrift::Types::STRUCT, :name => 'err', :class => ::InvalidOperation}
    }

    def struct_fields; FIELDS; end

    def validate
    end

    ::Thrift::Struct.generate_accessors self
  end

end

