1 #!/usr/bin/env python 2 # 3 # Copyright 2010 Google Inc. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Echo service demo. 19 20 Implements a simple echo service. The request and response objects are 21 the same message that contains numerous different fields useful for testing and 22 illustrating the forms interface. 23 """ 24 25 __author__ = 'rafek (at] google.com (Rafe Kaplan)' 26 27 import appengine_config 28 29 import logging 30 import time 31 32 from protorpc import messages 33 from protorpc import remote 34 from protorpc.webapp import service_handlers 35 36 package = 'protorpc.echo' 37 38 39 class SubMessage(messages.Message): 40 """A sub-message that can be required by EchoData.""" 41 42 value = messages.StringField(1) 43 44 45 class EchoData(messages.Message): 46 """Echo message. 47 48 Contains all relevant ProtoRPC data-types including recursive reference 49 to itself in nested and repeated form. 50 """ 51 52 class Color(messages.Enum): 53 """A simple enumeration type.""" 54 55 RED = 1 56 GREEN = 2 57 BLUE = 3 58 59 # A required field with a default. 60 required = messages.EnumField(Color, 1, 61 required=True, 62 default=Color.BLUE) 63 required_message = messages.MessageField(SubMessage, 18, required=True) 64 65 # Optional fields. 66 a_string = messages.StringField(2) 67 an_int = messages.IntegerField(3) 68 a_float = messages.FloatField(4) 69 a_bool = messages.BooleanField(5) 70 a_bytes = messages.BytesField(6) 71 a_color = messages.EnumField(Color, 7) 72 an_echo = messages.MessageField('EchoData', 8) 73 74 # Repeated fields. 75 strings = messages.StringField(9, repeated=True) 76 ints = messages.IntegerField(10, repeated=True) 77 floats = messages.FloatField(11, repeated=True) 78 bools = messages.BooleanField(12, repeated=True); 79 bytes = messages.BytesField(13, repeated=True) 80 colors = messages.EnumField(Color, 14, repeated=True) 81 echos = messages.MessageField('EchoData', 15, repeated=True) 82 83 # With defaults 84 default_string = messages.StringField(19, default='a default') 85 default_int = messages.IntegerField(20, default=30) 86 default_float = messages.FloatField(21, default=3.1415) 87 default_bool = messages.BooleanField(22, default=True) 88 default_bytes = messages.BytesField(23, default='YSBieXRlcw==') 89 default_color = messages.EnumField(Color, 24, default=Color.GREEN) 90 91 # If want_time is set to True, the response will contain current seconds 92 # since epoch. 93 want_time = messages.BooleanField(16) 94 time = messages.IntegerField(17) 95 96 97 class EchoService(remote.Service): 98 """Echo service echos response to client.""" 99 100 @remote.method(EchoData, EchoData) 101 def echo(self, request): 102 """Echo method.""" 103 logging.info('\n'.join( 104 ['Received request:', 105 ' Host = %s' % self.request_state.remote_host, 106 ' IP Address = %s' % self.request_state.remote_address, 107 ])) 108 if request.want_time: 109 request.time = int(time.time()) 110 return request 111 112 113 def main(): 114 service_handlers.run_services( 115 [('/echo', EchoService), 116 ]) 117 118 119 if __name__ == '__main__': 120 main() 121