-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
140 lines (106 loc) · 2.7 KB
/
agent.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
//go:build agent
// +build agent
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"google.golang.org/grpc"
pb "mediagui/mediaagent"
)
// var Version string
// Version - plugin version
var (
Version string
port = flag.Int("port", 7624, "The server port")
)
type mediaAgentServer struct {
host string
pb.UnimplementedMediaAgentServer
}
// Scan -
func (m *mediaAgentServer) Scan(_ context.Context, req *pb.ScanReq) (*pb.ScanRsp, error) {
log.Printf("Received Agent.Scan request: %v\n", req)
rep := &pb.ScanRsp{
Filenames: make([]string, 0),
}
for _, folder := range req.Folders {
list := m.walk(folder, req.Mask)
// if err != nil {
// mlog.Warning("Unable to scan folder (%s): %s", folder, err)
// }
// files = append(files, list...)
rep.Filenames = append(rep.Filenames, list...)
}
// for _, f := range files {
// rsp.Filenames = append(rsp.Filenames, f)
// }
log.Printf("Sent back %d files\n", len(rep.Filenames))
return rep, nil
}
func (m *mediaAgentServer) walk(folder, mask string) []string {
if folder[len(folder)-1] != '/' {
folder = folder + "/"
}
var files []string
filepath.Walk(folder, func(path string, f os.FileInfo, err error) error {
if err != nil {
log.Printf("Agent.Scan.walk: %s (%s) - [%+v]\n", err, path, f)
}
if f.IsDir() {
return nil
}
if !strings.Contains(mask, strings.ToLower(filepath.Ext(path))) {
// mlog.Info("[%s] excluding %s", filepath.Ext(path), path)
return nil
}
// log.Infof("file=%s", path)
files = append(files, m.host+":"+path)
return nil
})
return files
}
// Exists -
func (m *mediaAgentServer) Exists(_ context.Context, req *pb.ExistsReq) (*pb.ExistsRsp, error) {
log.Printf("Received Agent.Exists request: %d items", len(req.Items))
rep := &pb.ExistsRsp{
Items: make([]*pb.Item, 0),
}
for _, item := range req.Items {
exists := true
if _, err := os.Stat(item.Location); err != nil {
exists = !os.IsNotExist(err)
}
if !exists {
log.Printf("Location %s doesn't exist\n", item.Location)
rep.Items = append(rep.Items, item)
}
}
return rep, nil
}
func newServer() *mediaAgentServer {
host, err := os.Hostname()
if err != nil {
log.Fatalf("Unable to obtain hostname: %s", err)
}
return &mediaAgentServer{host: host}
}
func main() {
flag.Parse()
address := fmt.Sprintf("0.0.0.0:%d", *port)
lis, err := net.Listen("tcp", address)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
pb.RegisterMediaAgentServer(grpcServer, newServer())
log.Printf("started mediaagent v%s listening on %s", Version, address)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
}
}