Guest User

Untitled

a guest
Jan 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import sys
  8.  
  9.  
  10.  
  11. import Ice
  12.  
  13.  
  14.  
  15. Ice.loadSlice('robots.ice --all -I .')
  16.  
  17. import robots
  18.  
  19. Ice.loadSlice('drobots.ice')
  20.  
  21. import drobots
  22.  
  23. class DetectorControllerI(drobots.DetectorController):
  24. def __init__(self,Container):
  25. self.container=Container
  26.  
  27. """
  28.  
  29. DetectorController interface implementation.
  30.  
  31.  
  32.  
  33. It implements the alert method.
  34.  
  35.  
  36.  
  37. Remember: every alert call will include de position, so there is no need
  38.  
  39. to create a DetectorControllerI servant for every detector, you can re-use
  40.  
  41. the same servant (and its proxy) to every "makeDetectorController" petition
  42.  
  43. on the PlayerI
  44.  
  45. """
  46.  
  47. def alert(self, pos, robots_detected, current):
  48.  
  49. """
  50.  
  51. Method that receives a Point with the coordinates where the detector is
  52.  
  53. placed and the number of robots around it. This method is only invoked
  54.  
  55. when at least 1 robot is near to the detector. If there is no robots
  56.  
  57. around it, this method will never be called.
  58.  
  59. """
  60.  
  61. print("Alert: {} robots detected at {},{}".format(
  62.  
  63. robots_detected, pos.x, pos.y))
  64.  
  65.  
  66.  
  67.  
  68.  
  69. class ControllerI(drobots.RobotController):
  70.  
  71. """
  72.  
  73. RobotController interface implementation.
  74.  
  75.  
  76.  
  77. The implementation only retrieve and print the location of the assigned
  78.  
  79. robot
  80.  
  81. """
  82.  
  83. def __init__(self, bot, mines):
  84.  
  85. """
  86.  
  87. ControllerI constructor. Accepts only a "bot" argument, that should be
  88.  
  89. a RobotPrx object, usually sent by the game server.
  90.  
  91. """
  92.  
  93. self.bot = bot
  94.  
  95. self.mines = mines
  96.  
  97.  
  98.  
  99. def turn(self, current):
  100.  
  101. """
  102.  
  103. Method that will be invoked remotely by the server. In this method we
  104.  
  105. should communicate with out Robot
  106.  
  107. """
  108.  
  109. location = self.bot.location()
  110.  
  111. print("Turn of {} at location {},{}".format(
  112.  
  113. id(self), location.x, location.y))
  114.  
  115.  
  116.  
  117. def robotDestroyed(self, current):
  118.  
  119. """
  120.  
  121. Pending implementation:
  122.  
  123. void robotDestroyed();
  124.  
  125. """
  126.  
  127. pass
  128.  
  129.  
  130.  
  131. class DetectorControllerFactoryI(robots.DetectorController):
  132.  
  133. def __init__(self):
  134.  
  135. self.proxy = None # 1 solo proxy
  136.  
  137.  
  138.  
  139. def make(self, Container, current = None):
  140. print("MAKE DE DETECTORCONTROLLER")
  141.  
  142. if self.proxy == None:
  143.  
  144. servant = DetectorControllerI(Container)
  145.  
  146. proxy = current.adapter.addWithUUID(servant)
  147.  
  148. print(proxy)
  149.  
  150. proxy = drobots.DetectorControllerPrx.uncheckedCast(proxy)
  151.  
  152. self.proxy = proxy
  153.  
  154. return self.proxy
  155.  
  156.  
  157.  
  158. class DetectorControllerFactoryServer(Ice.Application):
  159.  
  160. def run(self, argv):
  161.  
  162. broker = self.communicator()
  163.  
  164. servant = DetectorControllerFactoryI()
  165.  
  166. adapter = broker.createObjectAdapter("DetectorAdapter")
  167.  
  168. proxy = adapter.add(servant,
  169.  
  170. broker.stringToIdentity("Detector"))
  171.  
  172. print(proxy)
  173. adapter.activate()
  174.  
  175. sys.stdout.flush()
  176.  
  177.  
  178.  
  179.  
  180.  
  181. self.shutdownOnInterrupt()
  182.  
  183. broker.waitForShutdown()
  184.  
  185.  
  186.  
  187. return 0
  188.  
  189.  
  190.  
  191.  
  192.  
  193. server = DetectorControllerFactoryServer()
  194.  
  195. sys.exit(server.main(sys.argv))
Add Comment
Please, Sign In to add comment