Exemplo n.º 1
0
    def test_getBigFile(self, bigTestfile):
        # Read in original data
        with open(bigTestfile, 'rb') as fp:
            data = fp.read()

        # Read via fileClient
        startTime = time.time()
        cmd = projUtils.getFileReqStruct(bigTestfile)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data
        print('Read Bigfile time: {}'.format(time.time() - startTime))

        # Write bigFile Synchronous
        startTime = time.time()
        cmd = projUtils.putBinaryFileReqStruct(bigTestfile)
        for putFilePart in projUtils.generateDataParts(data,
                                                       cmd,
                                                       compress=False):
            response = Web.sendDataMsgFromThread(putFilePart)
            assert response['status'] == 200
        print('Write Bigfile sync time: {}'.format(time.time() - startTime))

        # Write bigFile Asynchronous
        startTime = time.time()
        cmd = projUtils.putBinaryFileReqStruct(bigTestfile)
        callIds = []
        for putFilePart in projUtils.generateDataParts(data,
                                                       cmd,
                                                       compress=False):
            callId = Web.sendDataMsgFromThreadAsync(putFilePart)
            callIds.append(callId)
        for callId in callIds:
            response = Web.getDataMsgResponse(callId)
            assert response['status'] == 200
        print('Write Bigfile async time: {}'.format(time.time() - startTime))

        # Read back written data
        writtenPath = os.path.join(CommonOutputDir, bigTestfile)
        with open(writtenPath, 'rb') as fp:
            writtenData = fp.read()
        assert writtenData == data
Exemplo n.º 2
0
def send_data_response(client, response, compress=False):
    filename = response.get('filename')
    try:
        data = readFileData(filename)
        if len(data) == 0:
            raise RTError('Empty or zero length file')
    except Exception as err:
        errStr = "readFileData Exception: {}: {}: {}".format(
            filename, type(err), err)
        return send_error_response(client, response, errStr)
    for msgPart in generateDataParts(data, response, compress):
        send_response(client, msgPart)
    return
Exemplo n.º 3
0
 def on_message(client, message):
     """
     Main message dispatcher that will get the request from projectServer
     and call the registered handler to process the request. It will then
     return the result of the handler function back to the projectServer.
     """
     response = {'status': 400, 'error': 'unhandled request'}
     cmd = 'unknown'
     try:
         request = json.loads(message)
         request = decodeByteTypeArgs(request)
         # print(f'on_message: message {request} type: {type(request)}')
         # create the response message but without data objects
         response = request.copy()
         response.pop('data', None)
         response.pop('args', None)
         response.pop('kwargs', None)
         trimDictBytes(response)
         cmd = request.get('cmd')
         # decode any encoded byte args
         # TODO - spin off a thread for each request passing in client so the thread call client.send
         callResult = WsRemoteService.remoteHandler.runRemoteCall(request)
         # serialize the callResult
         # print(f'callresult type: {type(callResult)}')
         if isNativeType(callResult):
             if type(callResult) == bytes:
                 data = callResult
                 response['dataSerialization'] = 'bytes'
             else:
                 # encode to json and then as a byte string
                 data = json.dumps(callResult).encode()
                 response['dataSerialization'] = 'json'
         else:
             data = pickle.dumps(callResult)
             response['dataSerialization'] = 'pickle'
         # return callResult
         response['status'] = 200
         compress = False
         if len(data) > 1024 * 1024:
             compress = True
         for msgPart in generateDataParts(data, response,
                                          compress=compress):
             WsRemoteService.send_response(client, msgPart)
     except Exception as err:
         errStr = "RPC Exception: {}: {}".format(cmd, err)
         print(errStr)
         response.update({'status': 400, 'error': errStr})
         WsRemoteService.send_response(client, response)
         if cmd == 'error':
             sys.exit()
         return
