agent

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2022 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package agent describes an interface for modules to receive and send bundles.

The main interface is the ApplicationAgent, which only requires two channels for incoming and outgoing Messages. Additionally, it requests a list of endpoints. Due to this flexibility, an ApplicationAgent can be implemented in various forms, e.g., as an external interface for third-party programs or as an internal module. Both possibilities are already included in this package, for example the WebSocketAgent or the PingAgent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppAgentContainsEndpoint

func AppAgentContainsEndpoint(app ApplicationAgent, eids []bpv7.EndpointID) bool

AppAgentContainsEndpoint checks if an ApplicationAgent listens to at least one of the requested endpoints.

func AppAgentHasEndpoint

func AppAgentHasEndpoint(app ApplicationAgent, eid bpv7.EndpointID) bool

AppAgentHasEndpoint checks if an ApplicationAgent listens to this endpoint.

Types

type ApplicationAgent

type ApplicationAgent interface {
	// Endpoints returns the EndpointIDs that this ApplicationAgent answers to.
	Endpoints() []bpv7.EndpointID

	// MessageReceiver is a channel on which the ApplicationAgent must listen for incoming Messages.
	MessageReceiver() chan Message

	// MessageSender is a channel to which the ApplicationAgent can send outgoing Messages.
	MessageSender() chan Message
}

ApplicationAgent is an interface to describe application agents, which can both receive and transmit Bundles. Each implementation must provide the following methods to communicate its addresses. Furthermore two channels must be available, one for receiving and one for sending Messages.

On closing down, an ApplicationAgent MUST close its MessageSender channel and MUST leave the MessageReceiver open. The supervising code MUST close the MessageReceiver of its subjects.

type BundleMessage

type BundleMessage struct {
	Bundle bpv7.Bundle
}

BundleMessage indicates a transmitted Bundle. If the Message is received from an ApplicationAgent, it is an incoming Bundle. If the Message is sent from an ApplicationAgent, it is an outgoing Bundle.

func (BundleMessage) Recipients

func (bm BundleMessage) Recipients() []bpv7.EndpointID

Recipients are the Bundle destination for a BundleMessage.

type Message

type Message interface {
	// Recipients returns a list of endpoints to which this message is addressed.
	// However, if this message is not addressed to some specific endpoint, nil must be returned.
	Recipients() []bpv7.EndpointID
}

Message is a generic interface to specify an information exchange between an ApplicationAgent and some Manager. The following types named *Message are implementations of this interface.

type MuxAgent

type MuxAgent struct {
	sync.Mutex
	// contains filtered or unexported fields
}

MuxAgent mimics an ApplicationAgent to be used as a multiplexer for different ApplicationAgents.

func NewMuxAgent

func NewMuxAgent() (mux *MuxAgent)

NewMuxAgent creates a new MuxAgent used to multiplex different ApplicationAgents.

func (*MuxAgent) Endpoints

func (mux *MuxAgent) Endpoints() (endpoints []bpv7.EndpointID)

func (*MuxAgent) MessageReceiver

func (mux *MuxAgent) MessageReceiver() chan Message

func (*MuxAgent) MessageSender

func (mux *MuxAgent) MessageSender() chan Message

func (*MuxAgent) Register

func (mux *MuxAgent) Register(agent ApplicationAgent)

Register a new ApplicationAgent for this multiplexer. If this ApplicationAgent closes its channel or broadcasts a ShutdownMessage, it will be unregistered.

type PingAgent

type PingAgent struct {
	// contains filtered or unexported fields
}

PingAgent is a simple ApplicationAgent to "pong" / acknowledge incoming Bundles.

func NewPing

func NewPing(endpoint bpv7.EndpointID) *PingAgent

NewPing creates a new PingAgent ApplicationAgent.

func (*PingAgent) Endpoints

func (p *PingAgent) Endpoints() []bpv7.EndpointID

func (*PingAgent) MessageReceiver

func (p *PingAgent) MessageReceiver() chan Message

