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('k-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_k_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 service_path = rospy.get_name()+"/auctioneer_server"
00059
00060 auctioneer_server = rospy.Service(service_path,
00061 auction_srvs.srv.AuctioneerService,
00062 auctioneer.handle_auctioneer_server_callback)
00063
00064
00065 service_path = rospy.get_name()+"/auctioneer_bid_reception_server"
00066
00067 auctioneer_bid_reception_server = rospy.Service(service_path,
00068 auction_srvs.srv.AuctioneerBidReceptionService,
00069 auctioneer.handle_auctioneer_bid_reception_server_callback)
00070
00071
00072
00073
00074
00075
00076
00077
00078 def buyer_services():
00079
00080 global buyer_server
00081
00082
00083 service_path = rospy.get_name()+"/buyer_server"
00084
00085 buyer_server = rospy.Service(service_path,
00086 auction_srvs.srv.BuyerService,
00087 buyer_k_sap.handle_buyer_server_callback)
00088
00089
00090
00091
00092
00093
00094
00095
00096 def cleanup():
00097
00098 global master_server, auctioneer_server, auctioneer_bid_reception_server, buyer_server, role_assigned, actual_role
00099
00100 print "Cleanup time..."
00101
00102
00103 if actual_role == 'auctioneer':
00104
00105 role_assigned = False
00106 actual_role = 'none'
00107
00108 """
00109 print "auctioneer services will now shutdown"
00110 auctioneer_server.shutdown()
00111 #service_code, service_status, service_uri = master_server.lookupService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_server')
00112 #print 'Service: code:'+str(service_code) + ' status:' + service_status + ' uri:' + service_uri
00113 #master_server.unregisterService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_server',service_uri)
00114
00115 auctioneer_bid_reception_server.shutdown()
00116 #service_code, service_status, service_uri = master_server.lookupService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_bid_reception_server')
00117 #print 'Service: code:'+str(service_code) + ' status:' + service_status + ' uri:' + service_uri
00118 #master_server.unregisterService(rospy.get_name(),str(rospy.get_name())+'/auctioneer_bid_reception_server',service_uri)
00119 print "auctioneer services shutdown succeeded"
00120 """
00121 elif actual_role == 'buyer':
00122
00123 role_assigned = False
00124 actual_role = 'none'
00125 """
00126 print "buyer services shutdown succeeded"
00127 buyer_server.shutdown()
00128 #service_code, service_status, service_uri = master_server.lookupService(rospy.get_name(),str(rospy.get_name())+'/buyer_server')
00129 #print 'Service: code:'+str(service_code) + ' status:' + service_status + ' uri:' + service_uri
00130 #master_server.unregisterService(rospy.get_name(),str(rospy.get_name())+'/buyer_server',service_uri)
00131 print "buyer services shutdown succeeded"
00132 """
00133 else:
00134 return "Something wrong happened in cleanup, actual_role invalid to clean..."
00135
00136
00137
00138
00139
00140
00141 def handle_auction_config_server_callback(auction_req):
00142
00143 global role_assigned, actual_role
00144 global master_server, buyer_server
00145
00146
00147
00148
00149
00150
00151
00152 if rospy.has_param('/num_messages'):
00153 num_messages = rospy.get_param('/num_messages')
00154 num_messages += 2
00155 rospy.set_param('/num_messages', num_messages)
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 if rospy.has_param('/auction_closed'):
00167 if rospy.get_param('/auction_closed') == True:
00168 role_assigned = False
00169
00170
00171
00172
00173
00174
00175
00176 rospy.loginfo(rospy.get_name()+' '+str(role_assigned))
00177
00178
00179 if not role_assigned:
00180 role_assigned = True
00181
00182 if auction_req.role == 'be_auctioneer':
00183 actual_role = 'auctioneer'
00184 auctioneer_services()
00185 return {'response_info':'Auctioneer: '+rospy.get_name()+' ready for auction'}
00186
00187 elif auction_req.role == 'be_buyer':
00188 actual_role = 'buyer'
00189 buyer_services()
00190 return {'response_info':'Buyer: '+rospy.get_name()+' ready for auction'}
00191 else:
00192 return {'response_info':'invalid role requested'}
00193
00194
00195
00196
00197 else:
00198 return {'response_info':'node already have a role'}
00199
00200
00201
00202
00203
00204
00205
00206 if __name__ == "__main__":
00207
00208
00209 rospy.init_node('node', anonymous=True)
00210
00211
00212 service_path = rospy.get_name()+"/auction_config_server"
00213
00214 auction_config_server = rospy.Service(service_path,
00215 auction_srvs.srv.AuctionConfigService,
00216 handle_auction_config_server_callback)
00217
00218
00219
00220
00221
00222 rospy.spin()
00223
00224