Exemplo n.º 4
0
 def putBinaryFile(self, filename, data, compress=False):
     if self.local:
         outputDir = os.path.dirname(filename)
         if not os.path.exists(outputDir):
             os.makedirs(outputDir)
         with open(filename, 'wb+') as binFile:
             binFile.write(data)
     else:
         try:
             fileHash = None
             putFileCmd = projUtils.putBinaryFileReqStruct(filename)
             for putFilePart in projUtils.generateDataParts(data, putFileCmd, compress):
                 fileHash = putFilePart.get('fileHash')
                 projUtils.clientSendCmd(self.commPipes, putFilePart)
         except Exception as err:
             # Send error notice to clear any partially cached data on the server side
             # Add fileHash to message and send status=400 to notify
             if fileHash:
                 putFileCmd['fileHash'] = fileHash
                 putFileCmd['status'] = 400
                 projUtils.clientSendCmd(self.commPipes, putFileCmd)
             raise err
     return
Exemplo n.º 5
0
    def test_getFile(self, dicomTestFilename):
        print("test_getFile")
        global fileData
        assert Web.wsDataConn is not None
        # Try to initialize file watcher with non-allowed directory
        cmd = projUtils.initWatchReqStruct('/', '*', 0)
        response = Web.sendDataMsgFromThread(cmd)
        # we expect an error because '/' directory not allowed
        assert response['status'] == 400

        # Initialize with allowed directory
        cmd = projUtils.initWatchReqStruct(testDir, '*.dcm', 0)
        response = Web.sendDataMsgFromThread(cmd)
        assert response['status'] == 200

        dcmImg = readDicomFromFile(dicomTestFilename)
        anonDcm = anonymizeDicom(dcmImg)
        data = writeDicomToBuffer(anonDcm)
        # with open(dicomTestFilename, 'rb') as fp:
        #     data = fp.read()

        cmd = projUtils.watchFileReqStruct(dicomTestFilename)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        # import pdb; pdb.set_trace()
        assert responseData == data

        # Try compressed version
        cmd = projUtils.watchFileReqStruct(dicomTestFilename, compress=True)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        cmd = projUtils.getFileReqStruct(dicomTestFilename)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        # Try compressed version
        cmd = projUtils.getFileReqStruct(dicomTestFilename, compress=True)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        cmd = projUtils.getNewestFileReqStruct(dicomTestFilename)
        try:
            responseData = handleDataRequest(cmd)
        except Exception as err:
            assert False, str(err)
        assert responseData == data

        # Try to get a non-allowed file
        cmd = projUtils.getFileReqStruct('/tmp/file.nope')
        try:
            responseData = handleDataRequest(cmd)
        except RequestError as err:
            # Expecting a status not 200 error to be raised
            assert 'status' in str(err)
        else:
            self.fail('Expecting RequestError')

        # try from a non-allowed directory
        cmd = projUtils.getFileReqStruct('/nope/file.dcm')
        try:
            responseData = handleDataRequest(cmd)
        except RequestError as err:
            # Expecting a status not 200 error to be raised
            assert 'status' in str(err)
        else:
            self.fail('Expecting RequestError')

        # Test putTextFile
        testText = 'hello2'
        textFileName = os.path.join(tmpDir, 'test2.txt')
        cmd = projUtils.putTextFileReqStruct(textFileName, testText)
        response = Web.sendDataMsgFromThread(cmd)
        assert response['status'] == 200

        # Test putBinaryData function
        testData = b'\xFE\xED\x01\x23'
        dataFileName = os.path.join(tmpDir, 'test2.bin')
        cmd = projUtils.putBinaryFileReqStruct(dataFileName)
        for putFilePart in projUtils.generateDataParts(testData,
                                                       cmd,
                                                       compress=True):
            response = Web.sendDataMsgFromThread(putFilePart)
        assert response['status'] == 200
        # read back an compare to original
        cmd = projUtils.getFileReqStruct(dataFileName)
        response = Web.sendDataMsgFromThread(cmd)
        responseData = b64decode(response['data'])
        assert responseData == testData