func (*PingAgent) MessageSender

func (p *PingAgent) MessageSender() chan Message

type RestAgent

type RestAgent struct {
	// contains filtered or unexported fields
}

RestAgent is a RESTful Application Agent for simple bundle dispatching.

A client must register itself for some endpoint ID at first. After that, bundles sent to this endpoint can be retrieved or new bundles can be sent. For sending, bundles can be created by calling the BundleBuilder. Finally, a client should unregister itself.

This is all done by HTTP POSTing JSON objects. Their structure is described in `rest_agent_messages.go` by the types with the `Rest` prefix in their names.

A possible conversation follows as an example.

// 1. Registration of our client, POST to /register
// -> {"endpoint_id":"dtn://foo/bar"}
// <- {"error":"","uuid":"75be76e2-23fc-da0e-eeb8-4773f84a9d2f"}

// 2. Fetching bundles for our client, POST to /fetch
//    There will be to answers, one with new bundles and one without
// -> {"uuid":"75be76e2-23fc-da0e-eeb8-4773f84a9d2f"}
// <- {"error":"","bundles":[
//      {
//        "primaryBlock": {
//          "bundleControlFlags":null,
//          "destination":"dtn://foo/bar",
//          "source":"dtn://sender/",
//          "reportTo":"dtn://sender/",
//          "creationTimestamp":{"date":"2020-04-14 14:32:06","sequenceNo":0},
//          "lifetime":86400000000
//        },
//        "canonicalBlocks": [
//          {"blockNumber":1,"blockTypeCode":1,"blockControlFlags":null,"data":"S2hlbGxvIHdvcmxk"}
//        ]
//      }
//    ]}
// <- {"error":"","bundles":[]}

// 3. Create and dispatch a new bundle, POST to /build
// -> {
//      "uuid": "75be76e2-23fc-da0e-eeb8-4773f84a9d2f",
//      "arguments": {
//        "destination": "dtn://dst/",
//        "source": "dtn://foo/bar",
//        "creation_timestamp_now": 1,
//        "lifetime": "24h",
//        "payload_block": "hello world"
//      }
//    }
// <- {"error":""}

// 4. Unregister the client, POST to /unregister
// -> {"uuid":"75be76e2-23fc-da0e-eeb8-4773f84a9d2f"}
// <- {"error":""}

func NewRestAgent

func NewRestAgent(router *mux.Router) (ra *RestAgent)

NewRestAgent creates a new RESTful Application Agent.

func (*RestAgent) Endpoints

func (ra *RestAgent) Endpoints() (eids []bpv7.EndpointID)

func (*RestAgent) MessageReceiver

func (ra *RestAgent) MessageReceiver() chan Message

func (*RestAgent) MessageSender

func (ra *RestAgent) MessageSender() chan Message

type RestBuildRequest

type RestBuildRequest struct {
	UUID string                 `json:"uuid"`
	Args map[string]interface{} `json:"arguments"`
}

RestBuildRequest describes a JSON to be POSTed to /build.

type RestBuildResponse

type RestBuildResponse struct {
	Error string `json:"error"`
}

RestBuildResponse describes a JSON response for /build.

type RestFetchRequest

type RestFetchRequest struct {
	UUID string `json:"uuid"`
}

RestFetchRequest describes a JSON to be POSTed to /fetch.

type RestFetchResponse

type RestFetchResponse struct {
	Error   string        `json:"error"`
	Bundles []bpv7.Bundle `json:"bundles"`
}

RestFetchResponse describes a JSON response for /fetch.

type RestRegisterRequest

type RestRegisterRequest struct {
	EndpointId string `json:"endpoint_id"`
}

RestRegisterRequest describes a JSON to be POSTed to /register.

type RestRegisterResponse

type RestRegisterResponse struct {
	Error string `json:"error"`
	UUID  string `json:"uuid"`
}

RestRegisterResponse describes a JSON response for /register.

type RestUnregisterRequest

type RestUnregisterRequest struct {
	UUID string `json:"uuid"`
}

