'Respond to an incoming push
@param r: the S3Request
@param attr: the controller attributes'

def __receive(self, r, **attr):

    mixed = attr.get('mixed', False)
    get_vars = r.get_vars
    s3db = current.s3db
    db = current.db
    repository_uuid = get_vars.get('repository')
    connector = None
    if repository_uuid:
        rtable = s3db.sync_repository
        query = (rtable.uuid == repository_uuid)
        row = current.db(query).select(limitby=(0, 1)).first()
        if row:
            connector = S3SyncRepository(row)
    if (connector is None):
        r.error(403, 'Registration    required')
    current.log.debug(('S3Sync    PUSH    from    %s    (%s)' % (connector.name, connector.apitype)))
    default_update_policy = S3ImportItem.POLICY.NEWER
    default_conflict_policy = S3ImportItem.POLICY.MASTER
    ttable = s3db.sync_task
    if (not mixed):
        query = (((ttable.repository_id == connector.id) & (ttable.resource_name == r.tablename)) & (ttable.deleted != True))
        task = db(query).select(limitby=(0, 1)).first()
    else:
        task = None
    last_sync = None
    if task:
        strategy = task.strategy
        update_policy = (task.update_policy or default_update_policy)
        conflict_policy = (task.conflict_policy or default_conflict_policy)
        if (update_policy not in ('THIS', 'OTHER')):
            last_sync = task.last_pull
    else:
        policies = S3ImportItem.POLICY
        p = get_vars.get('update_policy', None)
        values = {'THIS': 'OTHER', 'OTHER': 'THIS'}
        switch = (lambda p: (((p in values) and values[p]) or p))
        if (p and (p in policies)):
            p = switch(p)
            update_policy = policies[p]
        else:
            update_policy = default_update_policy
        p = get_vars.get('conflict_policy', None)
        if (p and (p in policies)):
            p = switch(p)
            conflict_policy = policies[p]
        else:
            conflict_policy = default_conflict_policy
        msince = get_vars.get('msince', None)
        if (msince is not None):
            last_sync = s3_parse_datetime(msince)
        s = get_vars.get('strategy', None)
        if s:
            s = str(s).split(',')
            methods = S3ImportItem.METHOD
            strategy = [method for method in methods.values() if (method in s)]
        else:
            strategy = ttable.strategy.default
    source = r.read_body()
    resource = r.resource
    try:
        result = connector.receive(source, resource, strategy=strategy, update_policy=update_policy, conflict_policy=conflict_policy, last_sync=last_sync, onconflict=self.onconflict, mixed=mixed)
    except IOError:
        current.auth.permission.fail()
    except SyntaxError:
        e = sys.exc_info()[1]
        r.error(400, e)
    except NotImplementedError:
        r.error(405, 'Synchronization    method    not    supported    for    repository')
    log = self.log
    log.write(repository_id=connector.id, resource_name=('mixed' if mixed else resource.tablename), transmission=log.IN, mode=log.PUSH, action='receive', remote=result.get('remote', False), result=result.get('status', log.NONE), message=result.get('message', ''))
    return result.get('response')
