11import * as applicationManagerPath from "./ios-application-manager" ;
22import * as fileSystemPath from "./ios-device-file-system" ;
3- import * as constants from "../../../constants" ;
3+ import * as commonConstants from "../../../constants" ;
4+ import * as constants from "../../../../constants" ;
45import * as net from "net" ;
56import { cache } from "../../../decorators" ;
7+ import * as helpers from "../../../../common/helpers" ;
8+ import { IOSDeviceBase } from "../ios-device-base" ;
69
7- export class IOSDevice implements Mobile . IiOSDevice {
10+ export class IOSDevice extends IOSDeviceBase {
811 public applicationManager : Mobile . IDeviceApplicationManager ;
912 public fileSystem : Mobile . IDeviceFileSystem ;
1013 public deviceInfo : Mobile . IDeviceInfo ;
11-
12- private _socket : net . Socket ;
1314 private _deviceLogHandler : ( ...args : any [ ] ) => void ;
1415
1516 constructor ( private deviceActionInfo : IOSDeviceLib . IDeviceActionInfo ,
17+ protected $errors : IErrors ,
1618 private $injector : IInjector ,
17- private $processService : IProcessService ,
19+ protected $iOSDebuggerPortService : IIOSDebuggerPortService ,
20+ private $iOSSocketRequestExecutor : IiOSSocketRequestExecutor ,
21+ protected $processService : IProcessService ,
1822 private $deviceLogProvider : Mobile . IDeviceLogProvider ,
1923 private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ,
2024 private $iOSDeviceProductNameMapper : Mobile . IiOSDeviceProductNameMapper ,
2125 private $iosDeviceOperations : IIOSDeviceOperations ,
2226 private $mobileHelper : Mobile . IMobileHelper ) {
23-
27+ super ( ) ;
2428 this . applicationManager = this . $injector . resolve ( applicationManagerPath . IOSApplicationManager , { device : this , devicePointer : this . deviceActionInfo } ) ;
2529 this . fileSystem = this . $injector . resolve ( fileSystemPath . IOSDeviceFileSystem , { device : this , devicePointer : this . deviceActionInfo } ) ;
26-
2730 const productType = deviceActionInfo . productType ;
2831 const isTablet = this . $mobileHelper . isiOSTablet ( productType ) ;
29- const deviceStatus = deviceActionInfo . status || constants . UNREACHABLE_STATUS ;
32+ const deviceStatus = deviceActionInfo . status || commonConstants . UNREACHABLE_STATUS ;
3033 this . deviceInfo = {
3134 identifier : deviceActionInfo . deviceId ,
3235 vendor : "Apple" ,
3336 platform : this . $devicePlatformsConstants . iOS ,
3437 status : deviceStatus ,
35- errorHelp : deviceStatus === constants . UNREACHABLE_STATUS ? `Device ${ deviceActionInfo . deviceId } is ${ constants . UNREACHABLE_STATUS } ` : null ,
38+ errorHelp : deviceStatus === commonConstants . UNREACHABLE_STATUS ? `Device ${ deviceActionInfo . deviceId } is ${ commonConstants . UNREACHABLE_STATUS } ` : null ,
3639 type : "Device" ,
3740 isTablet : isTablet ,
3841 displayName : this . $iOSDeviceProductNameMapper . resolveProductName ( deviceActionInfo . deviceName ) || deviceActionInfo . deviceName ,
@@ -47,8 +50,29 @@ export class IOSDevice implements Mobile.IiOSDevice {
4750 return false ;
4851 }
4952
50- public getApplicationInfo ( applicationIdentifier : string ) : Promise < Mobile . IApplicationInfo > {
51- return this . applicationManager . getApplicationInfo ( applicationIdentifier ) ;
53+ @cache ( )
54+ public async openDeviceLogStream ( ) : Promise < void > {
55+ if ( this . deviceInfo . status !== commonConstants . UNREACHABLE_STATUS ) {
56+ this . _deviceLogHandler = this . actionOnDeviceLog . bind ( this ) ;
57+ this . $iosDeviceOperations . on ( commonConstants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
58+ this . $iosDeviceOperations . startDeviceLog ( this . deviceInfo . identifier ) ;
59+ }
60+ }
61+
62+ protected async getSocketCore ( appId : string ) : Promise < net . Socket > {
63+ await this . $iOSSocketRequestExecutor . executeAttachRequest ( this , constants . AWAIT_NOTIFICATION_TIMEOUT_SECONDS , appId ) ;
64+ const port = await this . getDebuggerPort ( appId ) ;
65+ const deviceId = this . deviceInfo . identifier ;
66+ const socket = await helpers . connectEventuallyUntilTimeout (
67+ async ( ) => {
68+ const deviceResponse = _ . first ( ( await this . $iosDeviceOperations . connectToPort ( [ { deviceId : deviceId , port : port } ] ) ) [ deviceId ] ) ;
69+ const _socket = new net . Socket ( ) ;
70+ _socket . connect ( deviceResponse . port , deviceResponse . host ) ;
71+ return _socket ;
72+ } ,
73+ commonConstants . SOCKET_CONNECTION_TIMEOUT_MS ) ;
74+
75+ return socket ;
5276 }
5377
5478 private actionOnDeviceLog ( response : IOSDeviceLib . IDeviceLogData ) : void {
@@ -57,34 +81,12 @@ export class IOSDevice implements Mobile.IiOSDevice {
5781 }
5882 }
5983
60- @cache ( )
61- public async openDeviceLogStream ( ) : Promise < void > {
62- if ( this . deviceInfo . status !== constants . UNREACHABLE_STATUS ) {
63- this . _deviceLogHandler = this . actionOnDeviceLog . bind ( this ) ;
64- this . $iosDeviceOperations . on ( constants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
65- this . $iosDeviceOperations . startDeviceLog ( this . deviceInfo . identifier ) ;
66- }
67- }
68-
6984 public detach ( ) : void {
7085 if ( this . _deviceLogHandler ) {
71- this . $iosDeviceOperations . removeListener ( constants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
86+ this . $iosDeviceOperations . removeListener ( commonConstants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
7287 }
7388 }
7489
75- // This function works only on OSX
76- public async connectToPort ( port : number ) : Promise < net . Socket > {
77- const deviceId = this . deviceInfo . identifier ;
78- const deviceResponse = _ . first ( ( await this . $iosDeviceOperations . connectToPort ( [ { deviceId : deviceId , port : port } ] ) ) [ deviceId ] ) ;
79-
80- const socket = new net . Socket ( ) ;
81- socket . connect ( deviceResponse . port , deviceResponse . host ) ;
82- this . _socket = socket ;
83-
84- this . $processService . attachToProcessExitSignals ( this , this . destroySocket ) ;
85- return this . _socket ;
86- }
87-
8890 private getActiveArchitecture ( productType : string ) : string {
8991 let activeArchitecture = "" ;
9092 if ( productType ) {
@@ -106,13 +108,6 @@ export class IOSDevice implements Mobile.IiOSDevice {
106108
107109 return activeArchitecture ;
108110 }
109-
110- private destroySocket ( ) {
111- if ( this . _socket ) {
112- this . _socket . destroy ( ) ;
113- this . _socket = null ;
114- }
115- }
116111}
117112
118113$injector . register ( "iOSDevice" , IOSDevice ) ;
0 commit comments