RestUnregisterRequest describes a JSON to be POSTed to /unregister.

type RestUnregisterResponse

type RestUnregisterResponse struct {
	Error string `json:"error"`
}

RestUnregisterResponse describes a JSON response for /unregister.

type ShutdownMessage

type ShutdownMessage struct{}

ShutdownMessage indicates the closing down of an ApplicationAgent. If the Message is received from an ApplicationAgent, it must close itself down. If the Message is sent from an ApplicationAgent, it is closing down itself.

func (ShutdownMessage) Recipients

func (sm ShutdownMessage) Recipients() []bpv7.EndpointID

Recipients are not available for a ShutdownMessage.

type SyscallRequestMessage

type SyscallRequestMessage struct {
	Sender  bpv7.EndpointID
	Request string
}

SyscallRequestMessage is sent from an ApplicationAgent to request some "syscall" specific information.

func (SyscallRequestMessage) Recipients

func (srm SyscallRequestMessage) Recipients() []bpv7.EndpointID

Recipients are not available for a SyscallRequestMessage.

type SyscallResponseMessage

type SyscallResponseMessage struct {
	Request   string
	Response  []byte
	Recipient bpv7.EndpointID
}

SyscallResponseMessage is the answer to a SyscallRequestMessage, sent to an ApplicationAgent. The Response is stored as a generic byte array. However, its content is defined for each syscall.

func (SyscallResponseMessage) Recipients

func (srm SyscallResponseMessage) Recipients() []bpv7.EndpointID

Recipients are the sender of the SyscallRequestMessage.

type WebSocketAgent

type WebSocketAgent struct {
	// contains filtered or unexported fields
}

WebSocketAgent is a WebSocket based ApplicationAgent. It can be used together with the WebSocketAgentConnector to exchange Messages.

func NewWebSocketAgent

func NewWebSocketAgent() (wa *WebSocketAgent)

NewWebSocketAgent will be started with its handler. The ServeHTTP function must be bound to the HTTP server.

func (*WebSocketAgent) Endpoints

func (w *WebSocketAgent) Endpoints() []bpv7.EndpointID

Endpoints of all currently connected clients.

func (*WebSocketAgent) MessageReceiver

func (w *WebSocketAgent) MessageReceiver() chan Message

MessageReceiver is a channel on which the ApplicationAgent must listen for incoming Messages.

func (*WebSocketAgent) MessageSender

func (w *WebSocketAgent) MessageSender() chan Message

MessageSender is a channel to which the ApplicationAgent can send outgoing Messages.

func (*WebSocketAgent) ServeHTTP

func (w *WebSocketAgent) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP must be bound to a HTTP endpoint, e.g., to /ws by a http.ServeMux.

type WebSocketAgentConnector

type WebSocketAgentConnector struct {
	// contains filtered or unexported fields
}

WebSocketAgentConnector is the client side version of the WebSocketAgent.

func NewWebSocketAgentConnector

func NewWebSocketAgentConnector(apiUrl, endpointId string) (wac *WebSocketAgentConnector, err error)

NewWebSocketAgentConnector creates a new WebSocketAgentConnector connection to a WebSocketAgent.

func (*WebSocketAgentConnector) Close

func (wac *WebSocketAgentConnector) Close()

Close this WebSocketAgentConnector.

func (*WebSocketAgentConnector) ReadBundle

func (wac *WebSocketAgentConnector) ReadBundle() (b bpv7.Bundle, err error)

ReadBundle returns the next incoming Bundle. This method blocks.

func (*WebSocketAgentConnector) Syscall

func (wac *WebSocketAgentConnector) Syscall(request string, timeout time.Duration) (response []byte, err error)

Syscall will be send to the server. An answer or an error after a timeout will be returned.

func (*WebSocketAgentConnector) WriteBundle

func (wac *WebSocketAgentConnector) WriteBundle(b bpv7.Bundle) (err error)

WriteBundle sends a Bundle to a server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL