Skip to content

Commit 72f9592

Browse files
authored
Release v1.2.3
2 parents 7daa451 + 00f7074 commit 72f9592

File tree

8 files changed

+278
-24
lines changed

8 files changed

+278
-24
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ P2Chat - is a core local messenger library, which based on Libp2p stack.
33

44
P2Chat basicaly supports discovery through **mDNS** service and support messaging via **PubSub**
55

6-
It supports next features:
6+
It supports following features:
77
- devices autodiscovery by `Rendezvous string`
88
- topic list exchanging between peers
99
- autoconnect group chats by `PubSub`
@@ -12,7 +12,8 @@ It supports next features:
1212

1313

1414
## Building
15-
Require go version >=1.12 , so make sure your `go version` is okay
15+
Require go version >=1.12 , so make sure your `go version` is okay.
16+
**WARNING!** Building happen only when this project locates outside of GOPATH environment.
1617

1718
```bash
1819
$ git clone https://github.com/MoonSHRD/p2chat

api/protocol.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ Flags:
55
- 0x0: Generic message
66
- 0x1: Request to get existing PubSub topics at the network
77
- 0x2: Response to the request for topics (ack)
8+
- 0x3: Request to ask peers for their MatrixID
9+
- 0x4: Response to the request for peers identity
810
*/
911
const (
10-
FLAG_GENERIC_MESSAGE int = 0x0
11-
FLAG_TOPICS_REQUEST int = 0x1
12-
FLAG_TOPICS_RESPONSE int = 0x2
12+
FlagGenericMessage int = 0x0
13+
FlagTopicsRequest int = 0x1
14+
FlagTopicsResponse int = 0x2
15+
FlagIdentityRequest int = 0x3
16+
FlagIdentityResponse int = 0x4
1317
)
1418

1519
// BaseMessage is the basic message format of our protocol
@@ -24,3 +28,11 @@ type GetTopicsRespondMessage struct {
2428
BaseMessage
2529
Topics []string `json:"topics"`
2630
}
31+
32+
// GetIdentityRespondMessage is the format of the message to answer of request for peer identity
33+
// Flag: 0x4
34+
type GetIdentityRespondMessage struct {
35+
BaseMessage
36+
Multiaddress string `json:"multiaddress"`
37+
MatrixID string `json:"matrix_id"`
38+
}

cmd/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func newTopic(topic string) {
103103
return
104104
case msg := <-incomingMessages:
105105
{
106-
handler.HandleIncomingMessage(msg, func(textMessage pkg.TextMessage) {
106+
handler.HandleIncomingMessage(serviceTopic, msg, func(textMessage pkg.TextMessage) {
107107
fmt.Printf("%s \x1b[32m%s\x1b[0m> ", textMessage.From, textMessage.Body)
108108
})
109109
}
@@ -135,7 +135,7 @@ func writeTopic(topic string) {
135135
}
136136
message := &api.BaseMessage{
137137
Body: text,
138-
Flag: api.FLAG_GENERIC_MESSAGE,
138+
Flag: api.FlagGenericMessage,
139139
}
140140

141141
sendData, err := json.Marshal(message)
@@ -204,7 +204,7 @@ func main() {
204204
// Set global PubSub object
205205
pubSub = pb
206206

207-
handler = pkg.NewHandler(pb, serviceTopic, &networkTopics)
207+
handler = pkg.NewHandler(pb, serviceTopic, sourceMultiAddr.String(), &networkTopics)
208208

209209
// Randezvous string = service tag
210210
// Disvover all peers with our service (all ms devices)

cmd/main_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"crypto/rand"
6+
"encoding/json"
7+
"fmt"
8+
"testing"
9+
"time"
10+
11+
"github.com/MoonSHRD/p2chat/api"
12+
"github.com/MoonSHRD/p2chat/pkg"
13+
"github.com/libp2p/go-libp2p"
14+
"github.com/libp2p/go-libp2p-core/crypto"
15+
"github.com/libp2p/go-libp2p-core/host"
16+
"github.com/libp2p/go-libp2p-core/peer"
17+
"github.com/libp2p/go-libp2p-core/peerstore"
18+
"github.com/libp2p/go-libp2p-core/protocol"
19+
pubsub "github.com/libp2p/go-libp2p-pubsub"
20+
"github.com/phayes/freeport"
21+
)
22+
23+
const (
24+
numberOfNodes = 3
25+
serviceTag = "moonshard"
26+
)
27+
28+
var (
29+
testHosts []host.Host
30+
testContexts []context.Context
31+
testHandlers []pkg.Handler
32+
testPubsubs []*pubsub.PubSub
33+
testSubscriptions []*pubsub.Subscription
34+
peerChan chan peer.AddrInfo
35+
)
36+
37+
// Creates mock host object
38+
func createHost() (context.Context, host.Host, error) {
39+
ctx, _ /* cancel */ := context.WithCancel(context.Background())
40+
// defer cancel()
41+
42+
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
43+
if err != nil {
44+
return nil, nil, err
45+
}
46+
47+
port, err := freeport.GetFreePort()
48+
if err != nil {
49+
return nil, nil, err
50+
}
51+
52+
host, err := libp2p.New(
53+
ctx,
54+
libp2p.Identity(prvKey),
55+
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%v", port)),
56+
)
57+
if err != nil {
58+
return nil, nil, err
59+
}
60+
61+
return ctx, host, nil
62+
}
63+
64+
func TestCreateHosts(t *testing.T) {
65+
for i := 0; i < numberOfNodes; i++ {
66+
tempCtx, tempHost, err := createHost()
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
71+
testHosts = append(testHosts, tempHost)
72+
testContexts = append(testContexts, tempCtx)
73+
}
74+
}
75+
76+
func TestMDNS(t *testing.T) {
77+
for i := 0; i < numberOfNodes; i++ {
78+
pb, err := pubsub.NewFloodsubWithProtocols(context.Background(), testHosts[i], []protocol.ID{protocol.ID("/moonshard/1.0.0")}, pubsub.WithMessageSigning(true), pubsub.WithStrictSignatureVerification(true))
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
83+
testPubsubs = append(testPubsubs, pb)
84+
testHandlers = append(testHandlers, pkg.NewHandler(pb, serviceTag, &networkTopics))
85+
86+
peerChan = pkg.InitMDNS(testContexts[i], testHosts[i], serviceTag)
87+
88+
subscription, err := pb.Subscribe(serviceTag)
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
testSubscriptions = append(testSubscriptions, subscription)
93+
94+
fmt.Println("Waiting for correct set up of PubSub...")
95+
time.Sleep(3 * time.Second)
96+
97+
for j := 0; j < i; j++ {
98+
select {
99+
case peer := <-peerChan:
100+
testHosts[i].Peerstore().AddAddr(peer.ID, peer.Addrs[0], peerstore.PermanentAddrTTL)
101+
102+
if err := testHosts[i].Connect(testContexts[i], peer); err != nil {
103+
t.Fatal(err)
104+
}
105+
default:
106+
}
107+
}
108+
}
109+
}
110+
111+
// Checks whether all nodes are connected to each other
112+
func TestGetPeers(t *testing.T) {
113+
for _, handler := range testHandlers {
114+
if len(handler.GetPeers(serviceTag)) != numberOfNodes-1 {
115+
t.Fatal("Not all nodes are connected to each other.")
116+
}
117+
}
118+
}
119+
120+
// Sends message to service topic
121+
func TestSendMessage(t *testing.T) {
122+
message := &api.BaseMessage{
123+
Body: fmt.Sprintf("%s send 'hello test'", testHosts[0].ID()),
124+
Flag: api.FlagGenericMessage,
125+
}
126+
127+
sendData, err := json.Marshal(message)
128+
if err != nil {
129+
t.Fatal("Error occurred when marshalling message object")
130+
}
131+
132+
err = testPubsubs[0].Publish(serviceTag, sendData)
133+
if err != nil {
134+
t.Fatal("Error occurred when publishing")
135+
}
136+
}
137+
138+
// Grabs message from service topic
139+
func TestGetMessage(t *testing.T) {
140+
for _, sub := range testSubscriptions[1:] {
141+
message, err := sub.Next(context.Background())
142+
if err != nil {
143+
t.Fatal(err)
144+
}
145+
146+
decodedMessage := &api.BaseMessage{}
147+
json.Unmarshal(message.Data, decodedMessage)
148+
149+
originalMessage := fmt.Sprintf("%s send 'hello test'", testHosts[0].ID())
150+
if decodedMessage.Body != originalMessage {
151+
t.Fatal("Message not does not match")
152+
}
153+
}
154+
}
155+
156+
func TestCloseHosts(t *testing.T) {
157+
for _, host := range testHosts {
158+
if err := host.Close(); err != nil {
159+
t.Fatal(fmt.Sprintf("Failed when closing host %v", host.ID()))
160+
}
161+
}
162+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ require (
88
github.com/libp2p/go-libp2p-core v0.0.6
99

1010
github.com/libp2p/go-libp2p-pubsub v0.1.0
11+
github.com/libp2p/go-libp2p-swarm v0.1.0
1112
github.com/multiformats/go-multiaddr v0.0.4
13+
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
1214
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
213213
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
214214
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
215215
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
216+
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
217+
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
216218
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
217219
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
218220
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

0 commit comments

Comments
 (0)