Skip to content

Commit

Permalink
all: rebase on master
Browse files Browse the repository at this point in the history
  • Loading branch information
iwasaki-kenta committed Feb 10, 2019
1 parent e104e05 commit d7fb0d7
Show file tree
Hide file tree
Showing 49 changed files with 4,467 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Perlin Network <support@perlin.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
@@ -0,0 +1,27 @@
# Noise

[![GoDoc][1]][2] [![Discord][7]][8] [![MIT licensed][5]][6] [![Build Status][9]][10] [![Go Report Card][11]][12] [![Coverage Statusd][13]][14]

[1]: https://godoc.org/github.com/perlin-network/noise?status.svg
[2]: https://godoc.org/github.com/perlin-network/noise
[5]: https://img.shields.io/badge/license-MIT-blue.svg
[6]: LICENSE
[7]: https://img.shields.io/discord/458332417909063682.svg
[8]: https://discord.gg/dMYfDPM
[9]: https://travis-ci.org/perlin-network/noise.svg?branch=master
[10]: https://travis-ci.org/perlin-network/noise
[11]: https://goreportcard.com/badge/github.com/perlin-network/noise
[12]: https://goreportcard.com/report/github.com/perlin-network/noise
[13]: https://codecov.io/gh/perlin-network/noise/branch/master/graph/badge.svg
[14]: https://codecov.io/gh/perlin-network/noise


<img align="right" width=400 src="media/chat.gif">

