Skip to main content

An SDK for the TracimDaemon project

Project description

TracimDaemonSDK

An SDK for the TracimDaemon project

Usage

Import the package

package main

import "github.com/Millefeuille42/TracimDaemonSDK"

Create a new TracimDaemon client

package main

import "github.com/Millefeuille42/TracimDaemonSDK"

func main() {
	client := TracimDaemonSDK.NewClient(TracimDaemonSDK.Config{
		MasterSocketPath: "path/to/daemon/socket",
		ClientSocketPath: "path/to/client/socket",
	})
	defer client.Close()
}

Create and listen to the client socket

package main

import (
	"github.com/Millefeuille42/TracimDaemonSDK"
	"log"
	"os"
)

func main() {
	client := TracimDaemonSDK.NewClient(TracimDaemonSDK.Config{
		MasterSocketPath: "path/to/daemon/socket",
		ClientSocketPath: "path/to/client/socket",
	})
	defer client.Close()

	err := client.CreateClientSocket()
	if err != nil {
		log.Fatal(err)
		return
	}
	defer client.ClientSocket.Close()
}

Set up various handlers

package main

import (
	"github.com/Millefeuille42/TracimDaemonSDK"
	"log"
	"os"
)

func genericHandler(c *TracimDaemonSDK.TracimDaemonClient, e *TracimDaemonSDK.DaemonEvent) {
	log.Printf("%s RECV: %s\n", c.Config.ClientSocketPath, e.Type)
}

func main() {
	client := TracimDaemonSDK.NewClient(TracimDaemonSDK.Config{
		MasterSocketPath: "path/to/daemon/socket",
		ClientSocketPath: "path/to/client/socket",
	})
	defer client.Close()

	err := client.CreateClientSocket()
	if err != nil {
		log.Fatal(err)
		return
	}
	defer client.ClientSocket.Close()

	client.RegisterHandler(TracimDaemonSDK.EventTypeGeneric, genericHandler)
}

With genericHandler being a function with the following signature:

func genericHandler(c *TracimDaemonSDK.TracimDaemonClient, e *TracimDaemonSDK.Event) {
    log.Printf("%s RECV: %s\n", c.Config.ClientSocketPath, e.DataParsed.EventType)
}

Register the client to the daemon and start listening to events

package main

import (
	"github.com/Millefeuille42/TracimDaemonSDK"
	"log"
	"os"
)

func genericHandler(c *TracimDaemonSDK.TracimDaemonClient, e *TracimDaemonSDK.DaemonEvent) {
	log.Printf("%s RECV: %s\n", c.Config.ClientSocketPath, e.Type)
}

func main() {
	client := TracimDaemonSDK.NewClient(TracimDaemonSDK.Config{
		MasterSocketPath: "path/to/daemon/socket",
		ClientSocketPath: "path/to/client/socket",
	})
	defer client.Close()

	err := client.CreateClientSocket()
	if err != nil {
		log.Fatal(err)
		return
	}
	defer client.ClientSocket.Close()

	client.RegisterHandler(TracimDaemonSDK.EventTypeGeneric, genericHandler)

	err = client.RegisterToMaster()
	if err != nil {
		log.Fatal(err)
		return
	}

	client.ListenToEvents()
}

The "minimal" client code is as follows:

package main

import (
	"github.com/Millefeuille42/TracimDaemonSDK"
	"log"
	"os"
)

func genericHandler(c *TracimDaemonSDK.TracimDaemonClient, e *TracimDaemonSDK.DaemonEvent) {
	log.Printf("%s RECV: %s\n", c.Config.ClientSocketPath, e.Type)
}

func main() {
	client := TracimDaemonSDK.NewClient(TracimDaemonSDK.Config{
		MasterSocketPath: os.Getenv("TRACIM_MINICLIENT_MASTER_SOCKET_PATH"),
		ClientSocketPath: os.Getenv("TRACIM_MINICLIENT_CLIENT_SOCKET_PATH"),
	})
	defer client.Close()

	err := client.CreateClientSocket()
	if err != nil {
		log.Fatal(err)
		return
	}
	defer client.ClientSocket.Close()

	client.RegisterHandler(TracimDaemonSDK.EventTypeGeneric, genericHandler)
	err = client.RegisterToMaster()
	if err != nil {
		log.Fatal(err)
		return
	}

	client.ListenToEvents()
}

