-
Notifications
You must be signed in to change notification settings - Fork 2
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
Basic Peer #7
Basic Peer #7
Conversation
Create cumuluspeer BasicStreamHandler and ExtractPeerInfo Add logger Main now parses command line arguments specifying multiaddress of peer to connect to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job for your first Go code!
cumuluspeer/peer.go
Outdated
@@ -0,0 +1,145 @@ | |||
package cumuluspeer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just call this peer
. We know it's cumulus because inside the cumulus folder. Short and succinct package names
cumuluspeer/peer.go
Outdated
const ( | ||
DefaultPort = 8765 // Cumulus peers communicate over this TCP port | ||
CumulusProtocol = "/cumulate/0.0.1" // Cumulus communication protocol | ||
DefaultIP = "127.0.0.1" // Default Host IP address if none given |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run gofmt here.
cumuluspeer/peer.go
Outdated
|
||
const ( | ||
DefaultPort = 8765 // Cumulus peers communicate over this TCP port | ||
CumulusProtocol = "/cumulate/0.0.1" // Cumulus communication protocol |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why cumulate
instead of cumulus
?
cumuluspeer/peer.go
Outdated
// Create a Cumulus host. | ||
// This may throw an error if we fail to create a key pair, a pid, or a new | ||
// multiaddress. | ||
func MakeBasicHost(ip string, port int) (host.Host, pstore.Peerstore, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should wrap host.Host
with your own struct peer.Peer
and return that here. Also use the pattern NewPeer
rather than MakePeer
cumuluspeer/peer.go
Outdated
// Communicate with peers. | ||
// TODO: Update this to do something useful. For now it just reads from the | ||
// stream and writes back what it read. | ||
func doCumulate(s net.Stream) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a method on the Peer
struct
func (p *Peer) Receive(s net.String) {...}
cumuluspeer/peer_test.go
Outdated
@@ -0,0 +1,38 @@ | |||
package cumuluspeer_test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all files in the same folder have the same package name
cumuluspeer/peer_test.go
Outdated
import ( | ||
"testing" | ||
|
||
cumuluspeer "github.com/ubclaunchpad/cumulus/cumuluspeer" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we don't need this import
cumuluspeer/peer_test.go
Outdated
) | ||
|
||
// Tests if we can make a basic host on a valid TCP port | ||
func TestMakeBasicHostValidPort(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestNew...
main.go
Outdated
if *targetPeer == "" { | ||
// No target was specified, wait for incoming connections | ||
log.Info("No target provided. Listening for incoming connections...") | ||
select {} // Hang until someone connects to us |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will hang this goroutine forever, is that what we want?
main.go
Outdated
// when joining the Cumulus Network. | ||
// port is the port to communicate over (defaults to peer.DefaultPort). | ||
// ip is the public IP address of the this host. | ||
targetPeer := flag.String("t", "", "target peer to connect to") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we know the IP and the port of the multiaddr can we infer the rest? A multiaddr as a CLI flag is an awkward ui
cumuluspeer/peer.go
Outdated
doCumulate(s) | ||
} | ||
|
||
// Communicate with peers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re: https://golang.org/doc/effective_go.html#commentary. Function comments start with function name, ie // doCumulate communicates with peers.
The first sentence should be a one-sentence summary that starts with the name being declared. If every doc comment begins with the name of the item it describes, the output of godoc can usefully be run through
grep
.
peer/peer.go
Outdated
// Open a stream with the peer | ||
stream, err := p.NewStream(context.Background(), peerid, | ||
CumulusProtocol) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return stream, err
if you aren't handling error
peer/peer.go
Outdated
// NewPeer creates a Cumulus host with the given IP addr and TCP port. | ||
// This may throw an error if we fail to create a key pair, a pid, or a new | ||
// multiaddress. | ||
func NewPeer(ip string, port int) (*Peer, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're making a package with one primary export (ie Peer
) then just call the constructor New
. Then you're calling peer.New(...)
-- makes more sense semantically
What Changed:
cumuluspeer
withMakeBasicHost
functionalitymain.go
to take host port number, IP address and target peer to connect to as command line argumentscumuluspeer
cumuluspeer
to WikiRemaining TODOs
BasicStreamHandler
to actually communicate useful information between peer and host