This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.go
171 lines (144 loc) · 4.58 KB
/
listen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright © 2020 Will Rowe <w.p.m.rowe@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/fsnotify/fsnotify"
ipfs "github.com/ipfs/go-ipfs-api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/will-rowe/scribe/src/backend"
"github.com/will-rowe/scribe/src/config"
)
// listenCmd represents the listen command
var listenCmd = &cobra.Command{
Use: "listen",
Short: "Listen for project updates that are being pushed to the network",
Long: `Listen for project updates that are being pushed to the network.
This command uses the pubsub protocol, which is currently an experimental IPFS
feature.`,
Run: func(cmd *cobra.Command, args []string) {
runListen()
},
}
// init the subcommand
func init() {
rootCmd.AddCommand(listenCmd)
}
// runListen is the main block for the listen subcommand
func runListen() {
// run the config checker to make sure we've got everything
if err := config.CheckConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// start the subcommand
log.Info("------------SCRIBE------------")
log.Info("starting the listen subcommand...")
log.Infof("\tconfig file: %v", viper.ConfigFileUsed())
log.Info("setting config watcher...")
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Infof("config file changed: %v", e.Name)
})
// configure the daemon
log.Info("configuring IPFS daemon...")
config, err := config.DumpConfig2Mem()
if err != nil {
log.Fatal(err)
}
if err := backend.ConfigureDaemon(config); err != nil {
log.Fatal(err)
}
log.Infof("\tupdated IPFS daemon using the scribe config")
// check if the IPFS daemon is running (launch if not)
tmpShell := ipfs.NewShell(backend.GetAPI())
if !tmpShell.IsUp() {
log.Infof("\tlaunching daemon")
if err := backend.LaunchDaemon(config); err != nil {
log.Fatal(err)
}
}
log.Infof("\tdaemon is running")
// init the node
log.Info("initialising the node...")
node, err := backend.InitNode(backend.GetAPI())
if err != nil {
log.Fatal(err)
}
nodeIdentity, err := node.Identity()
if err != nil {
log.Fatal(err)
}
log.Infof("\tAPI server listening on: %s", backend.GetAPI())
log.Info("\tswarm and gateway info to go here...")
log.Infof("\tnode identity: %v", nodeIdentity.ID)
// create context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// subscribe to the requested project
log.Info("subscribing the node...")
if err := node.Subscribe(ctx, viper.GetString("project")); err != nil {
log.Fatal(err)
}
defer func() {
if err := node.Unsubscribe(); err != nil {
log.Fatal(err)
}
}()
log.Infof("\tlistening for: %v", viper.GetString("project"))
// setup the pubsub listener
msgChan := make(chan *ipfs.Message)
errChan := make(chan error, 1)
sigChan := make(chan struct{})
go node.Listen(msgChan, errChan, sigChan)
// catch the os interupt for graceful close down
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
// wait for interupt
<-c
log.Info("interrupt received - shutting down")
// quit the PubSub listener
close(sigChan)
os.Exit(0)
}()
// process incoming messages
for {
select {
// collect any messages
case msg := <-msgChan:
// check the message over
log.Infof("\tmessage received from: %v", msg.From.Pretty())
log.Infof("\tcontent: %v", msg.Data)
// handle it
//var doc map[string]interface{}
//json.Unmarshal([]byte(s), &doc)
//context, hasContext := doc["@context"]
// collect any errors from the PubSub
case err := <-errChan:
log.Warn(err)
}
}
}