コード例 #1
0
ファイル: client.py プロジェクト: fiks/sc-web
    def find_links_with_content(self, data):
        """Find sc-links with specified content
        @param data: Content data for search
        @return: Returns list of sc-addrs of founded sc-links. If there are any error, then return None
        """
        # send request
        params = struct.pack('=I%ds' % len(data), len(data), data)
        data = struct.pack('=BBII', sctpCommandType.SCTP_CMD_FIND_LINKS, 0, 0, len(params))
        alldata = data + params

        self.sock.send(alldata)

        # recieve response
        data = self.sock.recv(10)
        cmdCode, cmdId, resCode, resSize = struct.unpack('=BIBI', data)
        if resCode != sctpResultCode.SCTP_RESULT_OK or resSize < 4:
            return None

        res = []
        data = self.sock.recv(resSize)
        resCount = struct.unpack('=I', data[:4])[0]
        for i in xrange(resCount):
            addr = ScAddr(0, 0)
            data = data[4:]
            addr.seg, addr.offset = struct.unpack('=HH', data)
            res.append(addr)

        return res
コード例 #2
0
ファイル: client.py プロジェクト: fiks/sc-web
    def create_arc(self, arc_type, begin_addr, end_addr):
        """Create new arc in sc-memory with specified type and begin, end elements
        @param arc_type: Type of sc-arc
        @param begin_addr: sc-addr of begin arc element
        @param end_addr: sc-addr of end arc element
        @return: If sc-arc was created, then returns it sc-addr; otherwise return None
        """
        # send request
        params = struct.pack('=HHHHH', arc_type, begin_addr.seg, begin_addr.offset, end_addr.seg, end_addr.offset)
        data = struct.pack('=BBII', sctpCommandType.SCTP_CMD_CREATE_ARC, 0, 0, len(params))
        alldata = data + params

        self.sock.send(alldata)

        # recieve response
        data = self.sock.recv(10)
        cmdCode, cmdId, resCode, resSize = struct.unpack('=BIBI', data)
        if resCode != sctpResultCode.SCTP_RESULT_OK:
            return None

        addr = ScAddr(0, 0)
        data = self.sock.recv(4)
        addr.seg, addr.offset = struct.unpack('=HH', data)

        return addr
コード例 #3
0
ファイル: api.py プロジェクト: fiks/sc-web
def scAddrs(request):
    result = "[]"
    if request.is_ajax():
        sctp_client = newSctpClient()

        # parse arguments
        first = True
        arg = None
        arguments = []
        idx = 0
        while first or (arg is not None):
            arg_str = u'%d_' % idx
            arg = ScAddr.parse_from_string(request.GET.get(arg_str, None))
            if arg is not None:
                arguments.append(arg)
            first = False
            idx += 1

        res = {}
        for idtf in arguments:
            addr = sctp_client.find_element_by_system_identifier(idtf)
            if addr is not None:
                res[idtf] = addr

        result = json.dumps(res)

    return HttpResponse(result, 'application/json')
コード例 #4
0
ファイル: client.py プロジェクト: fiks/sc-web
    def create_link(self):
        """Create new sc-link in memory
        @return: If sc-link was created, then returns it sc-addr; otherwise return None
        """
        # send request
        data = struct.pack('=BBII', sctpCommandType.SCTP_CMD_CREATE_LINK, 0, 0, 0)
        alldata = data

        self.sock.send(alldata)

        # recieve response
        data = self.sock.recv(10)
        cmdCode, cmdId, resCode, resSize = struct.unpack('=BIBI', data)
        if resCode != sctpResultCode.SCTP_RESULT_OK:
            return None

        addr = ScAddr(0, 0)
        data = self.sock.recv(4)
        addr.seg, addr.offset = struct.unpack('=HH', data)

        return addr
