Source

protorpc / python / protorpc / remote.py

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
#!/usr/bin/env python
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Remote service library.

This module contains classes that are useful for building remote services that
conform to a standard request and response model.  To conform to this model
a service must be like the following class:

  # Each service instance only handles a single request and is then discarded.
  # Make these objects light weight.
  class Service(object):

    # It must be possible to construct service objects without any parameters.
    # If your constructor needs extra information you should provide a
    # no-argument factory function to create service instances.
    def __init__(self):
      ...

    # Each remote method must use the 'remote' decorator, passing the request
    # and response message types.  The remote method itself must take a single
    # parameter which is an instance of RequestMessage and return an instance
    # of ResponseMessage.
    @method(RequestMessage, ResponseMessage)
    def remote_method(self, request):
      # Return an instance of ResponseMessage.

    # A service object may optionally implement a 'initialize_request_state'
    # method that takes as a parameter a single instance of a RequestState.  If
    # a service does not implement this method it will not receive the request
    # state.
    def initialize_request_state(self, state):
      ...

The 'Service' class is provided as a convenient base class that provides the
above functionality.  It implements all required and optional methods for a
service.  It also has convenience methods for creating factory functions that
can pass persistent global state to a new service instance.

The 'method' decorator is used to declare which methods of a class are
meant to service RPCs.  While this decorator is not responsible for handling
actual remote method invocations, such as handling sockets, handling various
RPC protocols and checking messages for correctness, it does attach information
to methods that responsible classes can examine and ensure the correctness
of the RPC.

When the remote decorator is used on a method, the wrapper method will have a
'remote' property associated with it.  The 'remote' property contains the
request_type and response_type expected by the methods implementation.

On its own, the remote decorator does not provide any support for subclassing
remote methods.  In order to extend a service, one would need to redecorate
the sub-classes methods.  For example:

  class MyService(Service):

    @method(DoSomethingRequest, DoSomethingResponse)
    def do_something(self, request):
      ... implement do-something ...

  class MyBetterService(Service):

    @method(DoSomethingRequest, DoSomethingResponse)
    def do_something(self, request):
      response = super(MyService, self).do_something.remote.method(request)
      ... do something with response ...
      return response

A Service subclass also has a Stub class that can be used with a transport for
making RPCs.  When a stub is created, it is capable of doing both synchronous
and asynchronous RPCs if the underlying transport supports it.  To make a stub
using an HTTP transport do:

  my_service = MyService.Stub(HttpTransport('<my service URL>'))

For synchronous calls, just call the expected methods on the service stub:

  request = DoSomethingRequest()
  ...
  response = my_service.do_something(request)

Each stub instance has an async object that can be used for initiating
asynchronous RPCs if the underlying protocol transport supports it.  To
make an asynchronous call, do:

  rpc = my_service.async.do_something(request)
  response = rpc.get_response()
