Skip to main content

A TEJ Protocol implementation

Project description

tej-protoc v1

tej-protoc v1 is a implementation of custom TEJ protocol. It is a full duplex protocol. This protocol can be used for sending large files and messages faster.

Installation

pip install tej-protoc

Diagram

+-------------------------------------------------------------------+
|                        TEJ Protocol v1                            |
+-------------------------------------------------------------------+
|         Status Byte            |                                  |
|--------------------------------|           Protocol Version       |
|   Status bit  |  Custom status |              8 bits              |
|    (1 bit)    |  (7 bits)      |                                  |
+-------------------------------------------------------------------+
|                         Number of files                           |
|                           (64 bits)                               |
+-------------------------------------------------------------------+
|        'n' File name length    |        'n' Actual file name      |
|         (16 bits)              |     (from 'n' filename length)   |
+-------------------------------------------------------------------+
|        'n' file length         |      'n' Actual file data        |
|        (64 bits)               |      (from 'n' file length)      |
+-------------------------------------------------------------------+
|                  Repeat for 'n' number of files                   |
+-------------------------------------------------------------------+
|         Message length         |           Message data           |
|           (64 bits)            |       (from message length)      |
+-------------------------------------------------------------------+

Status byte

The first bit in the status byte must be 1 and then remaining bits are the custom status bits. Custom status bit ranges from 0 to 127

Example usage

Server

Here is a simple server implementation with tej-protoc. Create a new file named server.py

from tej_protoc.server import TPServer
from tej_protoc import protocol
from tej_protoc import callbacks


class MessageCallback(callbacks.ResponseCallback):
    def connected(self, client):
        builder = protocol.BytesBuilder()
        builder.set_message(b'Hello')
        protocol.send(client, builder.bytes())

    def received(self, files, message):
        print('---- Received in server ----')
        for file in files:
            print(file.name)
        print('Message: ', message.decode())
        print('---------------------------------')


server = TPServer('localhost', 8000, MessageCallback)
server.listen()

You can also access the client object inside callback methods with self.client. Your code inside the Callback class is executed everytime when the client connects and the data is received. Once the client is connected connected method is called. If you want to send data when client connects, then you can send with protocol.send(client, builder.build()).

To send data from client, you need to build compatible bytes array with BytesBuilder class.

Client

from tej_protoc.client import TPClient
from tej_protoc import protocol
from tej_protoc import callbacks


class ClientCallback(callbacks.ResponseCallback):
    def connected(self, client):
        builder = protocol.BytesBuilder()
        builder.set_message(b'Sending from client')
        # To upload file
        # builder.add_file('file.txt', open('file.txt', 'rb').read())
        protocol.send(client, builder.bytes())

    def received(self, files, message):
        for file in files:
            print(f'Filename: {file.name}')
            # Other attributes: file.size, file.data

        print(f'Message: {message.decode()}')


def test_client():
    try:
        client = TPClient('localhost', 8000, ClientCallback)
        client.listen()
    except Exception as e:
        print('error', e)


test_client()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tej-protoc-0.2.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

tej_protoc-0.2.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file tej-protoc-0.2.1.tar.gz.

File metadata

  • Download URL: tej-protoc-0.2.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for tej-protoc-0.2.1.tar.gz
Algorithm Hash digest
SHA256 256294661de7cf53b652d8beb60beda3c461aecc66fed39fe97e283e10f623f8
MD5 18d1d5007b77c064c270aa95b2ec08a8
BLAKE2b-256 eb5f3f2d40c3803a3bd6400e71c55c8358196d6eb7b31621af68831b1dd6c6cd

See more details on using hashes here.

File details

Details for the file tej_protoc-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: tej_protoc-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for tej_protoc-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d658ae001640cc030abfb0b63745e4d4459fc971d2efa28d33e82139ce04d5a
MD5 23330f0ffb2a0291967a288e875a26b5
BLAKE2b-256 68c24119643b39b2e0e01ed78af7f46cb1b792bbef8027986a5c0776ded37de5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page