Skip to content

Commit 7a65aa6

Browse files
authored
Release v1.2.0
2 parents 2938308 + 190c85d commit 7a65aa6

File tree

5 files changed

+41
-151
lines changed

5 files changed

+41
-151
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
deps:
2-
// not sure that we need to type it right here
3-
// alternativly user should type it in terminal before making deps cause of go modules
4-
export GO111MODULE=on
5-
go get -t -d ./...
1+
all:
2+
cd cmd; \
3+
go build
4+
update-deps:
5+
go mod tidy

README.md

Lines changed: 13 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,24 @@
1-
# p2chat
1+
# P2Chat
2+
P2Chat - is a core local messenger library, which based on Libp2p stack.
23

3-
## What is this and how do I do rest of my life about it?
4-
p2hcat - is a core local messenger library, which based on Libp2p stack.
5-
6-
p2chat basicly supports discovery through **mdns** service and support messaging via **PubSub**
4+
P2Chat basicaly supports discovery through **mDNS** service and support messaging via **PubSub**
75

86
It supports next features:
9-
- devices autodiscovery by `Rendez-vous string`
7+
- devices autodiscovery by `Rendezvous string`
108
- topic list exchanging between peers
119
- autoconnect group chats by `PubSub`
12-
- default signing and validating messages (crypto)
10+
- default signing and validating messages (crypto) *[validation is temporary off, due to the incorrect signing messages on Android]*
1311
- crossplatform
1412

1513

16-
## How to build
17-
require go version >=1.12 , so make sure your `go version` is okay
18-
19-
If it start yelling about go modules, try
20-
```
21-
export GO111MODULE=on
22-
```
23-
I've include it into Makefile, but not sure it will work correctly
24-
25-
26-
27-
### How to build mDNS
28-
```
29-
go get -v -d ./...
30-
go build
31-
```
32-
33-
**Note** - it may require to use modules (with specifiec versions)
34-
35-
### How to use mDNS
36-
37-
Use two different terminal windows to run
38-
```
39-
./mDNS -port 6666
40-
./mDNS -port 6667
41-
```
42-
43-
## So how does it work?
44-
45-
1. **Configure a p2p host**
46-
```go
47-
ctx := context.Background()
48-
49-
// libp2p.New constructs a new libp2p Host.
50-
// Other options can be added here.
51-
host, err := libp2p.New(ctx)
52-
```
53-
[libp2p.New](https://godoc.org/github.com/libp2p/go-libp2p#New) is the constructor for libp2p node. It creates a host with given configuration.
54-
55-
2. **Set a default handler function for incoming connections.**
56-
57-
This function is called on the local peer when a remote peer initiate a connection and starts a stream with the local peer.
58-
```go
59-
// Set a function as stream handler.
60-
host.SetStreamHandler("/chat/1.1.0", handleStream)
61-
```
62-
63-
```handleStream``` is executed for each new stream incoming to the local peer. ```stream``` is used to exchange data between local and remote peer. This example uses non blocking functions for reading and writing from this stream.
64-
65-
```go
66-
func handleStream(stream net.Stream) {
67-
68-
// Create a buffer stream for non blocking read and write.
69-
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
70-
71-
go readData(rw)
72-
go writeData(rw)
73-
74-
// 'stream' will stay open until you close it (or the other side closes it).
75-
}
76-
```
77-
78-
3. **Find peers nearby using mdns**
79-
80-
Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#NewMdnsService) service in host.
81-
82-
```go
83-
peerChan := initMDNS(ctx, host, cfg.RendezvousString)
84-
```
85-
register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#Notifee) with service so that we get notified about peer discovery
86-
87-
```go
88-
n := &discoveryNotifee{}
89-
ser.RegisterNotifee(n)
90-
```
91-
92-
93-
4. **Open streams to peers found.**
94-
95-
Finally we open stream to the peers we found, as we find them
96-
97-
```go
98-
peer := <-peerChan // will block untill we discover a peer
99-
fmt.Println("Found peer:", peer, ", connecting")
100-
101-
if err := host.Connect(ctx, peer); err != nil {
102-
fmt.Println("Connection failed:", err)
103-
}
104-
105-
// open a stream, this stream will be handled by handleStream other end
106-
stream, err := host.NewStream(ctx, peer.ID, protocol.ID(cfg.ProtocolID))
107-
108-
if err != nil {
109-
fmt.Println("Stream open failed", err)
110-
} else {
111-
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
112-
113-
go writeData(rw)
114-
go readData(rw)
115-
fmt.Println("Connected to:", peer)
116-
}
117-
```
118-
119-
### How to build Android module
120-
121-
**WARNING** - android module was transfered into https://github.com/MoonSHRD/p2chat-android
122-
123-
```
124-
cd ./mDNS/android/
125-
gomobile bind -target=android -v
126-
```
127-
128-
this will generate you `*.aar` and `*.jar`packages for android
129-
130-
then, open your project in android studio, go `File -> ProjectStructure -> modules -> new module -> Import aar/jar`
131-
and then open "*.aar" file.
132-
133-
then you should press 'apply' and also add it as a dependancy to the project. You swicth for dependancy tab, then choose app module itself, then, in right window click add and add p2mobile module as a dependancy.
134-
135-
By default, you will able to invoke any experted functions (those one, which start with **C**apital letter in go code.
136-
137-
138-
## What types and functions will be accesable from p2chat in my android app?
139-
140-
If you want be able to invoke any go functions from java side, you need to export them via renaming exported functions with Capital Letter like this `Start()`. Note, if you want to export functions with an unusual type, than you need to create a structure in go with this type and export it as well.
141-
142-
From java side just type `import p2mobile.*;` and then invoke like `P2mobile.Start()`
143-
14414
## Building
15+
Require go version >=1.12 , so make sure your `go version` is okay
16+
14517
```bash
146-
$ cd cmd
147-
$ go build
18+
$ git clone https://github.com/MoonSHRD/p2chat
19+
$ cd p2chat
20+
$ go mod tidy
21+
$ go get -v -d ./... # not sure that it's neccessary
22+
$ make
14823
```
24+
Builded binary will be in `./cmd/`

cmd/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
"time"
1616

17-
api "github.com/MoonSHRD/p2chat/api"
18-
pkg "github.com/MoonSHRD/p2chat/pkg"
17+
"github.com/MoonSHRD/p2chat/api"
18+
"github.com/MoonSHRD/p2chat/pkg"
1919
mapset "github.com/deckarep/golang-set"
2020
"github.com/libp2p/go-libp2p"
2121
"github.com/libp2p/go-libp2p-core/crypto"
@@ -238,7 +238,10 @@ MainLoop:
238238
case msg := <-incomingMessages:
239239
{
240240
handler.HandleIncomingMessage(msg, func(textMessage pkg.TextMessage) {
241-
fmt.Printf("%s \x1b[32m%s\x1b[0m> ", textMessage.From, textMessage.Body)
241+
// Green console colour: \x1b[32m
242+
// Reset console colour: \x1b[0m
243+
fmt.Printf("%s > \x1b[32m%s\x1b[0m", textMessage.From, textMessage.Body)
244+
fmt.Print("> ")
242245
})
243246
}
244247
case newPeer := <-peerChan:
@@ -251,7 +254,8 @@ MainLoop:
251254
if err := host.Connect(ctx, newPeer); err != nil {
252255
fmt.Println("Connection failed:", err)
253256
}
254-
fmt.Println("\nConnected to:", newPeer)
257+
fmt.Println("Connected to:", newPeer)
258+
fmt.Println("> ")
255259
}
256260
}
257261
}

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
279279
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
280280
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
281281
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
282+
golang.org/x/tools v0.0.0-20190226205152-f727befe758c h1:vamGzbGri8IKo20MQncCuljcQ5uAO6kaCeawQPVblAI=
282283
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
283284
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
284285
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

