Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import roslib; roslib.load_manifest('sap_pkg')
00020
00021
00022 import os, xmlrpclib
00023
00024
00025 import rospy
00026
00027
00028 import auction_msgs.msg
00029
00030
00031 import auction_srvs.srv
00032
00033
00034 import auctioneer
00035 import buyer_sap
00036
00037
00038 import random
00039 import math
00040
00041
00042 role_assigned = False
00043 actual_role = 'none'
00044 master_server = ''
00045 buyer_server = ''
00046 auctioneer_server = ''
00047 auctioneer_bid_reception_server = ''
00048
00049
00050
00051
00052
00053 def auctioneer_services():
00054
00055 global auctioneer_server, auctioneer_bid_reception_server
00056
00057
00058
00059
00060
00061
00062
00063 service_path = rospy.get_name()+"/auctioneer_server"
00064
00065 auctioneer_server = rospy.Service(service_path, auction_srvs.srv.AuctioneerService, auctioneer.handle_auctioneer_server_callback)
00066
00067
00068
00069 service_path = rospy.get_name()+"/auctioneer_bid_reception_server"
00070
00071 auctioneer_bid_reception_server = rospy.Service(service_path, auction_srvs.srv.AuctioneerBidReceptionService, auctioneer.handle_auctioneer_bid_reception_server_callback)
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 def buyer_services():
00087
00088 global buyer_server
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 service_path = rospy.get_name()+"/buyer_server"
00101
00102 buyer_server = rospy.Service(service_path, auction_srvs.srv.BuyerService, buyer_sap.handle_buyer_server_callback)
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 def cleanup():
00117
00118 global master_server, auctioneer_server, auctioneer_bid_reception_server, buyer_server, role_assigned, actual_role
00119
00120 print "Cleanup time..."
00121
00122
00123 if actual_role == 'auctioneer':
00124
00125 role_assigned = False
00126 actual_role = 'none'
00127
00128 """
00129 print "auctioneer services will now shutdown"
00130 auctioneer_server.shutdown()
00131 #service_code, service_status, service_uri = master_server.lookupService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_server')
00132 #print 'Service: code:'+str(service_code) + ' status:' + service_status + ' uri:' + service_uri
00133 #master_server.unregisterService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_server',service_uri)
00134
00135 auctioneer_bid_reception_server.shutdown()
00136 #service_code, service_status, service_uri = master_server.lookupService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_bid_reception_server')
00137 #print 'Service: code:'+str(service_code) + ' status:' + service_status + ' uri:' + service_uri
00138 #master_server.unregisterService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_bid_reception_server',service_uri)
00139 print "auctioneer services shutdown succeeded"
00140 """
00141 elif actual_role == 'buyer':
00142
00143 role_assigned = False
00144 actual_role = 'none'
00145 """
00146 print "buyer services shutdown succeeded"
00147 buyer_server.shutdown()
00148 #service_code, service_status, service_uri = master_server.lookupService(rospy.get_name(),str(rospy.get_name())+'/buyer_server')
00149 #print 'Service: code:'+str(service_code) + ' status:' + service_status + ' uri:' + service_uri
00150 #master_server.unregisterService(rospy.get_name(),str(rospy.get_name())+'/buyer_server',service_uri)
00151 print "buyer services shutdown succeeded"
00152 """
00153 else:
00154 return "Something wrong happened in cleanup, actual_role invalid to clean..."
00155
00156
00157
00158
00159
00160
00161
00162 def handle_auction_config_server_callback(auction_req):
00163
00164 global role_assigned, actual_role
00165 global master_server, buyer_server
00166
00167
00168
00169
00170
00171
00172
00173 if rospy.has_param('/num_messages'):
00174 num_messages = rospy.get_param('/num_messages')
00175 num_messages += 2
00176 rospy.set_param('/num_messages', num_messages)
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 if rospy.has_param('/auction_closed'):
00188 if rospy.get_param('/auction_closed') == True:
00189 role_assigned = False
00190
00191
00192
00193
00194
00195
00196
00197 rospy.loginfo(rospy.get_name()+' '+str(role_assigned))
00198
00199
00200 if not role_assigned:
00201 role_assigned = True
00202
00203 if auction_req.role == 'be_auctioneer':
00204 actual_role = 'auctioneer'
00205 auctioneer_services()
00206 return {'response_info':'Auctioneer: '+rospy.get_name()+' ready for auction'}
00207
00208 elif auction_req.role == 'be_buyer':
00209 actual_role = 'buyer'
00210 buyer_services()
00211 return {'response_info':'Buyer: '+rospy.get_name()+' ready for auction'}
00212 else:
00213 return {'response_info':'invalid role requested'}
00214
00215
00216
00217
00218 else:
00219 return {'response_info':'node already have a role'}
00220
00221
00222
00223
00224
00225
00226
00227
00228 if __name__ == "__main__":
00229
00230
00231 rospy.init_node('node', anonymous=True)
00232
00233
00234 service_path = rospy.get_name()+"/auction_config_server"
00235
00236 auction_config_server = rospy.Service(service_path,
00237 auction_srvs.srv.AuctionConfigService,
00238 handle_auction_config_server_callback)
00239
00240
00241
00242
00243
00244 rospy.spin()
00245
00246