This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
hostsmanager.go
121 lines (107 loc) · 2.78 KB
/
hostsmanager.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
package main
import (
"errors"
"strings"
"github.com/openatx/atx-server/proto"
)
func deviceQueryToUdid(query string) (udid string, err error) {
if strings.HasPrefix(query, "ip:") {
infos := db.DeviceFindAll(proto.DeviceInfo{IP: query[3:], Present: newBool(true)})
return extractUdidFromInfos(infos)
}
return query, nil
}
func extractUdidFromInfos(infos []proto.DeviceInfo) (udid string, err error) {
if len(infos) == 0 {
return "", errors.New("not found")
}
if len(infos) > 1 {
return "", errors.New("too many matches")
}
return infos[0].Udid, nil
}
// // TODO: need to delete bellow
// type HostsManager struct {
// maps map[string]*proto.DeviceInfo
// mu sync.RWMutex
// }
// func NewHostsManager() *HostsManager {
// return &HostsManager{
// maps: make(map[string]*proto.DeviceInfo),
// }
// }
// func (t *HostsManager) Lookup(query string) *proto.DeviceInfo {
// if strings.HasPrefix(query, "ip:") {
// return t.FromIP(query[3:])
// }
// return t.FromUdid(query)
// }
// // A return value of nil indicates not found
// func (t *HostsManager) FromIP(ip string) *proto.DeviceInfo {
// t.mu.Lock()
// defer t.mu.Unlock()
// for _, info := range t.maps {
// if info.IP == ip {
// return info
// }
// }
// return nil
// }
// // A return value of nil indicates not found
// func (t *HostsManager) FromUdid(udid string) *proto.DeviceInfo {
// t.mu.Lock()
// defer t.mu.Unlock()
// return t.maps[udid]
// }
// func (t *HostsManager) AddFromDeviceInfo(devInfo *proto.DeviceInfo) {
// t.mu.Lock()
// defer t.mu.Unlock()
// udid := devInfo.Udid
// if info, ok := t.maps[udid]; ok {
// info.IP = devInfo.IP
// info.ConnectionCount++
// } else {
// devInfo.ConnectionCount = 1
// t.maps[udid] = devInfo
// }
// }
// func (t *HostsManager) Remove(udid string) {
// t.mu.Lock()
// defer t.mu.Unlock()
// if info, ok := t.maps[udid]; ok {
// info.ConnectionCount--
// if info.ConnectionCount <= 0 {
// delete(t.maps, udid)
// }
// }
// }
// func (t *HostsManager) Acquire(query string) error {
// info := t.Lookup(query)
// if info == nil {
// return errors.New("device not found")
// }
// if info.Reserved != "" {
// return errors.New("device already reserved")
// }
// info.Reserved = "hzsunshx"
// return nil
// }
// func (t *HostsManager) Release(query string) error {
// info := t.Lookup(query)
// if info == nil {
// return errors.New("device not found")
// }
// info.Reserved = ""
// return nil
// }
// func (t *HostsManager) Random() (devInfo *proto.DeviceInfo, err error) {
// t.mu.Lock()
// defer t.mu.Unlock()
// for _, info := range t.maps {
// if info.Ready != nil && *info.Ready == true && info.Reserved == "" {
// info.Reserved = "random"
// return info, nil
// }
// }
// return nil, errors.New("no devices avaliable")
// }