Пример #1
0
def _reply(cmd, socket, client_address=None):
    """Build a reply data and send it to client"""
    #ts = []; ts.append(timer())
    try:
        r = _replyData(cmd)
        if len(r) == 0:
            return 0
    except Exception as e:
        r = f'ERR.LS. Exception for cmd {cmd}: {e}'
        exc = traceback.format_exc()
        print('LS.Traceback: ' + repr(exc))
    #print(croppedText(f'reply_object={r}',100000))
    #ts.append(timer()); ts[-2] = round(ts[-1] - ts[-2],4)
    try:
        reply = ubjson.dumpb(r, no_float32=False)  # 75% time is spent here
    except Exception as e:
        reply = ubjson.dumpb(f'ERR.LS. Exception in dumpb: {e}')
    #ts.append(timer()); ts[-2] = round(ts[-1] - ts[-2],4)
    #printd(f'reply {len(reply)} bytes, doubles={no_float32}')
    host, port = client_address  # the port here is temporary
    #printd(croppedText(f'sending back {len(reply)} bytes to {client_address}'))
    #ts.append(timer()); ts[-2] = round(ts[-1] - ts[-2],4)
    if UDP:
        _send_UDP(reply, socket, client_address)  # 25% time spent here
        # initiate the sending of EOD to that client
    else:
        #self.request.sendall(reply)
        printi('TCP not supported yet')
    #ts.append(timer()); ts[-2] = round(ts[-1] - ts[-2],4)
    #print(f'reply times: {ts[:-1]}')
    return len(reply)
 def write_raw(self,data):
     """
     """
     ubdata = ubjson.dumpb(data)
     ubdata = ubdata + b'\n'
     print('Writing header')
     self.f.write(ubdata)
Пример #3
0
    def send_request(self, request):
        """
    Sends the request to the peer.

    :param request: The request as dictionary.
    :return: The response as dictionary
    """
        request = {
          "action": "req",
          "payload": ESCSEQ_OBJ_START + \
            ubjson.dumpb(request).replace(b'\x00', b'\x00\x00') + \
            ESCSEQ_OBJ_END,
          "cv": threading.Condition()
        }
        with request['cv']:
            self._msgqueue.put(request)
            if not request['cv'].wait(timeout=CALL_TIMEOUT):
                raise TimeoutError("No response within %d seconds" %
                                   CALL_TIMEOUT)
        if 'error' in request:
            raise request['error']
        try:
            return ubjson.loadb(request['response'])
        except ubjson.DecoderException:
            errmsg = "%s: Couldn't parse UBJSON from lora_controller. Got: %s" % \
              (self._name, " ".join(hex(x) for x in request['response']))
            logger.error(errmsg, exc_info=True)
            raise RuntimeError(errmsg)
Пример #4
0
    def _call(self, method, args={}):
        msg = ubjson.dumpb({method: self._flatten_arrays(args)})
        msglen = struct.pack("=i", len(msg))
        try:
            self.callpipe.write(msglen + msg)
            if method.startswith("Set"):
                return None
            self.callpipe.flush()
        except OSError as exc:
            raise AMSWorkerError('Error while sending a message') from exc
        if method == "Exit":
            return None

        results = []
        while True:
            try:
                msgbuf = self._read_exactly(self.replypipe, 4)
                msglen = struct.unpack("=i", msgbuf)[0]
                msgbuf = self._read_exactly(self.replypipe, msglen)
            except EOFError as exc:
                raise AMSWorkerError("Error while trying to read a reply") from exc

            try:
                msg = ubjson.loadb(msgbuf)
            except Exception as exc:
                raise AMSWorkerError("Error while decoding a reply") from exc

            if "return" in msg:
                ret = msg["return"]
                if ret["status"] == 0:
                    return results
                else:
                    raise AMSPipeError.from_message(ret)
            else:
                results.append(msg)
Пример #5
0
    def export(self, g3dModel, filepath):
        baseModel = self.mountJsonOutput(g3dModel)

        output_file = open(filepath, 'wb')

        output_file.write(ubjson.dumpb(baseModel))
        output_file.close()
