Skip to content

Updated to v1.8.0 (copied from Notehub) #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/blues/note

go 1.19
go 1.22

require (
github.com/blues/note-go v1.7.2
github.com/blues/note-go v1.8.0
github.com/golang/snappy v0.0.4
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d
github.com/google/uuid v1.3.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/blues/note-go v1.7.2 h1:hasFsMNTnvyp5MPpnhqL46LC1tqzxQ5sGg+NOcJUV8E=
github.com/blues/note-go v1.7.2/go.mod h1:GfslvbmFus7z05P1YykcbMedTKTuDNTf8ryBb1Qjq/4=
github.com/blues/note-go v1.8.0 h1:7h9xVXREnFK0bT7xcYyXq19s1yPcFhZjrkerqFV0TLg=
github.com/blues/note-go v1.8.0/go.mod h1:GfslvbmFus7z05P1YykcbMedTKTuDNTf8ryBb1Qjq/4=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10 changes: 6 additions & 4 deletions hub/discover.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import (
)

// NotehubDiscover is responsible for discovery of information about the services and apps
func NotehubDiscover(deviceUID string, deviceSN string, productUID string, hostname string, packetHandlerVersion string) (info notelib.DiscoverInfo, err error) {
func NotehubDiscover(deviceUID string, deviceSN string, productUID string, appUID string, needHandlerInfo bool, hostname string, packetHandlerVersion string) (info notelib.DiscoverInfo, err error) {
// Return basic info about the server
info.HubEndpointID = note.DefaultHubEndpointID
info.HubTimeNs = time.Now().UnixNano()
@@ -25,11 +25,13 @@ func NotehubDiscover(deviceUID string, deviceSN string, productUID string, hostn
err = err2
return
}
info.HubSessionHandler = device.Handler
info.HubSessionTicket = device.Ticket
info.HubSessionTicketExpiresTimeNs = device.TicketExpiresTimeSec * int64(1000000000)
info.HubDeviceStorageObject = notelib.FileStorageObject(deviceUID)
info.HubDeviceAppUID = device.AppUID
if needHandlerInfo {
info.HubSessionHandler = device.Handler
info.HubSessionTicket = device.Ticket
info.HubSessionTicketExpiresTimeNs = device.TicketExpiresTimeSec * int64(1000000000)
}
}

// Return the tcps issuer rootca cert, used for device-side certificate rotation
11 changes: 5 additions & 6 deletions hub/event.go
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@ import (
"github.com/blues/note-go/note"
notelib "github.com/blues/note/lib"
golc "github.com/google/open-location-code/go"
"github.com/google/uuid"
)

// Event log directory
@@ -27,12 +26,12 @@ func eventLogInit(dir string) {
}

// Event handling procedure
func notehubEvent(ctx context.Context, session *notelib.HubSession, local bool, file *notelib.Notefile, event *note.Event) (err error) {
func notehubEvent(ctx context.Context, session *notelib.HubSession, local bool, event *note.Event) (err error) {
// Retrieve the session context

// If this is a queue and this is a template note, recursively expand it to multiple notifications
if event.Bulk {
eventBulk(session, local, file, *event)
eventBulk(session, local, *event)
return
}

@@ -69,7 +68,7 @@ func notehubEvent(ctx context.Context, session *notelib.HubSession, local bool,
}

// For bulk data, process the template and payload, generating recursive notifications
func eventBulk(session *notelib.HubSession, local bool, file *notelib.Notefile, event note.Event) (err error) {
func eventBulk(session *notelib.HubSession, local bool, event note.Event) (err error) {
// Get the template from the note
bodyJSON, err := note.JSONMarshal(event.Body)
if err != nil {
@@ -107,8 +106,8 @@ func eventBulk(session *notelib.HubSession, local bool, file *notelib.Notefile,
nn.Bulk = false
nn.Body = &body
nn.Payload = payload
nn.EventUID = uuid.New().String()
notehubEvent(context.Background(), session, local, file, &nn)
nn.EventUID = notelib.GenerateEventUid(&nn)
notehubEvent(context.Background(), session, local, &nn)

}

6 changes: 3 additions & 3 deletions hub/http.go
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ func httpReqHandler(httpRsp http.ResponseWriter, httpReq *http.Request) {
reqJSON, err = io.ReadAll(httpReq.Body)
if err != nil {
err = fmt.Errorf("please supply a JSON request in the HTTP body")
io.WriteString(httpRsp, string(notelib.ErrorResponse(err)))
httpRsp.Write(notelib.ErrorResponse(err))
return
}

@@ -64,7 +64,7 @@ func httpReqHandler(httpRsp http.ResponseWriter, httpReq *http.Request) {
// Get the hub endpoint ID and storage object
var hubEndpointID, deviceStorageObject string
if err == nil {
_, hubEndpointID, _, deviceStorageObject, err = notelib.HubDiscover(deviceUID, "", device.ProductUID)
hubEndpointID, _, deviceStorageObject, err = notelib.HubDiscover(deviceUID, "", device.ProductUID, appUID)
}

// Process the request
@@ -73,7 +73,7 @@ func httpReqHandler(httpRsp http.ResponseWriter, httpReq *http.Request) {
var box *notelib.Notebox
box, err = notelib.OpenEndpointNotebox(ctx, hubEndpointID, deviceStorageObject, false)
if err == nil {
box.SetEventInfo(deviceUID, device.DeviceSN, device.ProductUID, appUID, notehubEvent, nil)
box.SetEventInfo(deviceUID, device.DeviceSN, device.ProductUID, appUID, "api:http", notehubEvent, nil)
rspJSON = box.Request(ctx, hubEndpointID, reqJSON)
box.Close(ctx)
}
5 changes: 2 additions & 3 deletions hub/session.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (
"net"

notelib "github.com/blues/note/lib"
"github.com/google/uuid"
)

// Process requests for the duration of a session being open
@@ -28,7 +27,7 @@ func sessionHandler(connSession net.Conn, secure bool) {
}

// Always start with a blank, inactive Session Context
sessionContext := notelib.NewHubSession(uuid.New().String(), sessionSource, secure)
sessionContext := notelib.NewHubSession(sessionSource, secure)

// Set up golang context with session context stored within
ctx := context.Background()
@@ -39,7 +38,7 @@ func sessionHandler(connSession net.Conn, secure bool) {
firstTransaction := sessionContext.Transactions == 0

// Extract a request from the wire, and exit if error
_, request, err = notelib.WireReadRequest(connSession, true)
_, request, err = notelib.WireReadRequest(ctx, connSession, true)
if err != nil {
if !notelib.ErrorContains(err, "{closed}") {
fmt.Printf("session: error reading request: %s\n", err)
7 changes: 7 additions & 0 deletions lib/bulk.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -608,6 +608,13 @@ func (tmplContext *BulkTemplateContext) BulkDecodeNextEntry() (body map[string]i
}
}

// Perform special processing of the body to remove a field added in the
// Notecard repo commit c29ba90d94687a442f3ed4e170372fb98f2200c9 wherein
// lora templates needed to be unique. This manifested itself as
// a field whose name is "_" being added to the template, which,
// if "noOmitEmpty", would actually show up to the user.
delete(body, "_")

// Exit if we'd encountered underrun
if tmplContext.BinUnderrun {
logDebug(context.Background(), "bin: bin underrun")
12 changes: 10 additions & 2 deletions lib/clang/notehub.pb.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.3.5 at Mon Sep 11 17:31:55 2023. */
/* Generated by nanopb-0.3.5 at Fri Sep 13 06:53:51 2024. */

#include "notehub.pb.h"

@@ -10,7 +10,7 @@



const pb_field_t notelib_NotehubPB_fields[60] = {
const pb_field_t notelib_NotehubPB_fields[62] = {
PB_FIELD( 1, INT64 , OPTIONAL, STATIC , FIRST, notelib_NotehubPB, Version, Version, 0),
PB_FIELD( 2, STRING , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, MessageType, Version, 0),
PB_FIELD( 3, STRING , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, Error, MessageType, 0),
@@ -70,8 +70,16 @@ const pb_field_t notelib_NotehubPB_fields[60] = {
PB_FIELD( 57, STRING , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, Where, SuppressResponse, 0),
PB_FIELD( 58, INT64 , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, WhereWhen, Where, 0),
PB_FIELD( 59, STRING , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, HubPacketHandler, WhereWhen, 0),
PB_FIELD( 60, UINT32 , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, PowerSource, HubPacketHandler, 0),
PB_FIELD( 61, DOUBLE , OPTIONAL, STATIC , OTHER, notelib_NotehubPB, PowerMahUsed, PowerSource, 0),
PB_LAST_FIELD
};


/* On some platforms (such as AVR), double is really float.
* These are not directly supported by nanopb, but see example_avr_double.
* To get rid of this error, remove any double fields from your .proto.
*/
PB_STATIC_ASSERT(sizeof(double) == 8, DOUBLE_MUST_BE_8_BYTES)

/* @@protoc_insertion_point(eof) */
16 changes: 11 additions & 5 deletions lib/clang/notehub.pb.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.3.5 at Mon Sep 11 17:31:55 2023. */
/* Generated by nanopb-0.3.5 at Fri Sep 13 06:53:51 2024. */

#ifndef PB_NOTEHUB_PB_H_INCLUDED
#define PB_NOTEHUB_PB_H_INCLUDED
@@ -134,14 +134,18 @@ typedef struct _notelib_NotehubPB {
int64_t WhereWhen;
bool has_HubPacketHandler;
char HubPacketHandler[254];
bool has_PowerSource;
uint32_t PowerSource;
bool has_PowerMahUsed;
double PowerMahUsed;
/* @@protoc_insertion_point(struct:notelib_NotehubPB) */
} notelib_NotehubPB;

/* Default values for struct fields */

/* Initializer values for message structs */
#define notelib_NotehubPB_init_default {false, 0, false, "", false, "", false, "", false, "", false, 0, false, "", false, "", false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, 0, false, ""}
#define notelib_NotehubPB_init_zero {false, 0, false, "", false, "", false, "", false, "", false, 0, false, "", false, "", false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, 0, false, ""}
#define notelib_NotehubPB_init_default {false, 0, false, "", false, "", false, "", false, "", false, 0, false, "", false, "", false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, 0, false, "", false, 0, false, 0}
#define notelib_NotehubPB_init_zero {false, 0, false, "", false, "", false, "", false, "", false, 0, false, "", false, "", false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, "", false, 0, false, 0, false, "", false, 0, false, 0, false, 0, false, 0, false, 0, false, 0, false, "", false, 0, false, "", false, "", false, 0, false, 0, false, 0, false, "", false, 0, false, "", false, 0, false, 0}

/* Field tags (for use in manual encoding/decoding) */
#define notelib_NotehubPB_Version_tag 1
@@ -203,12 +207,14 @@ typedef struct _notelib_NotehubPB {
#define notelib_NotehubPB_Where_tag 57
#define notelib_NotehubPB_WhereWhen_tag 58
#define notelib_NotehubPB_HubPacketHandler_tag 59
#define notelib_NotehubPB_PowerSource_tag 60
#define notelib_NotehubPB_PowerMahUsed_tag 61

/* Struct field encoding specification for nanopb */
extern const pb_field_t notelib_NotehubPB_fields[60];
extern const pb_field_t notelib_NotehubPB_fields[62];

/* Maximum encoded size of messages (where known) */
#define notelib_NotehubPB_size 4237
#define notelib_NotehubPB_size 4254

/* Message IDs (where set with "msgid" option) */
#ifdef PB_MSGID
20 changes: 9 additions & 11 deletions lib/concurrency_test.go
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ func TestConcurrency(t *testing.T) {

}

func parallelTest(t *testing.T, testName string, summarizeWork bool, workInstances int, workFn func(testid string) bool) {
func parallelTest(t *testing.T, testName string, summarizeWork bool, workInstances int, workFn func(ctx context.Context, testid string) bool) {
ctx := context.Background()

// Start the tests and notify the caller that we've started them
@@ -130,7 +130,7 @@ func parallelTest(t *testing.T, testName string, summarizeWork bool, workInstanc
}

// Perform work in parallel
func parallelWork(workFn func(testid string) bool, summarizeWork bool, pActive *int, iterations *uint64, errors *uint64) {
func parallelWork(workFn func(ctx context.Context, testid string) bool, summarizeWork bool, pActive *int, iterations *uint64, errors *uint64) {
ctx := context.Background()

// Notify the callers that we've started, and wait for 'go'
@@ -147,7 +147,7 @@ func parallelWork(workFn func(testid string) bool, summarizeWork bool, pActive *
localIterations := 0
testid := fmt.Sprintf("concurrency_test_%04d", id)
for !stopTests {
if !workFn(testid) {
if !workFn(ctx, testid) {
atomic.AddUint64(errors, 1)
} else {
atomic.AddUint64(iterations, 1)
@@ -168,9 +168,7 @@ func parallelWork(workFn func(testid string) bool, summarizeWork bool, pActive *
}

// Perform a single notebox test
func testNotebox(testid string) bool {
ctx := context.Background()

func testNotebox(ctx context.Context, testid string) bool {
box, err := OpenEndpointNotebox(ctx, "", FileStorageObject(testid), true)
if err != nil {
logDebug(ctx, "boxOpen: %s", err)
@@ -207,7 +205,7 @@ func testNotebox(testid string) bool {
}

for i := 0; i < 100; i++ {
err := nf.DeleteNote("", fmt.Sprintf("note%d", i))
err := nf.DeleteNote(ctx, "", fmt.Sprintf("note%d", i))
if err != nil {
return false
}
@@ -245,29 +243,29 @@ func testNotebox(testid string) bool {
}

// Perform a single notefile test
func testNotefile(testid string) bool {
func testNotefile(ctx context.Context, testid string) bool {

nf := CreateNotefile(false)
for i := 0; i < 100; i++ {
n, err := note.CreateNote([]byte("{\"hi\":\"there\"}"), nil)
if err != nil {
return false
}
_, err = nf.AddNote("", fmt.Sprintf("note%d", i), n)
_, err = nf.AddNote(ctx, "", fmt.Sprintf("note%d", i), n)
if err != nil {
return false
}
}
for i := 0; i < 100; i++ {
err := nf.DeleteNote("", fmt.Sprintf("note%d", i))
err := nf.DeleteNote(ctx, "", fmt.Sprintf("note%d", i))
if err != nil {
return false
}
n, err := note.CreateNote([]byte("{\"hi\":\"there\"}"), nil)
if err != nil {
return false
}
_, err = nf.AddNote("", "", n)
_, err = nf.AddNote(ctx, "", "", n)
if err != nil {
return false
}
36 changes: 34 additions & 2 deletions lib/debug.go
Original file line number Diff line number Diff line change
@@ -12,12 +12,13 @@ import (
"runtime"
"strconv"
"strings"
"time"
)

// Debugging details
var (
synchronous = true
debugEvent = true
DebugEvent = true
debugBox = false
debugSync = false
debugSyncMax = false
@@ -31,7 +32,7 @@ var (
)

var vars = [...]*bool{
&debugEvent, &debugBox, &debugSync, &debugSyncMax, &debugCompress, &debugHubRequest, &debugRequest, &debugFile,
&DebugEvent, &debugBox, &debugSync, &debugSyncMax, &debugCompress, &debugHubRequest, &debugRequest, &debugFile,
}

var debugEnvInitialized = false
@@ -207,3 +208,34 @@ func loggerHandler() {
runtime.Gosched()
}
}

// Default timewarn period used for suppressing LogInfo entirely
const LogPeriodSuppressSecs = 2

// PeriodicInfo is a struct that enables information to be displayed only periodically,
// for 'sampled' tracing purposes, rather than on a continuous basis
type LogPeriod struct {
LastLog time.Time
PeriodSecs int
}

// LogInfoPeriodReset ensures that the very next period log does a trace
func LogInfoPeriodReset(period *LogPeriod) {
period.LastLog = time.Time{}
}

// LogInfoPeriodically writes an unstructured log message with the INFO level but only periodically
func LogInfoPeriodically(ctx context.Context, period *LogPeriod, msg string, v ...interface{}) {
if !period.LastLog.IsZero() && time.Since(period.LastLog) < time.Duration(period.PeriodSecs)*time.Second {
return
}
logInfo(ctx, msg, v...)
period.LastLog = time.Now()
}

// LogInfoBumpTrace bumps a trace interval
func LogInfoBumpTrace(prev *time.Time) (duration time.Duration) {
duration = time.Since(*prev)
*prev = time.Now()
return
}
Loading