test implementation of channels for IPC, prep for currency

This commit is contained in:
kayos 2021-05-31 01:24:09 -07:00
parent 086b479e43
commit 91ba197c6c
3 changed files with 46 additions and 9 deletions

35
main.go

@ -15,9 +15,6 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
// ScanMgr will keep track of concurrently running scans
var ScanMgr *protogen.Meta
func listen() { func listen() {
flag.Parse() flag.Parse()
@ -58,9 +55,10 @@ func init() {
dust.PrintBanner() dust.PrintBanner()
ScanMgr = &protogen.Meta{ protogen.ScanMgr = &protogen.Meta{
Count: 0, Count: 0,
Scans: make(map[int]*protogen.Scan), Scans: make(map[int]*protogen.Scan),
Mailbox: make(chan protogen.Postcard),
} }
// setup json logfile // setup json logfile
@ -107,9 +105,30 @@ func main() {
defer eros.Slumber() defer eros.Slumber()
var scan *protogen.Scan var scan *protogen.Scan
scan = ScanMgr.NewScan() go func() {
for {
select {
case postcard := <- protogen.ScanMgr.Mailbox:
log.Info().
Int("originating_scan_id", postcard.From).
Str("message", postcard.Message).
Msg("Mailbox_IPC")
if postcard.Message == "quit" {
log.Warn().
Int("from", postcard.From).
Msg("Received command to quit via Mailbox")
os.Exit(0)
}
}
}
}()
scan = protogen.ScanMgr.NewScan()
//time.Sleep(30 * time.Millisecond) //time.Sleep(30 * time.Millisecond)
dust.Must("Scan", scan.Start()) go dust.Must("Scan", scan.Start())
} }

@ -12,6 +12,9 @@ import (
"time" "time"
) )
// ScanMgr will keep track of concurrently running scans
var ScanMgr *Meta
func (m *Meta) NewScan() *Scan { func (m *Meta) NewScan() *Scan {
// Here we are creating an "anonymous" instance of a Scan struct // Here we are creating an "anonymous" instance of a Scan struct
// You'll notice it doesn't contain most of the data that actually is supposed to live within that struct // You'll notice it doesn't contain most of the data that actually is supposed to live within that struct

@ -16,6 +16,8 @@ type Meta struct {
Count int Count int
Scans map[int]*Scan Scans map[int]*Scan
Mailbox chan Postcard
// // // Future Concepts // // // // // // Future Concepts // // //
// BLEScans map[int]*BLEScan // BLEScans map[int]*BLEScan
// LoraScans map[int]*LoraScan // LoraScans map[int]*LoraScan
@ -23,12 +25,25 @@ type Meta struct {
// WiFiScans map[int]*WiFiScan // WiFiScans map[int]*WiFiScan
} }
// Postcard will encapsulate interprocess communication messages that we send back to the main thread
type Postcard struct {
// From is the ScanID the message originated from
From int
Stamp time.Time
// Device ID - relevant DeviceID (if applicable) - Remember to use Scan.Devices[id] for easy referral
DeviceID int
// probably a string
Message string
}
// Instance of a BLE scan // Instance of a BLE scan
type Scan struct { type Scan struct {
Count int Count int
// The ID is how we will refer back to running scans during IPC to react or cancel a scan
ID int ID int
Started time.Time Started time.Time
Activity time.Time Activity time.Time
Devices map[int]*eros.Device Devices map[int]*eros.Device
} }