Пример #6
0
def encrypt5(in_stream, out_stream, public_key, secret_key, verify_all,
             symmetric, force, block_size):
    version = 5
    sym_key = nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE)
    if None == secret_key:
        sign_key = nacl.public.PrivateKey.generate()
    else:
        sign_key = nacl.public.PrivateKey(secret_key)

    print(sym_key)
    secure_metadata = {
        "key": sym_key,
        "block_size": block_size,
    }
    secure_metadata_bytes = ubjson.dumpb(secure_metadata)
    public_key = nacl.public.PublicKey(public_key)
    smdbox = nacl.public.Box(sign_key, public_key)
    encrypted_metadata = bytes(smdbox.encrypt(secure_metadata_bytes))
    metadata = {
        "sign_key": sign_key.public_key.encode(),
        "secure": encrypted_metadata,
    }
    encoded_metadata = ubjson.dumpb(metadata)
    metadata_length = len(encoded_metadata)
    first_bytes = struct.pack(FIRST_BYTES_FORMAT, version, metadata_length)

    out_stream.write(MAGIC_BYTES)
    out_stream.write(first_bytes)
    out_stream.write(encoded_metadata)
    # Using predictable nonces (but never twice for the same key)
    # there is a 3% size overhead due to the 16 byte signatures,
    # instead of a 7.8% size overhead due to a 40 byte signature+nonce
    counter = 0
    encrypter_box = nacl.secret.SecretBox(sym_key)
    block = in_stream.read(block_size)
    while len(block) > 0:
        # Use network endianness
        counternonce = counter.to_bytes(nacl.secret.SecretBox.NONCE_SIZE,
                                        "big")
        out_block = encrypter_box.encrypt(block,
                                          nonce=counternonce)._ciphertext
        # We are counting on this for the decryption
        assert len(out_block) == SECRET_BOX_SIGN_SIZE + len(block)
        out_stream.write(out_block)
        block = in_stream.read(block_size)
        counter += 1
    return
Пример #7
0
 def serialize(self, obj):
     """
     Implements :func:`autobahn.wamp.interfaces.IObjectSerializer.serialize`
     """
     data = ubjson.dumpb(obj)
     if self._batched:
         return struct.pack("!L", len(data)) + data
     else:
         return data
Пример #8
0
 def serialize(self, obj):
     """
     Implements :func:`autobahn.wamp.interfaces.IObjectSerializer.serialize`
     """
     data = ubjson.dumpb(obj)
     if self._batched:
         return struct.pack("!L", len(data)) + data
     else:
         return data
Пример #9
0
 def __calc_stashdump(self):
     with self.__stash_lock:
         stashdump = ubjson.dumpb(self.__stash)
         m = md5()
         m.update(stashdump)
         stashhash = m.hexdigest()
         if self.__stash_hash != stashhash:
             self.__stash_hash = stashhash
             return stashdump
         return None
Пример #10
0
 def __new_handshake(self, cursor=0, token=NULL_TOKEN):
     """ Returns a new binary handshake message """
     handshake = bytearray()
     handshake_contents = ubjson.dumpb({
         'type': CommType.HANDSHAKE.value,
         'payload': {
             'cursor': cursor,
             'clientToken': token,
             'isRealtime': self.realtime,
         }
     })
     handshake += pack(">L", len(handshake_contents))
     handshake += handshake_contents
     return handshake
    def write(self,data):
        """
        Writes data to file and to the loggerstreamdataobjects
        """

        if(self.mode == 'wb'):
            ubdata = ubjson.dumpb(data)
            ubdata = ubdata + b'\n'
            print('Writing',data)
            self.f.write(ubdata)
        if(self.fill_loggerstreamdata):
            log_stream_number = data[0]            
            loggerstream_ind = self._loggerstream_num2ind[log_stream_number]
            self.loggerstreams[loggerstream_ind].add_data(data[1])
Пример #12
0
def put_timestamp(hexhash: str, comment: str='', stream='timestamp') -> Optional[TxId]:

    client = get_active_rpc_client()

    if comment:
        data = dict(comment=comment)
        serialized = ubjson.dumpb(data)
        data_hex = hexlify(serialized).decode('utf-8')
        response = client.publish(stream, hexhash, data_hex)
    else:
        response = client.publish(stream, hexhash)

    if response['error'] is not None:
        raise RpcResponseError(response['error']['message'])

    return TxId(response['result'])
Пример #13
0
def put_timestamp(hexhash: str,
                  comment: str = '',
                  stream=app.STREAM_TIMESTAMP) -> Optional[TxId]:

    client = get_active_rpc_client()

    try:
        if comment:
            data = dict(comment=comment)
            serialized = ubjson.dumpb(data)
            data_hex = hexlify(serialized).decode('utf-8')
            response = client.publish(stream, hexhash, data_hex)
        else:
            response = client.publish(stream, hexhash, "")
        return TxId(response)

    except Exception as e:
        log.debug(e)
        raise RpcResponseError(str(e))
