예제 #1
0
def handle_request(req):
    command = req['command']
    print("Description: ", req['userDesc'])

    if command == u'NEW-USER':
        print('Handle')

    elif command == u'UPLOAD':
        des = Design(req['filePath'], req['userName'])
        des_name = des.full_name
        desc = Semant(req['userName'], req['fileName'], req['userDesc'])

        print "Coordinates of the design: "
        print des.project()

        result = des.to_stl(req['fileDir'])
        result['properties'] = des.project()
        result['score'] = des.score()

        # compute distance matrix and other information
        DesDom.add_design(des)
        SemDom.add_desc(desc)

        print 'Shape matrix: ', DesDom.dmat
        print 'Semantic matrix: ', SemDom.dmat

        log('UPLOAD', req['filePath'], 'by', req['userName'], '\n\tPROPERTIES',
            str(des.project()))

        return json.dumps(result)

    elif command == u'RECOMMEND':
        recomms = DesDom.recommend(req['userName'], req['condition'],
                                   RECOMM_NUM)
        names = map(lambda x: x[0], recomms)
        paths = map(render_path, names)
        result = {'recommendations': paths}

        log('RECOMMEND', 'to', req['userName'], '\n\tDESIGNS', str(paths))

        return json.dumps(result)

    elif command == u'DELETE':
        print('Received DELETE request')
        print('Request: ')
        print(req)

        DesDom.remove_design(req['userName'], req['designName'])
        SemDom.remove_desc(req['userName'], req['fileName'])

        result = {'delete': 'SUCCESS'}  # error handling required

        log('DELETE', 'design', req['designName'], 'by', req['userName'])

        return json.dumps(result)

    else:
        print('Handle')
예제 #2
0
    def handle(self):
        self.data = json.loads(self.request.recv(1024).strip())
        command = self.data['command']

        if VERBOSE:
            print("{} asked: {}".format(self.client_address[0], command))

        if command == u'NEW-USER':
            if VERBOSE:
                print("NEW-USER command not implemented")

        elif command == u'UPLOAD':
            des = Design(self.data['filePath'], self.data['userName'])
            des_name = des.full_name
            desc = Semant(self.data['userName'], self.data['fileName'],
                          self.data['userDesc'])

            if VERBOSE:
                print("Design projection: {}".format(des.project()))

            result = des.to_stl(self.data['fileDir'])
            result['properties'] = des.project()
            result['score'] = des.score()

            # Compute matrices
            DesDom.add_design(des)
            SemDom.add_desc(desc)

            if VERBOSE and PRINT_MAT:
                print('Shape Matrix:')
                print(DesDom.dmat)
                print('Semantic Matrix:')
                print(SemDom.dmat)

            log('UPLOAD', self.data['filePath'], 'by', self.data['userName'],
                '\n\tPROPERTIES', str(des.project()))

            self.request.sendall(json.dumps(result))

        elif command == u'RECOMMEND':
            recomms = DesDom.recommend(self.data['userName'],
                                       self.data['condition'], RECOMM_NUM)
            names = map(lambda x: x[0], recomms)
            paths = map(render_path, names)
            result = {'recommendations': paths}

            if VERBOSE:
                print("Recommending {} to {}".format(result,
                                                     self.data['userName']))

            log('RECOMMEND', 'to', self.data['userName'], '\n\tDESIGNS',
                str(paths))

            self.request.sendall(json.dumps(result))

        elif command == u'DELETE':
            try:
                DesDom.remove_design(self.data['userName'],
                                     self.data['designName'])
                SemDom.remove_desc(self.data['userName'],
                                   self.data['fileName'])

                result = {'delete': 'SUCCESS'}
                log('DELETE', 'design', self.data['designName'], 'by',
                    self.data['userName'])

                self.request.sendall(json.dumps(result))

            except Exception as e:
                print(e)
        else:
            print("{} NOT_IMPLEMENTED".format(command))