**noise** is an opinionated, easy-to-use P2P network stack for
*decentralized applications, and cryptographic protocols* written in
[Go](https://golang.org/) by Perlin Network.

**noise** is made to be robust, developer-friendly, performant, secure, and
cross-platform across multitudes of devices by making use of well-tested,
production-grade dependencies.
21 changes: 21 additions & 0 deletions callbacks.go
@@ -0,0 +1,21 @@
package noise

import "github.com/perlin-network/noise/payload"

type OnErrorCallback func(node *Node, err error) error
type OnPeerErrorCallback func(node *Node, peer *Peer, err error) error
type OnPeerDisconnectCallback func(node *Node, peer *Peer) error
type OnPeerInitCallback func(node *Node, peer *Peer) error

type BeforeMessageSentCallback func(node *Node, peer *Peer, msg []byte) ([]byte, error)
type BeforeMessageReceivedCallback func(node *Node, peer *Peer, msg []byte) ([]byte, error)

type AfterMessageSentCallback func(node *Node, peer *Peer) error
type AfterMessageReceivedCallback func(node *Node, peer *Peer) error

type AfterMessageEncodedCallback func(node *Node, peer *Peer, header, msg []byte) ([]byte, error)

type OnPeerDecodeHeaderCallback func(node *Node, peer *Peer, reader payload.Reader) error
type OnPeerDecodeFooterCallback func(node *Node, peer *Peer, msg []byte, reader payload.Reader) error

type OnMessageReceivedCallback func(node *Node, opcode Opcode, peer *Peer, message Message) error
10 changes: 10 additions & 0 deletions callbacks/mod.go
@@ -0,0 +1,10 @@
package callbacks

import (
"github.com/pkg/errors"
)

var DeregisterCallback = errors.New("callback deregistered")

type Callback func(params ...interface{}) error
type ReduceCallback func(in interface{}, params ...interface{}) (interface{}, error)
5 changes: 5 additions & 0 deletions callbacks/mod_test.go
@@ -0,0 +1,5 @@
package callbacks

const (
numCB = 10
)
48 changes: 48 additions & 0 deletions callbacks/opcode.go
@@ -0,0 +1,48 @@
package callbacks

import (
"math"
"sync"
)

// OpcodeCallbackManager maps opcodes to a sequential list of callback functions.
// We assumes that there are at most 1 << 8 - 1 callbacks (opcodes are represented as uint32).
type OpcodeCallbackManager struct {
sync.Mutex

callbacks [math.MaxUint8 + 1]*SequentialCallbackManager
}

func NewOpcodeCallbackManager() *OpcodeCallbackManager {
return &OpcodeCallbackManager{}
}

func (m *OpcodeCallbackManager) RegisterCallback(opcode byte, c Callback) {
m.Lock()

if m.callbacks[opcode] == nil {
m.callbacks[opcode] = NewSequentialCallbackManager()
}

manager := m.callbacks[opcode]

m.Unlock()

manager.RegisterCallback(c)
}

// RunCallbacks runs all callbacks on a variadic parameter list, and de-registers callbacks
// that throw an error.
func (m *OpcodeCallbackManager) RunCallbacks(opcode byte, params ...interface{}) (errs []error) {
m.Lock()
manager := m.callbacks[opcode]
m.Unlock()

if manager == nil {
return
}

errs = manager.RunCallbacks(params...)

return errs
}
90 changes: 90 additions & 0 deletions callbacks/opcode_test.go
@@ -0,0 +1,90 @@
package callbacks

import (
"testing"
)

// TODO(kenta): finish tests for opcode callbacks
func TestOpcodeCallback(t *testing.T) {
//{
// // test random order
// var results []int
// ocm := NewOpcodeCallbackManager()
// wg := &sync.WaitGroup{}
// for i := 0; i < numCB; i++ {
// wg.Add(1)
// go func(i int) {
// defer wg.Done()
// ocm.RegisterCallback(byte(i), func(params ...interface{}) error {
// results = append(results, i)
// return nil
// })
// }(i)
// }
// wg.Wait()
//
// // call the callbacks 3 times
// for j := 0; j < 3; j++ {
// results = nil
// for i := 0; i < numCB; i++ {
// errs := ocm.RunCallbacks(byte(i))
// assert.Equal(t, 0, len(errs))
// }
// assert.Equal(t, numCB, len(results))
// //t.Logf("res=%+v", results)
// }
//}
//{
// // test in order
// var results []int
// ocm := NewOpcodeCallbackManager()
// for i := 0; i < numCB; i++ {
// j := i
// ocm.RegisterCallback(byte(i), func(params ...interface{}) error {
// results = append(results, j)
// return nil
// })
// }
//
// // call the callbacks 3 times
// for j := 0; j < 3; j++ {
// results = nil
// for i := 0; i < numCB; i++ {
// errs := ocm.RunCallbacks(byte(i))
// assert.Equal(t, 0, len(errs))
// }
// assert.Equal(t, numCB, len(results))
// for i := 0; i < numCB; i++ {
// assert.Equal(t, i, results[i])
// }
// //t.Logf("res=%+v", results)
// }
//}
//{
// // test errors
// var results []int
// ocm := NewOpcodeCallbackManager()
// for i := 0; i < numCB; i++ {
// j := i
// ocm.RegisterCallback(byte(i), func(params ...interface{}) error {
// results = append(results, j)
// //return errors.Errorf("Error-%d", j)
// return DeregisterCallback
// })
// }
//
// // call the callbacks 3 times
// for j := 0; j < 3; j++ {
// results = nil
// for i := 0; i < numCB; i++ {
// errs := ocm.RunCallbacks(byte(i))
// assert.Equal(t, 0, len(errs))
// }
// assert.Equal(t, numCB, len(results))
// for i := 0; i < numCB; i++ {
// assert.Equal(t, i, results[i])
// }
// t.Logf("res=%+v", results)
// }
//}
}
40 changes: 40 additions & 0 deletions callbacks/reduce.go
@@ -0,0 +1,40 @@
package callbacks

type ReduceCallbackManager struct {
seqMgr *SequentialCallbackManager
}

func NewReduceCallbackManager() *ReduceCallbackManager {
return &ReduceCallbackManager{
seqMgr: NewSequentialCallbackManager(),
}
}

func (m *ReduceCallbackManager) UnsafelySetReverse() *ReduceCallbackManager {
m.seqMgr.UnsafelySetReverse()
return m
}

func (m *ReduceCallbackManager) RegisterCallback(c ReduceCallback) {
m.seqMgr.RegisterCallback(func(params ...interface{}) error {
valueOut := params[0].(*interface{})
var err error
*valueOut, err = c(*valueOut, params[1:]...)
return err
})
}

// RunCallbacks runs all callbacks on a variadic parameter list, and de-registers callbacks
// that throw an error.
func (m *ReduceCallbackManager) RunCallbacks(in interface{}, params ...interface{}) (res interface{}, errs []error) {
errs = m.seqMgr.RunCallbacks(append([]interface{}{&in}, params...)...)
res = in
return
}

// MustRunCallbacks runs all callbacks on a variadic parameter list, and de-registers callbacks
// that throw an error. Errors are ignored.
func (m *ReduceCallbackManager) MustRunCallbacks(in interface{}, params ...interface{}) interface{} {
out, _ := m.RunCallbacks(in, params...)
return out
}

0 comments on commit d7fb0d7

Please sign in to comment.