コード例 #5
0
ファイル: client.py プロジェクト: fiks/sc-web
    def find_element_by_system_identifier(self, idtf_data):
        """Find sc-element by it system identifier
        @param idtf_data: Identifier data for search
        @return: Returns sc-addrs of founded sc-element.
        If there are any error or sc-element wasn't found, then return None
        """
        # send request
        params = struct.pack('=I%ds' % len(idtf_data), len(idtf_data), idtf_data)
        data = struct.pack('=BBII', sctpCommandType.SCTP_CMD_FIND_ELEMENT_BY_SYSITDF, 0, 0, len(params))
        alldata = data + params

        self.sock.send(alldata)

        # recieve response
        data = self.sock.recv(10)
        cmdCode, cmdId, resCode, resSize = struct.unpack('=BIBI', data)
        if resCode != sctpResultCode.SCTP_RESULT_OK or resSize < 4:
            return None

        addr = ScAddr(0, 0)
        data = self.sock.recv(4)
        addr.seg, addr.offset = struct.unpack('=HH', data)

        return addr
コード例 #6
0
ファイル: client.py プロジェクト: fiks/sc-web
    def create_node(self, el_type):
        """Create new sc-node in memory with specified type
        @param el_type:	Type of node that would be created
        @return: If sc-node was created, then returns it sc-addr; otherwise return None
        """

        # send request
        params = struct.pack('=H', el_type)
        data = struct.pack('=BBII', sctpCommandType.SCTP_CMD_CREATE_NODE, 0, 0, len(params))
        alldata = data + params

        self.sock.send(alldata)

        # recieve response
        data = self.sock.recv(10)
        cmdCode, cmdId, resCode, resSize = struct.unpack('=BIBI', data)
        if resCode != sctpResultCode.SCTP_RESULT_OK:
            return None

        addr = ScAddr(0, 0)
        data = self.sock.recv(4)
        addr.seg, addr.offset = struct.unpack('=HH', data)

        return addr
コード例 #7
0
ファイル: api.py プロジェクト: fiks/sc-web
def get_identifier(request):
    result = None
    if request.is_ajax():
        lang_code = ScAddr.parse_from_string(request.GET.get(u'language', None))

        if lang_code is None:
            print "Invalid sc-addr of language"
            return HttpResponse(None)

        # get arguments
        idx = 1
        arguments = []
        arg = ''
        while arg is not None:
            arg = request.GET.get(u'%d_' % idx, None)
            if arg is not None:
                arguments.append(arg)
            idx += 1

        sctp_client = newSctpClient()

        keys = Keynodes(sctp_client)
        keynode_ui_nrel_idtf_language_relation = keys[KeynodeSysIdentifiers.ui_nrel_idtf_language_relation]

        # first of all we need to resolve language relation
        lang_relation_keynode = sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
            lang_code,
            ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
            ScElementType.sc_type_node | ScElementType.sc_type_const,
            ScElementType.sc_type_arc_pos_const_perm,
            keynode_ui_nrel_idtf_language_relation)
        if lang_relation_keynode is None:
            print "Can't resolve keynode for language '%s'" % str(lang_code)
            return HttpResponse(None)

        lang_relation_keynode = lang_relation_keynode[0][2]

        result = {}
        # get requested identifiers for arguments
        for addr_str in arguments:
            addr = ScAddr.parse_from_string(addr_str)
            if addr is None:
                print "Can't parse sc-addr from argument: %s" % addr_str
                return HttpResponse(None)

            identifier = sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                addr,
                ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                ScElementType.sc_type_link,
                ScElementType.sc_type_arc_pos_const_perm,
                lang_relation_keynode)
            idtf_value = None
            if identifier is not None:
                idtf_addr = identifier[0][2]

                # get identifier value
                idtf_value = sctp_client.get_link_content(idtf_addr)
                idtf_value = idtf_value.decode('utf-8')

                result[addr_str] = idtf_value

        result = json.dumps(result)

    return HttpResponse(result, 'application/json')
