From 2d242f8617a7048acb51bb8d2fa4de2a28dbb344 Mon Sep 17 00:00:00 2001 From: JoshuaDoes Date: Wed, 19 Jul 2023 20:58:56 +0000 Subject: [PATCH] Start planning packets with comments --- plugins.go | 88 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 24 deletions(-) diff --git a/plugins.go b/plugins.go index 07ab822..e2a86d3 100644 --- a/plugins.go +++ b/plugins.go @@ -1,11 +1,63 @@ package main import ( + //"encoding/json" "fmt" //"os/exec" "strings" ) +type Packet struct { + Channel string //The channel for this packet, should be unique per request as response must match + Opcode PacketOp //The operation to call + Data []byte //The data to use for this operation, if necessary +} + +/* Example packet order: (CHANNEL|OP|DATA), CHANNEL can be anything but must be unique so a counter is used in this example +-> 01|Ping| +<- 01|Pong|1234567890 +-> 02|AuthCheck| +<- 02|AuthResp|true +-> 03|ObjectGet|libremedia:stream:asdf1234 +<- 03|ObjectResp|{URI:"libremedia:stream:asdf1234",Type:"stream",Provider:"libremedia",Object:{...}} +-> 04|StreamReq|libremedia:stream:asdf1234 +<- 04|StreamResp|{audio data} +<- 04|StreamResp|{audio data} +<- 04|... +<- 04|StreamResp| +-> 05|Terminate| +<- 05|Terminate| +*/ + +// PacketOp is the operation being performed +type PacketOp int +const ( + //Pings the plugin with expectation of a pong response, plugin will be restarted/reconnected on timeout + PacketOpPing PacketOp = iota + //Pongs back in response to a ping, must hold current Unix epoch + PacketOpPong + //Requests to check if authentication succeeded, plugin session stalls until responded to or timeout + PacketOpAuthCheck + //Responds true or false for authentication, plugin is closed if false + PacketOpAuthResp + //Requests an object from the plugin, must hold URI string + PacketOpObjectGet + //Responds with an object encoded in JSON + PacketOpObjectResp + //Attempts to request the raw data of a stream format from the plugin, must hold PluginStreamRequest encoded in JSON + PacketOpStreamReq + //Responds sequentially to a stream request, must send response with empty data to terminate streaming session + PacketOpStreamResp + //Terminates the plugin, can be sent to plugin to request for it to shut down, or can be sent by plugin to let libremedia know it will no longer respond and must be closed + PacketOpTerminate +) + +// PluginStreamRequest holds a request to start a streaming session on the transport channel +type PluginStreamRequest struct { + URI string `json:"uri"` //The URI of the streamable object + Format int `json:"format"` //The format number to stream, according to the ordered list of formats in the stream object +} + var ( channels map[string][]*Packet ) @@ -87,17 +139,17 @@ func (p *Plugin) Send(op string, b []byte) error { func (p *Plugin) Store(b []byte) error { var packet *Packet //TODO: Decode b into *Packet - if _, exists := channels[packet.Id]; !exists { - channels[packet.Id] = make([]*Packet, 0) + if _, exists := channels[packet.Channel]; !exists { + channels[packet.Channel] = make([]*Packet, 0) } - channels[packet.Id] = append(channels[packet.Id], packet) + channels[packet.Channel] = append(channels[packet.Channel], packet) return nil } //IsPacketAvailable checks if a packet is available on the specified channel -func (p *Plugin) IsPacketAvailable(id string) bool { - if channel, exists := channels[id]; exists { - if len(channel) > 0 { +func (p *Plugin) IsPacketAvailable(channel string) bool { + if packets, exists := channels[channel]; exists { + if len(packets) > 0 { return true } } @@ -105,23 +157,11 @@ func (p *Plugin) IsPacketAvailable(id string) bool { } //ReadPacket reads the next packet from the given channel -func (p *Plugin) ReadPacket(id string) *Packet { - if channel, exists := channels[id]; exists { - if len(channel) > 0 { - packet := channel[0] - //TODO: Remove index 0 from slice stored in channels[id] map - return packet - } +func (p *Plugin) ReadPacket(channel string) *Packet { + if p.IsPacketAvailable(channel) { + packet := channels[channel][0] + //TODO: Remove index 0 from slice stored in channels[channel] map + return packet } return nil -} - -type Packet struct { - Id string `json:"id"` //The channel for this packet, should be unique per request as response will match - Op string `json:"op"` //The operation to call - Data []byte `json:"data"` //The data for this operation -} - -func NewPacket(op string, b []byte) []byte { - return nil -} +} \ No newline at end of file