@AccessCheck
    def startShaping(self, tc):
        self.logger.info("Request startShaping {0}".format(tc))
        # Sanity checking
        # IP
        try:
            socket.inet_aton(tc.device.controlledIP)
        except Exception as e:
            return TrafficControlRc(
                code=ReturnCode.INVALID_IP,
                message="Invalid IP {}".format(tc.device.controlledIP))
        # timer
        if tc.timeout < 0:
            return TrafficControlRc(
                code=ReturnCode.INVALID_TIMEOUT,
                message="Invalid Timeout {}".format(tc.timeout))

        new_id = None
        try:
            new_id = self.idmanager.new()
        except Exception as e:
            return TrafficControlRc(
                code=ReturnCode.ID_EXHAUST,
                message="No more session available: {0}".format(e))

        old_id = self._ip_to_id_map.get(tc.device.controlledIP, None)
        old_settings = self._current_shapings.get(
            tc.device.controlledIP, {}
        ).get('tc')

        tcrc = self._shape_interface(
            new_id,
            self.wan,
            tc.device.controlledIP,
            tc.settings.up,
        )
        if tcrc.code != ReturnCode.OK:
            return tcrc
        tcrc = self._shape_interface(
            new_id,
            self.lan,
            tc.device.controlledIP,
            tc.settings.down,
        )
        # If we failed to set shaping for LAN interfaces, we should remove
        # the shaping we just created for the WAN
        if tcrc.code != ReturnCode.OK:
            self._unshape_interface(
                new_id,
                self.wan,
                tc.device.controlledIP,
                tc.settings.up
            )
            return tcrc
        self._add_mapping(new_id, tc)
        self.db_task.queue.put(((tc, tc.timeout + time.time()), 'add_shaping'))
        # if there were an existing id, remove it from dict
        if old_id is not None:
            self._unshape_interface(
                old_id,
                self.wan,
                tc.device.controlledIP,
                old_settings.settings.up,
            )
            self._unshape_interface(
                old_id,
                self.lan,
                tc.device.controlledIP,
                old_settings.settings.down,
            )
            del self._id_to_ip_map[old_id]
            self.idmanager.free(old_id)

        return TrafficControlRc(code=ReturnCode.OK)
Start shaping a connection for a given device.
dataset/ETHPy150Open facebook/augmented-traffic-control/atc/atcd/atcd/AtcdThriftHandlerTask.py/AtcdThriftHandlerTask.startShaping