mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-20 20:07:51 +01:00
Implement backend.Stdout that can be subscribed/unsubscribed.
This commit is contained in:
@@ -8,6 +8,7 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/PeernetOfficial/core/dht"
|
"github.com/PeernetOfficial/core/dht"
|
||||||
@@ -214,7 +215,7 @@ func (peer *PeerInfo) cmdPong(msg *protocol.MessageRaw, connection *Connection)
|
|||||||
|
|
||||||
// cmdChat handles a chat message [debug]
|
// cmdChat handles a chat message [debug]
|
||||||
func (peer *PeerInfo) cmdChat(msg *protocol.MessageRaw, connection *Connection) {
|
func (peer *PeerInfo) cmdChat(msg *protocol.MessageRaw, connection *Connection) {
|
||||||
fmt.Printf("Chat from '%s': %s\n", connection.Address.String(), string(msg.PacketRaw.Payload))
|
fmt.Fprintf(peer.Backend.Stdout, "Chat from %s '%s': %s\n", hex.EncodeToString(peer.PublicKey.SerializeCompressed()), connection.Address.String(), string(msg.PacketRaw.Payload))
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmdLocalDiscovery handles an incoming announcement via local discovery
|
// cmdLocalDiscovery handles an incoming announcement via local discovery
|
||||||
|
|||||||
45
Filter.go
45
Filter.go
@@ -9,12 +9,15 @@ Filters allow the caller to intercept events. The filter functions must not modi
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/PeernetOfficial/core/blockchain"
|
"github.com/PeernetOfficial/core/blockchain"
|
||||||
"github.com/PeernetOfficial/core/btcec"
|
"github.com/PeernetOfficial/core/btcec"
|
||||||
"github.com/PeernetOfficial/core/dht"
|
"github.com/PeernetOfficial/core/dht"
|
||||||
"github.com/PeernetOfficial/core/protocol"
|
"github.com/PeernetOfficial/core/protocol"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Filters contains all functions to install the hook. Use nil for unused.
|
// Filters contains all functions to install the hook. Use nil for unused.
|
||||||
@@ -123,3 +126,45 @@ func (backend *Backend) initFilters() {
|
|||||||
func DefaultLogError(function, format string, v ...interface{}) {
|
func DefaultLogError(function, format string, v ...interface{}) {
|
||||||
log.Printf("["+function+"] "+format, v...)
|
log.Printf("["+function+"] "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MultiWriter code that allows to subscribe/unsubscribe.
|
||||||
|
type multiWriter struct {
|
||||||
|
writers map[uuid.UUID]io.Writer
|
||||||
|
sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a new writer that duplicates its writes to all the subscribed writers.
|
||||||
|
// Each write is written to each subscribed writer, one at a time. If any writer returns an error, the entire write operation continues.
|
||||||
|
func newMultiWriter() *multiWriter {
|
||||||
|
return &multiWriter{writers: make(map[uuid.UUID]io.Writer)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe a new writer to the list of writers
|
||||||
|
func (m *multiWriter) Subscribe(writer io.Writer) (id uuid.UUID) {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
id = uuid.New()
|
||||||
|
m.writers[id] = writer
|
||||||
|
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsubscribe a writer from the list of writers
|
||||||
|
func (m *multiWriter) Unsubscribe(id uuid.UUID) {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
delete(m.writers, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write a slice of byte to each of the subscribed writers. It will not return any errors.
|
||||||
|
func (m *multiWriter) Write(p []byte) (n int, err error) {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
for _, w := range m.writers {
|
||||||
|
w.Write(p)
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func Init(UserAgent string, ConfigFilename string, Filters *Filters) (backend *B
|
|||||||
backend = &Backend{
|
backend = &Backend{
|
||||||
ConfigFilename: ConfigFilename,
|
ConfigFilename: ConfigFilename,
|
||||||
userAgent: UserAgent,
|
userAgent: UserAgent,
|
||||||
|
Stdout: newMultiWriter(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if Filters != nil {
|
if Filters != nil {
|
||||||
@@ -108,4 +109,7 @@ type Backend struct {
|
|||||||
|
|
||||||
// peerMonitor is a list of channels receiving information about new peers
|
// peerMonitor is a list of channels receiving information about new peers
|
||||||
peerMonitor []chan<- *PeerInfo
|
peerMonitor []chan<- *PeerInfo
|
||||||
|
|
||||||
|
// Stdout bundles any output for the end-user. Writers may subscribe/unsubscribe.
|
||||||
|
Stdout *multiWriter
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user