Esempio n. 1
0
 def getHandler(self, request, context):
     params = {
         'from_utc': request.getRequest.queryParams.from_utc,
         'to_utc': request.getRequest.queryParams.to_utc,
         'params_json': request.getRequest.queryParams.params_json
     }
     logging.warning('this is a get request with params %s' % str(params))
     if pre_read_check(params):
         for datFrag in try_read(params):
             logging.warning(datFrag)
             yield data_pb2.Response(
                 code=data_pb2.StatusCode.Value('Ok'),
                 msg="get data successfully from the grpc server!",
                 datFragment=data_pb2.DatFragment(data=datFrag))
     else:
         yield data_pb2.Response(code=data_pb2.StatusCode.Value('Failed'),
                                 msg="Something wrong!")
Esempio n. 2
0
 def putHandler(self, request_iterator, context):
     logging.warning('this is a put request')
     if pre_write_check():
         for request in request_iterator:
             assert request.putRequest.metaData.uuid is not None
             payload = {
                 'raw': request.putRequest.datFragment.data.decode(),
                 'uuid': request.putRequest.metaData.uuid
             }
             write(payload)
         logging.warning('put succeeded...')
         return data_pb2.Response(
             code=data_pb2.StatusCode.Value('Ok'),
             msg="put data successfully the grpc server!")
     else:
         logging.warning('put failed...')
         return data_pb2.Response(code=data_pb2.StatusCode.Value('Failed'),
                                  msg="this node is full")
Esempio n. 3
0
    def ping(self, request, context):
        print "Receive ping request"

        fromSender = request.fromSender
        toReceiver = request.toReceiver
        originalSender = request.originalSender

        response = data_pb2.Response()
        response.msg = 'Ping Success'
        #status_code = data_pb2.StatusCode()
        status_code = 1
        response.Code = status_code

        print "Send Ping response"
        return response
Esempio n. 4
0
 def ping(self, request, context):
     logging.warning('this is a ping request')
     return data_pb2.Response(code=data_pb2.StatusCode.Value('Ok'),
                              msg="pinged successfully the grpc server!")
Esempio n. 5
0
    def putHandler(self, request_iterator, context):
        print "Receive put request"
        buffer = []
        timestamp = ''
        threshold = 0
        for request in request_iterator:
            print request
            fromSender = request.fromSender
            toReceiver = request.toReceiver
            originalSender = request.originalSender
            putRequest = request.putRequest
            metaData = putRequest.metaData
            DatFragment = putRequest.datFragment
            if DatFragment.timestamp_utc:
                timestamp = DatFragment.timestamp_utc
            data = DatFragment.data
            #if timestamp != '':
                # No timestamp, mesonet data
            #    data = self.normalize_data_mesonet(DatFragment.data, timestamp)
            #else:
                # Mesowest data
            #    data = self.normalize_data_mesowest(DatFragment.data)
            threshold = threshold + 1
            buffer.append(data)
            if threshold > FORWARDING_THRESHOLD:
                client = Client(host='169.254.230.239', port=8080)
                client.stub.putHandler(request_iterator)
                break
                '''
                file_name = 'forwarding' + '.csv'
                with open(file_name, 'w') as file:
                    file.write(str(buffer))
                    buffer = []
                call(['python grpc_client.py -x '])
                '''
        # Save buffer to my mongo database
        print "Saving buffer to mongodb...."
        print "Timestamp is " + str(timestamp)
        client = MongoClient('localhost', 27017)
        db = client.pymongo_test
        #if timestamp:
        #    insert_bulk_mongo_mesonet(db, buffer, timestamp)
        #else:
        insert_bulk_mongo(db, buffer)
        print "Finish saving buffer to mongodb"

        # Save buffer to File
        #timestamp_utc = DatFragment.timestamp_utc
        file_name = 'timestamp_utc' + '.txt'
        with open(file_name, 'w') as file:
            file.write(str(buffer))

        # Call comand to send file to clsuter
        # call Java client
        call(['../ProjectCluster/client.sh', ' 1 -write -' + file_name])
        # call Python client

        #os.system('sh ../ProjectCluster/client.sh 1 -write -' + file_name)

        # Reponse to server
        response = data_pb2.Response()
        status_code = 1
        response.Code = status_code
        response.msg = 'Put Request success'
        datFragment = data_pb2.DatFragment()
        datFragment.data = "Success".encode()
        response.datFragment.data = "Success".encode()
        #dataFragment.data = "Success".encode()
        #response.datFragment=DatFragment(data="Success".encode())
        line = "Success"
        #response=Response(
        #    Code=1,
        ##    datFragment=DatFragment(timestamp_utc="0/0/0", data=line)
        #)
        print response
        return response