'Parse an incoming datagram. Check the action and based on that, send a response.'

def datagramReceived(self, response, (host, port)):

    (connection_id, action, transaction_id) = struct.unpack_from('!qii', response, 0)
    if ((action == 0) and (connection_id != UDP_TRACKER_INIT_CONNECTION_ID)):
        self.send_error(host, port, 'invalid    protocol')
    self.transaction_id = transaction_id
    if (action == TRACKER_ACTION_CONNECT):
        self.send_connection_reply(host, port)
    elif (action == TRACKER_ACTION_SCRAPE):
        if ((len(response) - 16) < LENGTH_INFOHASH):
            self.send_error(host, port, 'no    infohash')
            return
        num_infohashes = ((len(response) - 16) / LENGTH_INFOHASH)
        infohashes = []
        for ind in xrange(num_infohashes):
            tup = struct.unpack_from((('!' + str(LENGTH_INFOHASH)) + 'c'), response, (16 + (ind * LENGTH_INFOHASH)))
            infohash = ''.join(tup)
            if (not self.tracker_session.tracker_info.has_info_about_infohash(infohash)):
                self.send_error(host, port, ('no    info    about    hash    %s' % infohash))
                return
            infohashes.append(infohash)
        self.send_scrape_reply(host, port, infohashes)
