|
1 | | -# p2chat |
| 1 | +# P2Chat |
| 2 | +P2Chat - is a core local messenger library, which based on Libp2p stack. |
2 | 3 |
|
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** |
7 | 5 |
|
8 | 6 | It supports next features: |
9 | | -- devices autodiscovery by `Rendez-vous string` |
| 7 | +- devices autodiscovery by `Rendezvous string` |
10 | 8 | - topic list exchanging between peers |
11 | 9 | - 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]* |
13 | 11 | - crossplatform |
14 | 12 |
|
15 | 13 |
|
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 | | - |
144 | 14 | ## Building |
| 15 | +Require go version >=1.12 , so make sure your `go version` is okay |
| 16 | + |
145 | 17 | ```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 |
148 | 23 | ``` |
| 24 | +Builded binary will be in `./cmd/` |
0 commit comments