forked from dryark/ios_remote_provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_tracker.go
159 lines (137 loc) · 4.03 KB
/
device_tracker.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
package main
import (
"fmt"
"strconv"
"sync"
log "github.com/sirupsen/logrus"
)
type Event struct {
action int
uuid string
}
type DeviceTracker struct {
Config *Config
DevMap map [string] *Device
localPorts []int
process map[string] *GenericProc
lock *sync.Mutex
cf *ControlFloor
bridge BridgeRoot
}
func NewDeviceTracker( config *Config, detect bool ) (*DeviceTracker) {
var cf *ControlFloor
if detect {
cf = NewControlFloor( config )
}
self := &DeviceTracker{
process: make( map[string] *GenericProc ),
lock: &sync.Mutex{},
DevMap: make( map [string] *Device ),
//EventCh: make( chan Event ),
Config: config,
localPorts: []int{
8102, 8103, 8104, 8105, 8106, 8107, 8108, 8109, 8110, 8111, 8112, 8113, 8114, 8115, 8116,
},
cf: cf,
}
self.bridge = NewIIFBridge(
func( dev BridgeDev ) ProcTracker { return self.onDeviceConnect1( dev ) },
func( dev BridgeDev ) { self.onDeviceDisconnect1( dev ) },
config.iosIfPath,
self,
detect,
)
if detect {
cf.DevTracker = self
}
return self
}
func (self *DeviceTracker) startProc( proc *GenericProc ) {
self.lock.Lock()
self.process[ proc.name ] = proc
self.lock.Unlock()
}
func ( self *DeviceTracker ) stopProc( procName string ) {
self.lock.Lock()
delete( self.process, procName )
self.lock.Unlock()
}
func (self *DeviceTracker) getPort() (int) {
var port int
self.lock.Lock()
port, self.localPorts = self.localPorts[0], self.localPorts[1:]
self.lock.Unlock()
return port
}
func (self *DeviceTracker) getDevice( udid string ) (*Device) {
return self.DevMap[ udid ]
}
func (self *DeviceTracker) onDeviceConnect1( bdev BridgeDev ) *Device {
udid := bdev.getUdid()
fmt.Printf("udid: %s\n", udid)
//dev := self.DevMap[ udid ]
//devConf := self.Config.devs[ udid ]
mgInfo := bdev.gestalt( []string{
"main-screen-width",
"main-screen-height",
"main-screen-scale",
} )
width, _ := strconv.Atoi( mgInfo["main-screen-width"] )
height, _ := strconv.Atoi( mgInfo["main-screen-height"] )
scale, _ := strconv.Atoi( mgInfo["main-screen-scale"] )
self.cf.notifyDeviceExists( udid, width, height, width/scale, height/scale )
dev := self.onDeviceConnect( udid, bdev )
self.cf.notifyDeviceInfo( dev )
dev.startEventLoop()
//dev.openBackupStream()
bdev.setProcTracker( self )
dev.startProcs()
return dev
}
func (self *DeviceTracker) onDeviceDisconnect1( bdev BridgeDev ) {
udid := bdev.getUdid()
dev := self.DevMap[ udid ]
self.onDeviceDisconnect( dev )
dev.stopEventLoop()
dev.endProcs()
}
func (self *DeviceTracker) shutdown() {
for _,proc := range self.process {
log.WithFields( log.Fields{
"type": "shutdown_proc",
"proc": proc.name,
"pid": proc.pid,
} ).Info("Shutdown proc")
go func() { proc.Kill() }()
}
for _,dev := range self.DevMap {
self.cf.notifyProvisionStopped( dev.udid )
}
for _,dev := range self.DevMap {
log.WithFields( log.Fields{
"type": "shutdown_device",
"uuid": censorUuid( dev.udid ),
} ).Info("Shutdown device")
dev.shutdown()
}
}
func (self *DeviceTracker) onDeviceConnect( uuid string, bdev BridgeDev ) (*Device){
dev := self.DevMap[ uuid ]
if dev != nil {
dev.connected = true
return dev
}
dev = NewDevice( self.Config, self, uuid, bdev )
devInfo := getAllDeviceInfo( bdev )
log.WithFields( log.Fields{
"type": "devInfo",
"uuid": censorUuid( uuid ),
"info": devInfo,
} ).Info("Device Info")
dev.info = devInfo
self.DevMap[ uuid ] = dev
return dev
}
func (self *DeviceTracker) onDeviceDisconnect( dev *Device ) {
dev.connected = false
}