Skip to content

Commit

Permalink
readme: Fix the plugin example and make description more detailed
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed Jul 8, 2018
1 parent 9f1b746 commit 488af9d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ Now that you have your keys, we can start listening and handling messages from i
```go
builder := builders.NewNetworkBuilder()

// Set the address in which peers will use to connect to you.
// Set the address which noise will listen on and peers will use to connect to you.
// For example, set the host part to `localhost` if you are testing locally, or your public IP
// address if you are connected to the internet directly.
builder.SetAddress("tcp://localhost:3000")

// Alternatively...
Expand All @@ -111,7 +113,7 @@ builder.SetAddress(network.FormatAddress("tcp", "localhost", 3000))
// Set the cryptographic keys used for your network.
builder.SetKeys(keys)

// ... add plugins or set anymore options here.
// ... add plugins or set any more options here.

// Build the network.
net, err := builder.Build()
Expand All @@ -136,20 +138,24 @@ net.BlockUntilListening()
```

... in any goroutine you desire. The goroutine will block until the server is ready to start listening.

See `examples/getting_started` for a full working example to get started with.

## Plugins

Plugins are a way to interface with the lifecycle of your network.


```go
type Plugin struct{}

func (*Plugin) Startup(net *Network) {}
func (*Plugin) Receive(ctx *PluginContext) error { return nil }
func (*Plugin) Cleanup(net *Network) {}
func (*Plugin) PeerConnect(client *PeerClient) {}
func (*Plugin) PeerDisconnect(client *PeerClient) {}
type YourAwesomePlugin struct {
network.Plugin
}

func (state *YourAwesomePlugin) Startup(net *network.Network) {}
func (state *YourAwesomePlugin) Receive(ctx *network.PluginContext) error { return nil }
func (state *YourAwesomePlugin) Cleanup(net *network.Network) {}
func (state *YourAwesomePlugin) PeerConnect(client *network.PeerClient) {}
func (state *YourAwesomePlugin) PeerDisconnect(client *network.PeerClient) {}
```

They are registered through `builders.NetworkBuilder` through the following:
Expand All @@ -158,7 +164,7 @@ They are registered through `builders.NetworkBuilder` through the following:
builder := builders.NewNetworkBuilder()

// Add plugin.
builder.AddPlugin(new(Plugin))
builder.AddPlugin(new(YourAwesomePlugin))
```

**noise** comes with three plugins: `discovery.Plugin`, `backoff.Plugin` and `nat.Plugin`.
Expand Down

0 comments on commit 488af9d

Please sign in to comment.