pkg/handler.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/MoonSHRD/p2chat/api"
1111
mapset "github.com/deckarep/golang-set"
12-
peer "github.com/libp2p/go-libp2p-core/peer"
12+
"github.com/libp2p/go-libp2p-core/peer"
1313
pubsub "github.com/libp2p/go-libp2p-pubsub"
1414
)
1515

@@ -50,8 +50,6 @@ func (h *Handler) HandleIncomingMessage(msg pubsub.Message, handleTextMessage fu
5050
switch message.Flag {
5151
// Getting regular message
5252
case api.FLAG_GENERIC_MESSAGE:
53-
// Green console colour: \x1b[32m
54-
// Reset console colour: \x1b[0m
5553
textMessage := TextMessage{
5654
Body: message.Body,
5755
From: addr,
@@ -66,7 +64,7 @@ func (h *Handler) HandleIncomingMessage(msg pubsub.Message, handleTextMessage fu
6664
Body: "",
6765
Flag: api.FLAG_TOPICS_RESPONSE,
6866
},
69-
Topics: h.getTopics(),
67+
Topics: h.GetTopics(),
7068
}
7169
sendData, err := json.Marshal(respond)
7270
if err != nil {
@@ -94,11 +92,22 @@ func (h *Handler) HandleIncomingMessage(msg pubsub.Message, handleTextMessage fu
9492
}
9593

9694
// Get list of topics **this** node is subscribed to
97-
func (h *Handler) getTopics() []string {
95+
func (h *Handler) GetTopics() []string {
9896
topics := h.pb.GetTopics()
9997
return topics
10098
}
10199

100+
// Get list of peers subscribed on specific topic
101+
func (h *Handler) GetPeers(topic string) []peer.ID {
102+
peers := h.pb.ListPeers(topic)
103+
return peers
104+
}
105+
106+
// Blacklists a peer by its id
107+
func (h *Handler) BlacklistPeer(pid peer.ID) {
108+
h.pb.BlacklistPeer(pid)
109+
}
110+
102111
// Requesting topics from **other** peers
103112
func (h *Handler) RequestNetworkTopics(ctx context.Context) {
104113

0 commit comments

Comments
 (0)