コード例 #8
0
ファイル: api.py プロジェクト: fiks/sc-web
def doCommand(request):
    result = "[]"
    if request.is_ajax():
        #result = u'[{"type": "node", "id": "1", "identifier": "node1"},' \
        #		 u'{"type": "arc", "id": "2", "begin": "1", "end": "3"},' \
        #		 u'{"type": "node", "id": "3", "identifier": "node2"}]'
        sctp_client = newSctpClient()

        cmd_addr = ScAddr.parse_from_string(request.GET.get(u'cmd', None))
        output_addr = ScAddr.parse_from_string(request.GET.get(u'output', None))
        # parse arguments
        first = True
        arg = None
        arguments = []
        idx = 0
        while first or (arg is not None):
            arg = ScAddr.parse_from_string(request.GET.get(u'%d_' % idx, None))
            if arg is not None:
                # check if sc-element exist
                if sctp_client.check_element(arg):
                    arguments.append(arg)
                else:
                    return HttpResponse(None, 'application/json')

            first = False
            idx += 1

        if (len(arguments) > 0) and (cmd_addr is not None) and (output_addr is not None):
            keys = Keynodes(sctp_client)

            keynode_ui_rrel_commnad = keys[KeynodeSysIdentifiers.ui_rrel_commnad]
            keynode_ui_rrel_command_arguments = keys[KeynodeSysIdentifiers.ui_rrel_command_arguments]
            keynode_ui_nrel_command_result = keys[KeynodeSysIdentifiers.ui_nrel_command_result]
            keynode_ui_user_command_question = keys[KeynodeSysIdentifiers.ui_user_command_question]
            keynode_ui_command_generate_instance = keys[KeynodeSysIdentifiers.ui_command_generate_instance]
            keynode_ui_command_initiated = keys[KeynodeSysIdentifiers.ui_command_initiated]
            keynode_ui_command_finished = keys[KeynodeSysIdentifiers.ui_command_finished]
            keynode_ui_nrel_command_result = keys[KeynodeSysIdentifiers.ui_nrel_command_result]
            keynode_ui_user = keys[KeynodeSysIdentifiers.ui_user]
            keynode_nrel_author = keys[KeynodeSysIdentifiers.nrel_author]
            keynode_ui_nrel_user_answer_formats = keys[KeynodeSysIdentifiers.ui_nrel_user_answer_formats]
            keynode_nrel_translation = keys[KeynodeSysIdentifiers.nrel_translation]
            keynode_nrel_answer = keys[KeynodeSysIdentifiers.question_nrel_answer]
            keynode_question_initiated = keys[KeynodeSysIdentifiers.question_initiated]
            keynode_question = keys[KeynodeSysIdentifiers.question]


            # create command in sc-memory
            inst_cmd_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_command_generate_instance,
                inst_cmd_addr)

            inst_cmd_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, inst_cmd_addr, cmd_addr)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_rrel_commnad, inst_cmd_arc)

            # create arguments
            args_addr = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            args_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, inst_cmd_addr, args_addr)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_rrel_command_arguments,
                args_arc)

            idx = 1
            for arg in arguments:
                arg_arc = sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, args_addr, arg)
                if arg_arc is None:
                    return HttpResponse(None, 'application/json')

                idx_addr = sctp_client.find_element_by_system_identifier(str(u'rrel_%d' % idx))
                if idx_addr is None:
                    return HttpResponse(None, 'application/json')
                idx += 1
                sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, idx_addr, arg_arc)

            # initialize command
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_command_initiated,
                inst_cmd_addr)
            cmd_finished = checkCommandFinished(inst_cmd_addr, keynode_ui_command_finished, sctp_client)
            while cmd_finished is None:
                time.sleep(0.1)
                cmd_finished = checkCommandFinished(inst_cmd_addr, keynode_ui_command_finished, sctp_client)


            # get command result
            cmd_result = sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                inst_cmd_addr,
                ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                ScElementType.sc_type_node | ScElementType.sc_type_const,
                ScElementType.sc_type_arc_pos_const_perm,
                keynode_ui_nrel_command_result)
            if cmd_result is None:
                return HttpResponse(None, 'application/json')

            cmd_result = cmd_result[0][2]

            # @todo support all possible commands
            # try to find question node
            question = sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F,
                keynode_question,
                ScElementType.sc_type_arc_pos_const_perm,
                ScElementType.sc_type_node | ScElementType.sc_type_const,
                ScElementType.sc_type_arc_pos_const_perm,
                cmd_result)
            if question is None:
                return HttpResponse(None, 'application/json')

            question = question[0][2]

            # create author
            user_node = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_user, user_node)

            author_arc = sctp_client.create_arc(ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                question, user_node)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_nrel_author, author_arc)

            # create output formats set
            output_formats_node = sctp_client.create_node(ScElementType.sc_type_node | ScElementType.sc_type_const)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, output_formats_node, output_addr)

            format_arc = sctp_client.create_arc(ScElementType.sc_type_arc_common | ScElementType.sc_type_const,
                question, output_formats_node)
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_ui_nrel_user_answer_formats,
                format_arc)

            # initiate question
            sctp_client.create_arc(ScElementType.sc_type_arc_pos_const_perm, keynode_question_initiated, question)

            # first of all we need to wait answer to this question
            #print sctp_client.iterate_elements(sctpIteratorType.SCTP_ITERATOR_3F_A_A, keynode_question_initiated, 0, 0)

            answer = findAnswer(question, keynode_nrel_answer, sctp_client)
            while answer is None:
                time.sleep(0.1)
                answer = findAnswer(question, keynode_nrel_answer, sctp_client)

            answer_addr = answer[0][2]
            translation = findTranslation(answer_addr, keynode_nrel_translation, sctp_client)
            while translation is None:
                time.sleep(0.1)
                translation = findTranslation(answer_addr, keynode_nrel_translation, sctp_client)

            # get output string
            translation_addr = translation[0][2]
            result = sctp_client.get_link_content(translation_addr);

    return HttpResponse(result, 'application/json')