Пример #14
0
def _send_dictio(dictio, sock, hostPort: tuple):
    """low level send"""
    global LastDictio
    LastDictio = dictio.copy()
    # _printd('executing: '+str(dictio))
    dictio['username'] = Username
    dictio['program'] = Program
    dictio['pid'] = PID
    # _printd(f'send_dictio to {hostPort}: {dictio}')
    encoded = ubjson.dumpb(dictio)
    if UDP:
        sock.sendto(encoded, hostPort)
    else:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(hostPort)
        except Exception as e:
            _printe('in sock.connect:' + str(e))
            sys.exit()
        sock.sendall(encoded)
Пример #15
0
import ubjson

data = {
    "name": "python-ubjson",
    "versions": ["1", "2"],
    "group": {
        "is_a_package": True,
        "value": 42
    }
}
serialized = ubjson.dumpb(data)
print(serialized)
with open("/tmp/data.json", "wb") as f:
    f.write(serialized)
Пример #16
0
import traceback

lck = threading.Lock()


def recv_fun(ser):
    while True:
        res = ser.read(32)
        lck.acquire()
        if (res != b''):
            print("".join(chr(x) for x in res), end='', flush=True)
        lck.release()


with serial.Serial(sys.argv[1], 115200, timeout=0.1) as ser:
    recvthread = threading.Thread(target=recv_fun, args=[ser])
    recvthread.start()
    print("Ready, press return to enter command.")
    while True:
        input()
        lck.acquire()
        jsoncmd = input("Enter JSON command: ")
        try:
            out_ubjson = ubjson.dumpb(json.loads(jsoncmd))
            ser.write(b'\x00\x01')
            ser.write(out_ubjson)
            ser.write(b'\x00\x02')
        except:
            traceback.print_exc()
        lck.release()
Пример #17
0
# Sends a get_lora_channel command and tries to parse the response.

import socket
import time
import ubjson
import sys

# The command that should be sent
command = {"get_lora_channel":{ }}

# Replace with IP and port shown in the RIOT console
target_if = socket.getaddrinfo('fd00::1337:0001' if len(sys.argv)<2 else sys.argv[1], 9000, socket.AF_INET6, socket.SOCK_STREAM)
(family, socktype, proto, canonname, sockaddr) = target_if[0]

# Wrap the command in OBJ_BEGIN and OBJ_END sequences
bincmd = b'\x00\x01' + ubjson.dumpb(command) + b'\x00\x02'

# Open socket and send command
s = socket.socket(family, socktype, proto)
s.connect(sockaddr)
s.send(bincmd)

# Collect the response. Will loop until a OBJ_END sequence arrives
data = b''
try:
	prefix = s.recv(2)
	if prefix == b'\x00\x01':
		while data[-2:] != b'\x00\x02':
			data += s.recv(1)
      # Unescape bianry zeroes
			if data[-2:]==b'\x00\x00':
Пример #18
0
 def ask_retransmit(offsetSize):
     global retransmitInProgress
     retransmitInProgress = tuple(offsetSize)
     cmd = {'cmd': ('retransmit', offsetSize)}
     _printi(f'Asking to retransmit port {port}: {cmd}')
     sock.sendto(ubjson.dumpb(cmd), addr)
Пример #19
0
    bson_files_sizes += [bson_file_size]

    with open(bin_file_name.format(i), 'rb') as bin_file:
        bs = bin_file.read()
        start = time.time()
        bson.loads(bs)
        end = time.time()
        bson_unpack_time += [end - start]

    bson_compression += [bson_file_size / json_file_size]

    # Testing ubjson library.
    bin_file_name = 'test-{}.ubjson.bin'
    with open(bin_file_name.format(i), 'wb') as bin_file:
        start = time.time()
        ubs = ubjson.dumpb(json_data)
        end = time.time()
        ubjson_pack_time += [end - start]
        bin_file.write(ubs)

    ubjson_file_size = os.stat(bin_file_name.format(i)).st_size
    ubjson_files_sizes += [ubjson_file_size]

    with open(bin_file_name.format(i), 'rb') as bin_file:
        ubs = bin_file.read()
        start = time.time()
        ubjson.loadb(ubs)
        end = time.time()
        ubjson_unpack_time += [end - start]

    ubjson_compression += [ubjson_file_size / json_file_size]