A custom logger can also be set in the Logger field of the TracimDaemonClient

Definitions

TLMEvent

TLMEvent is the struct that represents the data sent by Tracim (see tracim TLM documentation)

type TLMEvent struct {
	EventId   int         `json:"event_id"`
	EventType string      `json:"event_type"`
	Read      interface{} `json:"read"`
	Created   time.Time   `json:"created"`
	Fields    interface{} `json:"fields"`
}

DaemonEvent

DaemonEvent is the event format used to communicate between apps

type DaemonEvent struct {
	Path   string `json:"path"`
	Type string `json:"type"`
	Data interface{} `json:"data,omitempty"`
}
  • The Data field can contain additional information of any format
  • The Path field is the path to the client socket (as defined in the config)
  • The Type field is any of the Daemon* constants defined in daemonEvents.go

A Type is expected to contain additional data if there is a <eventType>Data struct defined in daemonEvents.go.

EventHandler

EventHandler is the function definition for the event handlers. It takes a TracimDaemonClient and a DaemonEvent as parameters

type EventHandler func(*TracimDaemonClient, *DaemonEvent)

By default, handlers for DaemonAccountInfo and DaemonPing are already defined, it is possible to override them.

Event types

Event types are defined by tracim. It is also possible to set handlers for every DaemonEvent type. There also is events defined by the SDK, for convenience.

// EventTypeGeneric is the event type for generic events (every DaemonEvent)
EventTypeGeneric = "custom_message"
// EventTypeError is the event type for errors
EventTypeError = "custom_error"

Protocol (for developers of another language)

Communication

Client and daemons communicate with the DaemonEvent format, i.e. JSON data following this format:

{
    "type": "event_type",
    "path": "/path/to/client/socket",
    "data": {}
}

(See above DaemonEvent section for details about each field)

Registering / unregistering a client

When registering / unregistering a client a DaemonEvent must be sent on the daemon socket.

{
    "type": "client_add",
    "path": "/path/to/client/socket",
    "data": {
      "path": "/path/to/client/socket",
      "pid": 999
    }
}

To register a client, the client must send the message to the daemon socket, with the type field set to client_add. To unregister a client, the client must send the message to the daemon socket, with the type field set to client_delete.

In both, additional info, defined as follows, is required:

{
  "path": "/path/to/client/socket",
  "pid": 999
}

With pid being the PID of the client process.

Receiving events

Once registered, the client will receive DaemonEvents from the daemon.

(See above DaemonEvent section for details about types and data)

Ack and Keep-Alive

Ack

The daemon will send a DaemonAck upon receiving any managed events not expecting a response, otherwise the expected response is sent. As for now, a DaemonPong for a DaemonPing and a DaemonAccountInfo for a DaemonClientAdd

The daemon expects no DaemonAck on its messages.

Keep-Alive

The daemon will periodically (once every minute) send a DaemonPing event, clients have a minute to respond with DaemonPong, If not, it will unregister un-responding clients at the next ping.

It is possible to test the daemon responsiveness by sending it DaemonPing events. It will respond with a DaemonPong as soon as possible.

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

tracim_daemon_sdk-1.0.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tracim_daemon_sdk-1.0.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file tracim_daemon_sdk-1.0.0.tar.gz.

File metadata

  • Download URL: tracim_daemon_sdk-1.0.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for tracim_daemon_sdk-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bf9bae660927933edac7f231e122753e3fccfdf3575721855fdb6ee7b8f20e27
MD5 89656591880b84e1fcc7b5ea7ad1114c
BLAKE2b-256 848a8b7255311fff3bae477c128e4c1b674e8ac98d3469939f16637ce8eca3e0

See more details on using hashes here.

File details

Details for the file tracim_daemon_sdk-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tracim_daemon_sdk-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19f51f491bed12447c892522cd563f5420afd765587d2dd8757aa379655344c8
MD5 e37aea5d21ce118a86d24f4720b35961
BLAKE2b-256 6305d108c52fb345b1869490dedab5b6415a0edf67d94a85a52e95a6d2661c1a

See more details on using hashes here.

Supported by

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