def run(self): while not self.stop_work: self.add_water() time.sleep(1) self.check_plants() gprint(" WORKER: no more plants to work with")
def add_water(self): for place in self.garden.grow_places: if place.plant.is_alive and place.water < float( place.plant.WATER / 4): water = self.garden.get_water(place.plant.WATER) gprint( " WORKER: add %.2f water for %s place (%.2f left in tank)" % (water, place.plant.NAME, self.garden.water_tank)) place.add_water(water)
def plant(self, plant): water = self.get_water(plant.WATER) if water: place = growplace.GrowPlace(plant) place.add_water(water) self.grow_places.append(place) else: gprint(" GARDEN: ERROR: no more water in tank, can't plant")
def update_status(self): gprint(" [%10s] ALIVE: %s, GROW UP: %s" % (self.NAME, self.is_alive, self.grow_up)) if self.grow_up or not self.is_alive: return timedelta = datetime.datetime.today() - self.plant_time if timedelta.seconds > self.GROW_TIME: gprint(" [%10s] GROW UP" % self.NAME) self.grow_up = True
def generate_packages(self): times = constants.NUMBER_OF_PACKAGES while times: try: self.sender.send_package(self.generate_package_json()) if times: time.sleep( random.randint( 1, constants.MAX_NEW_PACKAGE_WAIT_TIME_SEC)) except: gprint(" ERROR: Can't send data to server") time.sleep(1) times -= 1
def main(): parse_command_line_arguments() gprint("START") gprint("server: %s:%d" % (socket.gethostbyname(socket.gethostname()), server_port)) gprint("client: %s:%d" % (client_ip, client_port)) my_garden = garden.Garden(constants.INITIAL_TANK_WATER) sender_thread = sender.SenderThread(client_ip, client_port) receiver_thread = receiver.ReceiverThread(my_garden, server_port) sender_thread.start() receiver_thread.start() my_garden.start() sender_thread.join() receiver_thread.join() gprint("STOP")
def run(self): gprint(" START sender thread") self.sender = self.Sender(self.client_ip, self.client_port) while not self.sender.sock: try: self.sender.init_connection() except: gprint( " ERROR: Can't init connection with server, retry in 1 sec" ) self.sender.stop_connection() time.sleep(1) self.generate_packages() self.sender.stop_connection() gprint(" STOP sender thread")
def drink_water(self, water): if water < self.REQUIRED_WATER: gprint(" [%s] DIES" % self.NAME) self.is_alive = False
def send_package(self, package): self.sock.send(package.encode("utf8")) gprint(" >>> %s:%s %s" % (self.client_ip, self.client_port, package))
def init_connection(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.client_ip, int(self.client_port))) gprint(" sender connection created")
def stop_connection(self): if self.sock: self.sock.close() self.sock = None gprint(" sender connection closed")
def __init__(self, water): self.water_tank = float(water) self.nature_thread = nature.Nature(self.grow_places) self.worker_thread = worker.Worker(self) gprint(" GARDEN: tank have %.2f of water" % water)
def stop(self): gprint(" GARDEN: no more packages expected") self.nature_thread.stop()