Skip to content

Commit

Permalink
Remove message acknowledgement
Browse files Browse the repository at this point in the history
Instead wait for an EOF from the other side.
  • Loading branch information
dennis-tra committed Feb 4, 2021
1 parent a951f82 commit f7b1e02
Show file tree
Hide file tree
Showing 22 changed files with 574 additions and 895 deletions.
8 changes: 7 additions & 1 deletion cmd/pcp/pcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ var (
func main() {

app := &cli.App{
Name: "pcp",
Name: "pcp",
Authors: []*cli.Author{
{
Name: "Dennis Trautwein",
Email: "[email protected]",
},
},
Usage: "Peer Copy, a peer-to-peer data transfer tool.",
Version: fmt.Sprintf("%s+%s", Version, Build[:7]),
EnableBashCompletion: true,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/stretchr/testify v1.6.1
github.com/urfave/cli/v2 v2.3.0
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9
go.uber.org/atomic v1.6.0
go.uber.org/zap v1.16.0 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
Expand Down
1 change: 0 additions & 1 deletion internal/app/ioutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package app

import (
Expand Down
16 changes: 8 additions & 8 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

// Bytes attaches a unit to the bytes value and makes it human readable.
func Bytes(bytes int64) string {
if bytes >= 1E12 {
return fmt.Sprintf("%dTB", bytes/1E12)
} else if bytes >= 1E9 {
return fmt.Sprintf("%dGB", bytes/1E9)
} else if bytes >= 1E6 {
return fmt.Sprintf("%dMB", bytes/1E6)
} else if bytes >= 1E3 {
return fmt.Sprintf("%dKB", bytes/1E3)
if bytes >= 1e12 {
return fmt.Sprintf("%dTB", bytes/1e12)
} else if bytes >= 1e9 {
return fmt.Sprintf("%dGB", bytes/1e9)
} else if bytes >= 1e6 {
return fmt.Sprintf("%dMB", bytes/1e6)
} else if bytes >= 1e3 {
return fmt.Sprintf("%dKB", bytes/1e3)
} else {
return fmt.Sprintf("%dB", bytes)
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -80,6 +81,19 @@ func FillContext(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, ContextKey, conf), nil
}

func FromContext(ctx context.Context) (*Config, error) {
obj := ctx.Value(ContextKey)
if obj == nil {
return nil, fmt.Errorf("config not found in context")
}
config, ok := obj.(*Config)
if !ok {
return nil, fmt.Errorf("config not found in context")
}

return config, nil
}

func save(relPath string, obj interface{}, perm os.FileMode) error {

path, err := appXdg.ConfigFile(relPath)
Expand Down
8 changes: 3 additions & 5 deletions pkg/initialize/cmd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package initialize

import (
"fmt"
"os"

"github.com/dennis-tra/pcp/internal/log"
"github.com/urfave/cli/v2"

"github.com/dennis-tra/pcp/pkg/config"
Expand All @@ -29,11 +27,11 @@ func Action(c *cli.Context) error {
}

if conf.Settings.Exists {
fmt.Fprintln(os.Stderr, "Loaded settings.json from: ", conf.Settings.Path)
log.Infoln("Loaded settings.json from: ", conf.Settings.Path)
}

if conf.Identity.Exists {
fmt.Fprintln(os.Stderr, "Loaded identity.json from: ", conf.Identity.Path)
log.Infoln("Loaded identity.json from: ", conf.Identity.Path)
}

if !conf.Identity.IsInitialized() {
Expand Down
7 changes: 3 additions & 4 deletions pkg/node/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package node

import (
"context"
"fmt"
"os"
"sort"
"sync"
"time"
Expand All @@ -12,6 +10,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/discovery"

"github.com/dennis-tra/pcp/internal/app"
"github.com/dennis-tra/pcp/internal/log"
"github.com/dennis-tra/pcp/pkg/commons"
)

Expand Down Expand Up @@ -137,7 +136,7 @@ func (m *MdnsProtocol) PeersList() []peer.AddrInfo {
// to be selected by the user via its index.
func (m *MdnsProtocol) PrintPeers(peers []peer.AddrInfo) {
for i, p := range peers {
fmt.Fprintf(os.Stdout, "[%d] %s\n", i, p.ID)
log.Infof("[%d] %s\n", i, p.ID)
}
fmt.Fprintln(os.Stdout, )
log.Infoln()
}
71 changes: 35 additions & 36 deletions pkg/node/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -112,7 +111,7 @@ func TestMdnsProtocol_HandlePeerFound_deletesPeerAfterGCTime(t *testing.T) {

gcDurationTmp := gcDuration
gcDuration = 0 * time.Millisecond
peerId := peer.ID("peer-id")
peerID := peer.ID("peer-id")

var wg sync.WaitGroup
wg.Add(1)
Expand All @@ -130,12 +129,12 @@ func TestMdnsProtocol_HandlePeerFound_deletesPeerAfterGCTime(t *testing.T) {

appTime = mTime

pi := peer.AddrInfo{ID: peerId}
pi := peer.AddrInfo{ID: peerID}
p.HandlePeerFound(pi)

wg.Wait()

_, found := p.Peers.Load(peerId)
_, found := p.Peers.Load(peerID)
assert.False(t, found)

gcDuration = gcDurationTmp
Expand All @@ -150,7 +149,7 @@ func TestMdnsProtocol_HandlePeerFound_deletesPeerAfterGCTimeWithIntermediateRese

gcDurationTmp := gcDuration
gcDuration = 100 * time.Millisecond
peerId := peer.ID("peer-id")
peerID := peer.ID("peer-id")

mTime := mock.NewMockTimer(ctrl)
mTime.EXPECT().
Expand All @@ -159,15 +158,15 @@ func TestMdnsProtocol_HandlePeerFound_deletesPeerAfterGCTimeWithIntermediateRese
DoAndReturn(time.AfterFunc)
appTime = mTime

pi := peer.AddrInfo{ID: peerId}
pi := peer.AddrInfo{ID: peerID}
p.HandlePeerFound(pi)
time.Sleep(50 * time.Millisecond)
p.HandlePeerFound(pi)
time.Sleep(75 * time.Millisecond)
_, found := p.Peers.Load(peerId)
_, found := p.Peers.Load(peerID)
assert.True(t, found)
time.Sleep(50 * time.Millisecond)
_, found = p.Peers.Load(peerId)
_, found = p.Peers.Load(peerID)
assert.False(t, found)

gcDuration = gcDurationTmp
Expand All @@ -192,31 +191,31 @@ func TestMdnsProtocol_PeerList_returnsListOfPeers(t *testing.T) {
assert.Equal(t, p3, list[2])
}

func ExampleMdnsProtocol_PrintPeers_doesNotError() {

ctx := context.Background()
net := mocknet.New(ctx)
h, _ := net.GenPeer()

n := &Node{Host: h}
p := NewMdnsProtocol(n)

id1, _ := peer.Decode("QmUfTE3fCY8DvSAG5XGkmVvuqyihxujH52mbm8RCFJGMwD")
id2, _ := peer.Decode("12D3KooWFDN3sDfvn9dPvFZG4P1hMg21PTmUt56zCtX7VWRZkLV8")
id3, _ := peer.Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")

p1 := peer.AddrInfo{ID: id1}
p2 := peer.AddrInfo{ID: id2}
p3 := peer.AddrInfo{ID: id3}

p.HandlePeerFound(p1)
p.HandlePeerFound(p3)
p.HandlePeerFound(p2)

p.PrintPeers(p.PeersList())

// Output:
// [0] 12D3KooWFDN3sDfvn9dPvFZG4P1hMg21PTmUt56zCtX7VWRZkLV8
// [1] QmUfTE3fCY8DvSAG5XGkmVvuqyihxujH52mbm8RCFJGMwD
// [2] QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
}
//func ExampleMdnsProtocol_PrintPeers_doesNotError() {
//
// ctx := context.Background()
// net := mocknet.New(ctx)
// h, _ := net.GenPeer()
//
// n := &Node{Host: h}
// p := NewMdnsProtocol(n)
//
// id1, _ := peer.Decode("QmUfTE3fCY8DvSAG5XGkmVvuqyihxujH52mbm8RCFJGMwD")
// id2, _ := peer.Decode("12D3KooWFDN3sDfvn9dPvFZG4P1hMg21PTmUt56zCtX7VWRZkLV8")
// id3, _ := peer.Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
//
// p1 := peer.AddrInfo{ID: id1}
// p2 := peer.AddrInfo{ID: id2}
// p3 := peer.AddrInfo{ID: id3}
//
// p.HandlePeerFound(p1)
// p.HandlePeerFound(p3)
// p.HandlePeerFound(p2)
//
// p.PrintPeers(p.PeersList())
//
// // Output:
// // [0] 12D3KooWFDN3sDfvn9dPvFZG4P1hMg21PTmUt56zCtX7VWRZkLV8
// // [1] QmUfTE3fCY8DvSAG5XGkmVvuqyihxujH52mbm8RCFJGMwD
// // [2] QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
//}
Loading

0 comments on commit f7b1e02

Please sign in to comment.