Giter VIP home page Giter VIP logo

tej-protoc's Introduction

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()

tej-protoc's People

Contributors

tejmagar avatar

Stargazers

Rajesh avatar Kritish Neupane avatar  avatar Bishwas Bhandari avatar

Watchers

 avatar

Forkers

gnostr-org

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.