Skip to content

Commit

Permalink
Start planning packets with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaDoes committed Jul 19, 2023
1 parent 2213191 commit 2d242f8
Showing 1 changed file with 64 additions and 24 deletions.
88 changes: 64 additions & 24 deletions plugins.go
Original file line number Diff line number Diff line change
@@ -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
)
Expand Down Expand Up @@ -87,41 +139,29 @@ 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
}
}
return false
}

//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
}
}

0 comments on commit 2d242f8

Please sign in to comment.