Git Product home page Git Product logo

tcp-mathematica's Introduction

tcp-mathematica

Implementation of the simple protocol to transfer arbitrary data over TCP between two kernels.

  • No handshaking
  • First 4 bytes - the length of the message (unsigned int)
  • The rest is payload
  • No additional separators
  • Fully asynchronous
  • It works effectively with byte arrays
  • TCP packet size doesn't matter

Installation

It is performed simply via Import

Import["https://raw.githubusercontent.com/JerryI/tcp-mathematica/main/JTP/JTP.wl"];

The examples are stored in test.nb.

Server

It is always listening for data from multiple clients. Firstly, you need to create server by

server = JTPServer[] // JTPServerStart

The host address and the port can be specified by "host" and "port" options. By the default, it starts to listerning. It evaluates every received message (not TCP packet) and replies.

One can access to the logs using

server["log"]["Elements"] // TableForm // Dynamic

Client

You can use it in the same manner

client = JTPClient["host"->"", "port"->] // JTPClientStart

There are a few different modes how to work with server:

1. Evaluate and fetch the result

The simples model is like Evaluate in the notebook interface

JTPClientEvaluate[client, expression]

The second argument has Hold attribute on èxpression. After evaluating on the server it will return the result.

2. Asynchronous evaluation

The same as the previous, but doen't stop the program and accepts callback[uuid, result] function to handle the answer from the server.

JTPClientEvaluateAsync[client, expression, Promise -> callback]

If you don't care about the result - use

JTPClientSend[cli, socket]	

3. Listening mode

To start contineous listerning mode on the client use

JTPClientStartListening[cli, Promise -> callback]

If the callback option is not specified, then only the evaluation of the incomming answers will be performed. Note that in this mode the methods (1), (2) are disabled.

Misc

Other notes on everything...

Local variables

The evaluation of the incomming messages on the client and server either performes inside Block[] with defined variable socket = uuid, where uuid is id of the socket, which sent the message. It can be obtained by

JTPClientEvaluate[cli, socket] == "0e5eac ... "

And to reply to yourself using low-level functions on the server's side

JTPClientStartListening[cli, Promise -> Function[{x, y}, CreateWindow[DialogNotebook[y]]]]
JTPClientSend[cli, reply[socket, "ahaha"]; "ohoho"];

This part is still in development...

Send messages to the particular client from server (async)

For this reason you need to obtain uuid of the socket first

JTPSend[uuid, expr]

Multiple kernels

This part is still in development...

tcp-mathematica's People

Contributors

jerryi avatar kirillbelovtest avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

kirillbelovtest

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.