コード例 #9
0
ファイル: client.py プロジェクト: fiks/sc-web
    def iterate_elements(self, iterator_type, *args):
        """Iterate element by specified template and return results
        """
        params = None
        params_count = None
        if iterator_type == sctpIteratorType.SCTP_ITERATOR_3A_A_F:
            params_count = 3
            params = struct.pack('=BHHHH', iterator_type, args[0], args[1], args[2].seg, args[2].offset)
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_3F_A_A:
            params_count = 3
            params = struct.pack('=BHHHH', iterator_type, args[0].seg, args[0].offset, args[1], args[2])
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_3F_A_F:
            params_count = 3
            params = struct.pack('=BHHHHH', iterator_type, args[0].seg, args[0].offset, args[1], args[2].seg,
                args[2].offset)
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_5_A_A_F_A_A:
            params_count = 5
            params = struct.pack('=BHHHHHH', iterator_type, args[0], args[1], args[2].seg, args[2].offset, args[3],
                args[4])
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_5_A_A_F_A_F:
            params_count = 5
            params = struct.pack('=BHHHHHHH', iterator_type, args[0], args[1], args[2].seg, args[2].offset, args[3],
                args[4].seg, args[4].offset)
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_5_F_A_A_A_A:
            params_count = 5
            params = struct.pack('=BHHHHHH', iterator_type, args[0].seg, args[0].offset, args[1], args[2], args[3],
                args[4])
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_5_F_A_F_A_A:
            params_count = 5
            params = struct.pack('=BHHHHHHH', iterator_type, args[0].seg, args[0].offset, args[1], args[2].seg,
                args[2].offset, args[3], args[4])
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_5_F_A_F_A_F:
            params_count = 5
            params = struct.pack('=BHHHHHHHH', iterator_type, args[0].seg, args[0].offset, args[1], args[2].seg,
                args[2].offset, args[3], args[4].seg, args[4].offset)
        elif iterator_type == sctpIteratorType.SCTP_ITERATOR_5F_A_A_A_F:
            params_count = 5
            params = struct.pack('=BHHHHHHH', iterator_type, args[0].seg, args[0].offset, args[1], args[2], args[3],
                args[4].seg, args[4].offset)

        params_len = len(params)
        # send request
        data = struct.pack('=BBII', sctpCommandType.SCTP_CMD_ITERATE_ELEMENTS, 0, 0, params_len)
        alldata = data + params

        self.sock.send(alldata)

        # recieve response
        data = self.sock.recv(10)
        cmdCode, cmdId, resCode, resSize = struct.unpack('=BIBI', data)
        if resCode != sctpResultCode.SCTP_RESULT_OK or resSize == 0:
            return None

        res_count_data = self.sock.recv(4)
        res_count = struct.unpack('=I', res_count_data)[0]

        if res_count == 0:
            return None

        results = []
        for idx in xrange(res_count):
            result_item = []
            for j in xrange(params_count):
                addr_data = self.sock.recv(4)
                addr = ScAddr(0, 0)
                addr.seg, addr.offset = struct.unpack('=HH', addr_data)
                result_item.append(addr)

            results.append(result_item)

        return results