"""

from __future__ import with_statement

__author__ = 'rafek@google.com (Rafe Kaplan)'

import logging
import os
import sys
import threading
from wsgiref import headers as wsgi_headers

from . import message_types
from . import messages
from . import protobuf
from . import protojson
from . import descriptor
from . import util


__all__ = [
    'ApplicationError',
    'MethodNotFoundError',
    'NetworkError',
    'RequestError',
    'RpcError',
    'ServerError',
    'ServerError',
    'ServiceConfigurationError',
    'ServiceDefinitionError',

    'HttpRequestState',
    'ProtocolConfig',
    'Protocols',
    'RequestState',
    'RpcState',
    'RpcStatus',
    'Service',
    'StubBase',
    'check_rpc_status',
    'get_remote_method_info',
    'is_error_status',
    'method',
    'remote',
]


class ServiceDefinitionError(messages.Error):
  """Raised when a service is improperly defined."""


class ServiceConfigurationError(messages.Error):
  """Raised when a service is incorrectly configured."""


# TODO: Use error_name to map to specific exception message types.
class RpcStatus(messages.Message):
  """Status of on-going or complete RPC.

  Fields:
    state: State of RPC.
    error_name: Error name set by application.  Only set when
      status is APPLICATION_ERROR.  For use by application to transmit
      specific reason for error.
    error_message: Error message associated with status.
  """

  class State(messages.Enum):
    """Enumeration of possible RPC states.

    Values:
      OK: Completed successfully.
      RUNNING: Still running, not complete.
      REQUEST_ERROR: Request was malformed or incomplete.
      SERVER_ERROR: Server experienced an unexpected error.
      NETWORK_ERROR: An error occured on the network.
      APPLICATION_ERROR: The application is indicating an error.
        When in this state, RPC should also set application_error.
    """
    OK = 0
    RUNNING = 1

    REQUEST_ERROR = 2
    SERVER_ERROR = 3
    NETWORK_ERROR = 4
    APPLICATION_ERROR = 5
    METHOD_NOT_FOUND_ERROR = 6

  state = messages.EnumField(State, 1, required=True)
  error_message = messages.StringField(2)
  error_name = messages.StringField(3)


RpcState = RpcStatus.State


class RpcError(messages.Error):
  """Base class for RPC errors.

  Each sub-class of RpcError is associated with an error value from RpcState
  and has an attribute STATE that refers to that value.
  """

  def __init__(self, message, cause=None):
    super(RpcError, self).__init__(message)
    self.cause = cause

  @classmethod
  def from_state(cls, state):
    """Get error class from RpcState.

    Args:
      state: RpcState value.  Can be enum value itself, string or int.

    Returns:
      Exception class mapped to value if state is an error.  Returns None
      if state is OK or RUNNING.
    """
    return _RPC_STATE_TO_ERROR.get(RpcState(state))


class RequestError(RpcError):
  """Raised when wrong request objects received during method invocation."""

  STATE = RpcState.REQUEST_ERROR


class MethodNotFoundError(RequestError):
  """Raised when unknown method requested by RPC."""

  STATE = RpcState.METHOD_NOT_FOUND_ERROR


class NetworkError(RpcError):
  """Raised when network error occurs during RPC."""

  STATE = RpcState.NETWORK_ERROR


class ServerError(RpcError):
  """Unexpected error occured on server."""

  STATE = RpcState.SERVER_ERROR


class ApplicationError(RpcError):
  """Raised for application specific errors.

  Attributes:
    error_name: Application specific error name for exception.
  """

  STATE = RpcState.APPLICATION_ERROR

  def __init__(self, message, error_name=None):
    """Constructor.

    Args:
      message: Application specific error message.
      error_name: Application specific error name.  Must be None, string
      or unicode string.
    """
    super(ApplicationError, self).__init__(message)
    self.error_name = error_name

  def __str__(self):
    return self.args[0]

  def __repr__(self):
    if self.error_name is None:
      error_format = ''
    else:
      error_format = ', %r' % self.error_name
    return '%s(%r%s)' % (type(self).__name__, self.args[0], error_format)


_RPC_STATE_TO_ERROR = {
  RpcState.REQUEST_ERROR: RequestError,
  RpcState.NETWORK_ERROR: NetworkError,
  RpcState.SERVER_ERROR: ServerError,
  RpcState.APPLICATION_ERROR: ApplicationError,
  RpcState.METHOD_NOT_FOUND_ERROR: MethodNotFoundError,
}

class _RemoteMethodInfo(object):
  """Object for encapsulating remote method information.

  An instance of this method is associated with the 'remote' attribute
  of the methods 'invoke_remote_method' instance.

  Instances of this class are created by the remote decorator and should not
  be created directly.
  """

  def __init__(self,
               method,
               request_type,
               response_type):
    """Constructor.

    Args:
      method: The method which implements the remote method.  This is a
        function that will act as an instance method of a class definition
        that is decorated by '@method'.  It must always take 'self' as its
        first parameter.
      request_type: Expected request type for the remote method.
      response_type: Expected response type for the remote method.
    """
    self.__method = method
    self.__request_type = request_type
    self.__response_type = response_type

  @property
  def method(self):
    """Original undecorated method."""
    return self.__method

  @property
  def request_type(self):
    """Expected request type for remote method."""
    if isinstance(self.__request_type, basestring):
      self.__request_type = messages.find_definition(
        self.__request_type,
        relative_to=sys.modules[self.__method.__module__])
    return self.__request_type

  @property
  def response_type(self):
    """Expected response type for remote method."""
    if isinstance(self.__response_type, basestring):
      self.__response_type = messages.find_definition(
        self.__response_type,
        relative_to=sys.modules[self.__method.__module__])
    return self.__response_type


def method(request_type=message_types.VoidMessage,
           response_type=message_types.VoidMessage):
  """Method decorator for creating remote methods.

  Args:
    request_type: Message type of expected request.
    response_type: Message type of expected response.

  Returns:
    'remote_method_wrapper' function.

  Raises:
    TypeError: if the request_type or response_type parameters are not
      proper subclasses of messages.Message.
  """
  if (not isinstance(request_type, basestring) and
      (not isinstance(request_type, type) or
       not issubclass(request_type, messages.Message) or
       request_type is messages.Message)):
    raise TypeError(
        'Must provide message class for request-type.  Found %s',
        request_type)

  if (not isinstance(response_type, basestring) and
      (not isinstance(response_type, type) or
       not issubclass(response_type, messages.Message) or
       response_type is messages.Message)):
    raise TypeError(
        'Must provide message class for response-type.  Found %s',
        response_type)

  def remote_method_wrapper(method):
    """Decorator used to wrap method.

    Args:
      method: Original method being wrapped.

    Returns:
      'invoke_remote_method' function responsible for actual invocation.
      This invocation function instance is assigned an attribute 'remote'
      which contains information about the remote method:
        request_type: Expected request type for remote method.
        response_type: Response type returned from remote method.

    Raises:
      TypeError: If request_type or response_type is not a subclass of Message
        or is the Message class itself.
    """

    def invoke_remote_method(service_instance, request):
      """Function used to replace original method.

      Invoke wrapped remote method.  Checks to ensure that request and
      response objects are the correct types.

      Does not check whether messages are initialized.

      Args:
        service_instance: The service object whose method is being invoked.
          This is passed to 'self' during the invocation of the original
          method.
        request: Request message.

      Returns:
        Results of calling wrapped remote method.

      Raises:
        RequestError: Request object is not of the correct type.
        ServerError: Response object is not of the correct type.
      """
      if not isinstance(request, remote_method_info.request_type):
        raise RequestError('Method %s.%s expected request type %s, '
                           'received %s' %
                           (type(service_instance).__name__,
                            method.__name__,
                            remote_method_info.request_type,
                            type(request)))
      response = method(service_instance, request)
      if not isinstance(response, remote_method_info.response_type):
        raise ServerError('Method %s.%s expected response type %s, '
                          'sent %s' %
                          (type(service_instance).__name__,
                           method.__name__,
                           remote_method_info.response_type,
                           type(response)))
      return response

    remote_method_info = _RemoteMethodInfo(method,
                                           request_type,
                                           response_type)

    invoke_remote_method.remote = remote_method_info
    invoke_remote_method.__name__ = method.__name__
    return invoke_remote_method

  return remote_method_wrapper


def remote(request_type, response_type):
  """Temporary backward compatibility alias for method."""
  logging.warning('The remote decorator has been renamed method.  It will be '
                  'removed in very soon from future versions of ProtoRPC.')
  return method(request_type, response_type)


def get_remote_method_info(method):
  """Get remote method info object from remote method.

  Returns:
    Remote method info object if method is a remote method, else None.
  """
  if not callable(method):
    return None

  try:
    method_info = method.remote
  except AttributeError:
    return None

  if not isinstance(method_info, _RemoteMethodInfo):
    return None

  return method_info


class StubBase(object):
  """Base class for client side service stubs.

  The remote method stubs are created by the _ServiceClass meta-class
  when a Service class is first created.  The resulting stub will
  extend both this class and the service class it handles communications for.

  Assume that there is a service:

    class NewContactRequest(messages.Message):

      name = messages.StringField(1, required=True)
      phone = messages.StringField(2)
      email = messages.StringField(3)

    class NewContactResponse(message.Message):

      contact_id = messages.StringField(1)

    class AccountService(remote.Service):

      @remote.method(NewContactRequest, NewContactResponse):
      def new_contact(self, request):
        ... implementation ...

  A stub of this service can be called in two ways.  The first is to pass in a
  correctly initialized NewContactRequest message:

    request = NewContactRequest()
    request.name = 'Bob Somebody'
    request.phone = '+1 415 555 1234'
    
    response = account_service_stub.new_contact(request)

  The second way is to pass in keyword parameters that correspond with the root
  request message type:

      account_service_stub.new_contact(name='Bob Somebody',
                                       phone='+1 415 555 1234')

  The second form will create a request message of the appropriate type.
  """

  def __init__(self, transport):
    """Constructor.

    Args:
      transport: Underlying transport to communicate with remote service.
    """
    self.__transport = transport

  @property
  def transport(self):
    """Transport used to communicate with remote service."""
    return self.__transport


class _ServiceClass(type):
  """Meta-class for service class."""
   
  def __new_async_method(cls, remote):
    """Create asynchronous method for Async handler.

    Args:
      remote: RemoteInfo to create method for.
    """
    def async_method(self, *args, **kwargs):
      """Asynchronous remote method.

      Args:
        self: Instance of StubBase.Async subclass.

        Stub methods either take a single positional argument when a full
        request message is passed in, or keyword arguments, but not both.

        See docstring for StubBase for more information on how to use remote
        stub methods.

      Returns:
        Rpc instance used to represent asynchronous RPC.
      """
      if args and kwargs:
        raise TypeError('May not provide both args and kwargs')

      if not args:
        # Construct request object from arguments.
        request = remote.request_type()
        for name, value in kwargs.iteritems():
          setattr(request, name, value)
      else:
        # First argument is request object.
        request = args[0]

      return self.transport.send_rpc(remote, request)

    async_method.__name__ = remote.method.__name__
    async_method = util.positional(2)(async_method)
    async_method.remote = remote
    return async_method

  def __new_sync_method(cls, async_method):
    """Create synchronous method for stub.

    Args:
      async_method: asynchronous method to delegate calls to.
    """
    def sync_method(self, *args, **kwargs):
      """Synchronous remote method.

      Args:
        self: Instance of StubBase.Async subclass.
        args: Tuple (request,):
          request: Request object.
        kwargs: Field values for request.  Must be empty if request object
          is provided.

      Returns:
        Response message from synchronized RPC.
      """
      return async_method(self.async, *args, **kwargs).response
    sync_method.__name__ = async_method.__name__
    sync_method.remote = async_method.remote
    return sync_method

  def __create_async_methods(cls, remote_methods):
    """Construct a dictionary of asynchronous methods based on remote methods.

    Args:
      remote_methods: Dictionary of methods with associated RemoteInfo objects.

    Returns:
      Dictionary of asynchronous methods with assocaited RemoteInfo objects.
      Results added to AsyncStub subclass.
    """
    async_methods = {}
    for method_name, method in remote_methods.iteritems():
      async_methods[method_name] = cls.__new_async_method(method.remote)
    return async_methods

  def __create_sync_methods(cls, async_methods):
    """Construct a dictionary of synchronous methods based on remote methods.

    Args:
      async_methods: Dictionary of async methods to delegate calls to.

    Returns:
      Dictionary of synchronous methods with assocaited RemoteInfo objects.
      Results added to Stub subclass.
    """
    sync_methods = {}
    for method_name, async_method in async_methods.iteritems():
      sync_methods[method_name] = cls.__new_sync_method(async_method)
    return sync_methods

  def __new__(cls, name, bases, dct):
    """Instantiate new service class instance."""
    if StubBase not in bases:
      # Collect existing remote methods.
      base_methods = {}
      for base in bases:
        try:
          remote_methods = base.__remote_methods
        except AttributeError:
          pass
        else:
          base_methods.update(remote_methods)

      # Set this class private attribute so that base_methods do not have
      # to be recacluated in __init__.
      dct['_ServiceClass__base_methods'] = base_methods

      for attribute, value in dct.iteritems():
        base_method = base_methods.get(attribute, None)
        if base_method:
          if not callable(value):
            raise ServiceDefinitionError(
              'Must override %s in %s with a method.' % (
                attribute, name))

          if get_remote_method_info(value):
            raise ServiceDefinitionError(
              'Do not use method decorator when overloading remote method %s '
              'on service %s.' %
              (attribute, name))

          base_remote_method_info = get_remote_method_info(base_method)
          remote_decorator = method(
            base_remote_method_info.request_type,
            base_remote_method_info.response_type)
          new_remote_method = remote_decorator(value)
          dct[attribute] = new_remote_method

    return type.__new__(cls, name, bases, dct)

  def __init__(cls, name, bases, dct):
    """Create uninitialized state on new class."""
    type.__init__(cls, name, bases, dct)

    # Only service implementation classes should have remote methods and stub
    # sub classes created.  Stub implementations have their own methods passed
    # in to the type constructor.
    if StubBase not in bases:
      # Create list of remote methods.
      cls.__remote_methods = dict(cls.__base_methods)

      for attribute, value in dct.iteritems():
        value = getattr(cls, attribute)
        remote_method_info = get_remote_method_info(value)
        if remote_method_info:
          cls.__remote_methods[attribute] = value

      # Build asynchronous stub class.
      stub_attributes = {'Service': cls}
      async_methods = cls.__create_async_methods(cls.__remote_methods)
      stub_attributes.update(async_methods)
      async_class = type('AsyncStub', (StubBase, cls), stub_attributes)
      cls.AsyncStub = async_class

      # Constructor for synchronous stub class.
      def __init__(self, transport):
        """Constructor.

        Args:
          transport: Underlying transport to communicate with remote service.
        """
        super(cls.Stub, self).__init__(transport)
        self.async = cls.AsyncStub(transport)

      # Build synchronous stub class.
      stub_attributes = {'Service': cls,
                         '__init__': __init__}
      stub_attributes.update(cls.__create_sync_methods(async_methods))
     
      cls.Stub = type('Stub', (StubBase, cls), stub_attributes)

  @staticmethod
  def all_remote_methods(cls):
    """Get all remote methods of service.

    Returns:
      Dict from method name to unbound method.
    """
    return dict(cls.__remote_methods)


class RequestState(object):
  """Request state information.

  Properties:
    remote_host: Remote host name where request originated.
    remote_address: IP address where request originated.
    server_host: Host of server within which service resides.
    server_port: Post which service has recevied request from.
  """

  @util.positional(1)
  def __init__(self,
               remote_host=None,
               remote_address=None,
               server_host=None,
               server_port=None):
    """Constructor.

    Args:
      remote_host: Assigned to property.
      remote_address: Assigned to property.
      server_host: Assigned to property.
      server_port: Assigned to property.
    """
    self.__remote_host = remote_host
    self.__remote_address = remote_address
    self.__server_host = server_host
    self.__server_port = server_port

  @property
  def remote_host(self):
    return self.__remote_host

  @property
  def remote_address(self):
    return self.__remote_address

  @property
  def server_host(self):
    return self.__server_host

  @property
  def server_port(self):
    return self.__server_port

  def _repr_items(self):
    for name in ['remote_host',
                 'remote_address',
                 'server_host',
                 'server_port']:
      yield name, getattr(self, name)

  def __repr__(self):
    """String representation of state."""
    state = [self.__class__.__name__]
    for name, value in self._repr_items():
      if value:
        state.append('%s=%r' % (name, value))

    return '<%s>' % (' '.join(state),)


class HttpRequestState(RequestState):
  """HTTP request state information.

  NOTE: Does not attempt to represent certain types of information from the
  request such as the query string as query strings are not permitted in
  ProtoRPC URLs unless required by the underlying message format.

  Properties:
    headers: wsgiref.headers.Headers instance of HTTP request headers.
    http_method: HTTP method as a string.
    service_path: Path on HTTP service where service is mounted.  This path
      will not include the remote method name.
  """

  @util.positional(1)
  def __init__(self, 
               http_method=None,
               service_path=None,
               headers=None,
               **kwargs):
    """Constructor.

    Args:
      Same as RequestState, including:
        http_method: Assigned to property.
        service_path: Assigned to property.
        headers: HTTP request headers.  If instance of Headers, assigned to
          property without copying.  If dict, will convert to name value pairs
          for use with Headers constructor.  Otherwise, passed as parameters to
          Headers constructor.
    """
    super(HttpRequestState, self).__init__(**kwargs)

    self.__http_method = http_method
    self.__service_path = service_path
    
    # Initialize headers.
    if isinstance(headers, dict):
      header_list = []
      for key, value in sorted(headers.items()):
        if not isinstance(value, list):
          value = [value]
        for item in value:
          header_list.append((key, item))
        headers = header_list
    self.__headers = wsgi_headers.Headers(headers or [])

  @property
  def http_method(self):
    return self.__http_method

  @property
  def service_path(self):
    return self.__service_path

  @property
  def headers(self):
    return self.__headers

  def _repr_items(self):
    for item in super(HttpRequestState, self)._repr_items():
      yield item

    for name in ['http_method', 'service_path']:
      yield name, getattr(self, name)

    yield 'headers', list(self.headers.items())


class Service(object):
  """Service base class.

  Base class used for defining remote services.  Contains reflection functions,
  useful helpers and built-in remote methods.

  Services are expected to be constructed via either a constructor or factory
  which takes no parameters.  However, it might be required that some state or
  configuration is passed in to a service across multiple requests.

  To do this, define parameters to the constructor of the service and use
  the 'new_factory' class method to build a constructor that will transmit
  parameters to the constructor.  For example:

    class MyService(Service):

      def __init__(self, configuration, state):
        self.configuration = configuration
        self.state = state

    configuration = MyServiceConfiguration()
    global_state = MyServiceState()

    my_service_factory = MyService.new_factory(configuration,
                                               state=global_state)

  The contract with any service handler is that a new service object is created
  to handle each user request, and that the construction does not take any
  parameters.  The factory satisfies this condition:

    new_instance = my_service_factory()
    assert new_instance.state is global_state

  Attributes:
    request_state: RequestState set via initialize_request_state.
  """

  __metaclass__ = _ServiceClass

  __request_state = None

  @classmethod
  def all_remote_methods(cls):
    """Get all remote methods for service class.

    Built-in methods do not appear in the dictionary of remote methods.

    Returns:
      Dictionary mapping method name to remote method.
    """
    return _ServiceClass.all_remote_methods(cls)

  @classmethod
  def new_factory(cls, *args, **kwargs):
    """Create factory for service.

    Useful for passing configuration or state objects to the service.  Accepts
    arbitrary parameters and keywords, however, underlying service must accept
    also accept not other parameters in its constructor.

    Args:
      args: Args to pass to service constructor.
      kwargs: Keyword arguments to pass to service constructor.

    Returns:
      Factory function that will create a new instance and forward args and
      keywords to the constructor.
    """
    def service_factory():
      return cls(*args, **kwargs)

    # Update docstring so that it is easier to debug.
    full_class_name = '%s.%s' % (cls.__module__, cls.__name__)
    service_factory.func_doc = (
        'Creates new instances of service %s.\n\n'
        'Returns:\n'
        '  New instance of %s.'
        % (cls.__name__, full_class_name))

    # Update name so that it is easier to debug the factory function.
    service_factory.func_name = '%s_service_factory' % cls.__name__

    service_factory.service_class = cls

    return service_factory

  def initialize_request_state(self, request_state):
    """Save request state for use in remote method.

    Args:
      request_state: RequestState instance.
    """
    self.__request_state = request_state

  @classmethod
  def definition_name(cls):
    """Get definition name for Service class.

    Package name is determined by the global 'package' attribute in the
    module that contains the Service definition.  If no 'package' attribute
    is available, uses module name.  If no module is found, just uses class
    name as name.

    Returns:
      Fully qualified service name.
    """
    try:
      return cls.__definition_name
    except AttributeError:
      outer_definition_name = cls.outer_definition_name()
      if outer_definition_name is None:
        cls.__definition_name = cls.__name__
      else:
        cls.__definition_name = '%s.%s' % (outer_definition_name, cls.__name__)

      return cls.__definition_name

  @classmethod
  def outer_definition_name(cls):
    """Get outer definition name.

    Returns:
      Package for service.  Services are never nested inside other definitions.
    """
    return cls.definition_package()

  @classmethod
  def definition_package(cls):
    """Get package for service.

    Returns:
      Package name for service.
    """
    try:
      return cls.__definition_package
    except AttributeError:
      cls.__definition_package = util.get_package_for_module(cls.__module__)

    return cls.__definition_package


  @property
  def request_state(self):
    """Request state associated with this Service instance."""
    return self.__request_state


def is_error_status(status):
  """Function that determines whether the RPC status is an error.

  Args:
    status: Initialized RpcStatus message to check for errors.
  """
  status.check_initialized()
  return RpcError.from_state(status.state) is not None


def check_rpc_status(status):
  """Function converts an error status to a raised exception.

  Args:
    status: Initialized RpcStatus message to check for errors.

  Raises:
    RpcError according to state set on status, if it is an error state.
  """
  status.check_initialized()
  error_class = RpcError.from_state(status.state)
  if error_class is not None:
    if error_class is ApplicationError:
      raise error_class(status.error_message, status.error_name)
    else:
      raise error_class(status.error_message)


class ProtocolConfig(object):
  """Configuration for single protocol mapping.

  A read-only protocol configuration provides a given protocol implementation
  with a name and a set of content-types that it recognizes.

  Properties:
    protocol: The protocol implementation for configuration (usually a module,
      for example, protojson, protobuf, etc.).  This is an object that has the
      following attributes:
        CONTENT_TYPE: Used as the default content-type if default_content_type
          is not set.
        ALTERNATIVE_CONTENT_TYPES (optional): A list of alternative
          content-types to the default that indicate the same protocol.
        encode_message: Function that matches the signature of
          ProtocolConfig.encode_message.  Used for encoding a ProtoRPC message.
        decode_message: Function that matches the signature of
          ProtocolConfig.decode_message.  Used for decoding a ProtoRPC message.
    name: Name of protocol configuration.
    default_content_type: The default content type for the protocol.  Overrides
      CONTENT_TYPE defined on protocol.
    alternative_content_types: A list of alternative content-types supported
      by the protocol.  Must not contain the default content-type, nor
      duplicates.  Overrides ALTERNATIVE_CONTENT_TYPE defined on protocol.
    content_types: A list of all content-types supported by configuration.
      Combination of default content-type and alternatives.
  """

  def __init__(self,
               protocol,
               name,
               default_content_type=None,
               alternative_content_types=None):
    """Constructor.

    Args:
      protocol: The protocol implementation for configuration.
      name: The name of the protocol configuration.
      default_content_type: The default content-type for protocol.  If none
        provided it will check protocol.CONTENT_TYPE.
      alternative_content_types:  A list of content-types.  If none provided,
        it will check protocol.ALTERNATIVE_CONTENT_TYPES.  If that attribute
        does not exist, will be an empty tuple.

    Raises:
      ServiceConfigurationError if there are any duplicate content-types.
    """
    self.__protocol = protocol
    self.__name = name
    self.__default_content_type = (default_content_type or
                                   protocol.CONTENT_TYPE).lower()
    if alternative_content_types is None:
      alternative_content_types = getattr(protocol,
                                          'ALTERNATIVE_CONTENT_TYPES',
                                          ())
    self.__alternative_content_types = tuple(
      content_type.lower() for content_type in alternative_content_types)
    self.__content_types = (
      (self.__default_content_type,) + self.__alternative_content_types)

    # Detect duplicate content types in definition.
    previous_type = None
    for content_type in sorted(self.content_types):
      if content_type == previous_type:
        raise ServiceConfigurationError(
          'Duplicate content-type %s' % content_type)
      previous_type = content_type

  @property
  def protocol(self):
    return self.__protocol

  @property
  def name(self):
    return self.__name

  @property
  def default_content_type(self):
    return self.__default_content_type

  @property
  def alternate_content_types(self):
    return self.__alternative_content_types

  @property
  def content_types(self):
    return self.__content_types      

  def encode_message(self, message):
    """Encode message.

    Args:
      message: Message instance to encode.

    Returns:
      String encoding of Message instance encoded in protocol's format.
    """
    return self.__protocol.encode_message(message)

  def decode_message(self, message_type, encoded_message):
    """Decode buffer to Message instance.

    Args:
      message_type: Message type to decode data to.
      encoded_message: Encoded version of message as string.

    Returns:
      Decoded instance of message_type.
    """
    return self.__protocol.decode_message(message_type, encoded_message)


class Protocols(object):
  """Collection of protocol configurations.

  Used to describe a complete set of content-type mappings for multiple
  protocol configurations.

  Properties:
    names: Sorted list of the names of registered protocols.
    content_types: Sorted list of supported content-types.
  """

  __default_protocols = None
  __lock = threading.Lock()

  def __init__(self):
    """Constructor."""
    self.__by_name = {}
    self.__by_content_type = {}

  def add_protocol_config(self, config):
    """Add a protocol configuration to protocol mapping.

    Args:
      config: A ProtocolConfig.

    Raises:
      ServiceConfigurationError if protocol.name is already registered
        or any of it's content-types are already registered.
    """
    if config.name in self.__by_name:
      raise ServiceConfigurationError(
        'Protocol name %r is already in use' % config.name)
    for content_type in config.content_types:
      if content_type in self.__by_content_type:
        raise ServiceConfigurationError(
          'Content type %r is already in use' % content_type)

    self.__by_name[config.name] = config
    self.__by_content_type.update((t, config) for t in config.content_types)

  def add_protocol(self, *args, **kwargs):
    """Add a protocol configuration from basic parameters.

    Simple helper method that creates and registeres a ProtocolConfig instance.
    """
    self.add_protocol_config(ProtocolConfig(*args, **kwargs))

  @property
  def names(self):
    return tuple(sorted(self.__by_name))

  @property
  def content_types(self):
    return tuple(sorted(self.__by_content_type))

  def lookup_by_name(self, name):
    """Look up a ProtocolConfig by name.

    Args:
      name: Name of protocol to look for.

    Returns:
      ProtocolConfig associated with name.

    Raises:
      KeyError if there is no protocol for name.
    """
    return self.__by_name[name.lower()]

  def lookup_by_content_type(self, content_type):
    """Look up a ProtocolConfig by content-type.

    Args:
      content_type: Content-type to find protocol configuration for.

    Returns:
      ProtocolConfig associated with content-type.

    Raises:
      KeyError if there is no protocol for content-type.
    """
    return self.__by_content_type[content_type.lower()]

  @classmethod
  def new_default(cls):
    """Create default protocols configuration.

    Returns:
      New Protocols instance configured for protobuf and protorpc.
    """
    protocols = cls()
    protocols.add_protocol(protobuf, 'protobuf')
    protocols.add_protocol(protojson, 'protojson')
    return protocols

  @classmethod
  def get_default(cls):
    """Get the global default Protocols instance.

    Returns:
      Current global default Protocols instance.
    """
    default_protocols = cls.__default_protocols
    if default_protocols is None:
      with cls.__lock:
        default_protocols = cls.__default_protocols
        if default_protocols is None:
          default_protocols = cls.new_default()
          cls.__default_protocols = default_protocols
    return default_protocols

  @classmethod
  def set_default(cls, protocols):
    """Set the global default Protocols instance.

    Args:
      protocols: A Protocols instance.

    Raises:
      TypeError if protocols is not an instance of Protocols.
    """
    if not isinstance(protocols, Protocols):
      raise TypeError(
        'Expected value of type "Protocols", found %r' % protocols)
    with cls.__lock:
      cls.__default_protocols = protocols