forked from open-horizon/edge-sync-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (191 loc) · 5.49 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/open-horizon/edge-sync-service-client/embedded/client"
)
var (
help *bool
auth = flag.Bool("auth", false, "Specify whether to use authentication.")
orgID = flag.String("org", "", "Specify the organization ID of the destination to receive the file from (optional)")
received = flag.Bool("received", false, "Mark objects as received and NOT consumed.")
verbosity = flag.Int("V", 1, "Verbosity level (0-4)")
destDir = flag.String("dir", "", "Destination Directory to save the files at.")
numReceivers = flag.Int("numReceivers", 16, "Maximum number of files to receive in parallel")
)
const (
none = iota
low
medium
high
)
func init() {
if flag.CommandLine.Lookup("h") == nil {
help = flag.Bool("h", false, "Display usage information.")
}
}
var updatesChannel chan *client.ObjectMetaData
var V int
var goOn bool
var waitGroup sync.WaitGroup
var stopChannel chan bool
func main() {
flag.Parse()
helpFlagValue, err := strconv.ParseBool(flag.CommandLine.Lookup("h").Value.String())
if err == nil && helpFlagValue {
fmt.Fprintln(os.Stderr,
"Usage: embedded-receive-file [-h] [-org orgID] [-c sync-service-config-file] [-auth] [-received] [-dir destDir] [-numReceivers ConcurrencyLevel] [-V verbosityLevel]")
flag.PrintDefaults()
os.Exit(0)
}
V = *verbosity
if len(*orgID) == 0 {
fmt.Fprintln(os.Stderr, "An organization ID must be specified using the -org command line parameter")
os.Exit(1)
}
if *auth {
auth := &CloudSampleAuth{}
auth.AddEdgeNode("sampleKey", "sampleSecret", *orgID, "edge", "node1")
client.SetAuthenticator(auth)
}
// Start the embedded sync client (no parameters are used)
err = os.Setenv("NODE_TYPE", "CSS")
if err != nil && V > none {
fmt.Printf("Failed to set NODE_TYPE to CSS, error: %s\n", err)
}
syncClient := client.NewSyncServiceClient("", "", 0)
syncClient.SetOrgID(*orgID)
goOn = true
stopChannel = make(chan bool)
updatesChannel = make(chan *client.ObjectMetaData, *numReceivers)
for i := 0; i < *numReceivers; i++ {
waitGroup.Add(1)
go fileReceiver(syncClient, updatesChannel)
}
syncClient.StartPollingForUpdates("send-file", 10, updatesChannel)
fmt.Printf("Press the Enter key to exit\n")
buffer := make([]byte, 10)
os.Stdin.Read(buffer)
goOn = false
syncClient.StopPollingForUpdates()
syncClient.Stop(5)
close(stopChannel)
waitGroup.Wait()
}
func fileReceiver(syncClient *client.SyncServiceClient, updatesChannel chan *client.ObjectMetaData) {
defer waitGroup.Done()
for goOn {
select {
case <-stopChannel:
break
case object, ok := <-updatesChannel:
if !ok {
break
}
if object.Deleted {
deleteFile(syncClient, object)
} else {
receiveFile(syncClient, object)
}
}
}
}
func deleteFile(syncClient *client.SyncServiceClient, object *client.ObjectMetaData) {
syncClient.MarkObjectDeleted(object)
}
func receiveFile(syncClient *client.SyncServiceClient, object *client.ObjectMetaData) {
var ok bool
parts := strings.Split(object.ObjectID, "@")
path := parts[0]
if V > medium {
fmt.Printf("Entering receiveFile, file %s\n", path)
}
if object.DestinationDataURI != "" {
if V > low {
fmt.Printf("Received the file %s with DestinationDataURI %s\n", path, object.DestinationDataURI)
}
uri, err := url.Parse(object.DestinationDataURI)
if err != nil || !strings.EqualFold(uri.Scheme, "file") || uri.Host != "" {
if V > none {
fmt.Printf(" Invalid DestinationDataURI: %s, err=%v, scheme=%s, host=%s\n", object.DestinationDataURI, err, uri.Scheme, uri.Host)
}
return
}
file, err := os.Open(uri.Path)
if err != nil {
if V > none {
fmt.Printf("Couldn't open the file at DestinationDataURI %s. Error: %s\n", uri.Path, err)
}
return
}
err = file.Close()
if err != nil && V > none {
fmt.Printf("Failed to close the file at DestinationDataURI %s. Error: %s\n", uri.Path, err)
}
ok = true
} else {
if len(*destDir) > 0 && !filepath.IsAbs(path) {
path = filepath.Join(*destDir, path)
}
fDir := filepath.Dir(path)
file, err := ioutil.TempFile(fDir, "rcvTmp")
if err != nil {
if V > none {
fmt.Printf("Failed to open the file %s for writing. Error: %s\n", path, err)
}
return
}
tmpFile := file.Name()
if syncClient.FetchObjectData(object, file) {
err = file.Close()
if err != nil && V > none {
fmt.Printf("Failed to close the file %s. Error: %s\n", tmpFile, err)
}
err = os.Rename(tmpFile, path)
if err != nil {
err = os.Remove(tmpFile)
if err != nil && V > none {
fmt.Printf("Failed to remove the file %s. Error: %s\n", tmpFile, err)
}
if V > none {
fmt.Printf(" Failed to rename temporary file %s to target file %s\n", tmpFile, path)
}
return
}
ok = true
} else {
err = file.Close()
if err != nil && V > none {
fmt.Printf("Failed to close the file %s. Error: %s\n", file.Name(), err)
}
err = os.Remove(tmpFile)
if err != nil && V > none {
fmt.Printf("Failed to remove the file %s. Error: %s\n", tmpFile, err)
}
if V > none {
fmt.Printf(" FetchObjectData failed, file %s\n", path)
}
}
}
if ok {
if *received {
if V > low {
fmt.Printf(" Calling MarkObjectReceived for file %s\n", path)
}
syncClient.MarkObjectReceived(object)
} else {
if V > low {
fmt.Printf(" Calling MarkObjectConsumed for file %s\n", path)
}
syncClient.MarkObjectConsumed(object)
}
fmt.Printf("File %s received\n", path)
}
}