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" ;
67
@@ -10,39 +11,32 @@ export class IOSDevice implements Mobile.IiOSDevice {
1011 public deviceInfo : Mobile . IDeviceInfo ;
1112 private socket : net . Socket ;
1213
13- // private static sockets: { [id: string]: net.Socket; } = {};
14-
15- // get socket(): net.Socket {
16- // return IOSDevice.sockets[this.deviceInfo.identifier];
17- // }
18- // set socket(newSocket: net.Socket) {
19- // IOSDevice.sockets[this.deviceInfo.identifier] = newSocket;
20- // }
21-
2214 private _deviceLogHandler : ( ...args : any [ ] ) => void ;
2315
2416 constructor ( private deviceActionInfo : IOSDeviceLib . IDeviceActionInfo ,
17+ private $errors : IErrors ,
2518 private $injector : IInjector ,
19+ private $iOSDebuggerPortService : IIOSDebuggerPortService ,
20+ private $iOSSocketRequestExecutor : IiOSSocketRequestExecutor ,
2621 private $processService : IProcessService ,
2722 private $deviceLogProvider : Mobile . IDeviceLogProvider ,
2823 private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ,
2924 private $iOSDeviceProductNameMapper : Mobile . IiOSDeviceProductNameMapper ,
3025 private $iosDeviceOperations : IIOSDeviceOperations ,
31- private $logger : ILogger ,
3226 private $mobileHelper : Mobile . IMobileHelper ) {
3327
3428 this . applicationManager = this . $injector . resolve ( applicationManagerPath . IOSApplicationManager , { device : this , devicePointer : this . deviceActionInfo } ) ;
3529 this . fileSystem = this . $injector . resolve ( fileSystemPath . IOSDeviceFileSystem , { device : this , devicePointer : this . deviceActionInfo } ) ;
3630
3731 const productType = deviceActionInfo . productType ;
3832 const isTablet = this . $mobileHelper . isiOSTablet ( productType ) ;
39- const deviceStatus = deviceActionInfo . status || constants . UNREACHABLE_STATUS ;
33+ const deviceStatus = deviceActionInfo . status || commonConstants . UNREACHABLE_STATUS ;
4034 this . deviceInfo = {
4135 identifier : deviceActionInfo . deviceId ,
4236 vendor : "Apple" ,
4337 platform : this . $devicePlatformsConstants . iOS ,
4438 status : deviceStatus ,
45- errorHelp : deviceStatus === constants . UNREACHABLE_STATUS ? `Device ${ deviceActionInfo . deviceId } is ${ constants . UNREACHABLE_STATUS } ` : null ,
39+ errorHelp : deviceStatus === commonConstants . UNREACHABLE_STATUS ? `Device ${ deviceActionInfo . deviceId } is ${ commonConstants . UNREACHABLE_STATUS } ` : null ,
4640 type : "Device" ,
4741 isTablet : isTablet ,
4842 displayName : this . $iOSDeviceProductNameMapper . resolveProductName ( deviceActionInfo . deviceName ) || deviceActionInfo . deviceName ,
@@ -69,31 +63,45 @@ export class IOSDevice implements Mobile.IiOSDevice {
6963
7064 @cache ( )
7165 public async openDeviceLogStream ( ) : Promise < void > {
72- if ( this . deviceInfo . status !== constants . UNREACHABLE_STATUS ) {
66+ if ( this . deviceInfo . status !== commonConstants . UNREACHABLE_STATUS ) {
7367 this . _deviceLogHandler = this . actionOnDeviceLog . bind ( this ) ;
74- this . $iosDeviceOperations . on ( constants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
68+ this . $iosDeviceOperations . on ( commonConstants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
7569 this . $iosDeviceOperations . startDeviceLog ( this . deviceInfo . identifier ) ;
7670 }
7771 }
7872
7973 public detach ( ) : void {
8074 if ( this . _deviceLogHandler ) {
81- this . $iosDeviceOperations . removeListener ( constants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
75+ this . $iosDeviceOperations . removeListener ( commonConstants . DEVICE_LOG_EVENT_NAME , this . _deviceLogHandler ) ;
8276 }
8377 }
8478
85- // This function works only on OSX
86- public async connectToPort ( port : number ) : Promise < net . Socket > {
87- console . log ( "connectToPort" ) ;
79+ public async getLiveSyncSocket ( appId : string , projectDir : string ) : Promise < net . Socket > {
80+ return this . getSocket ( appId , projectDir ) ;
81+ }
82+
83+ public async getDebugSocket ( appId : string , projectDir : string ) : Promise < net . Socket > {
84+ return this . getSocket ( appId , projectDir ) ;
85+ }
86+
87+ private async getSocket ( appId : string , projectDir : string ) : Promise < net . Socket > {
88+ if ( this . socket ) {
89+ return this . socket ;
90+ }
91+
92+ await this . $iOSSocketRequestExecutor . executeAttachRequest ( this , constants . AWAIT_NOTIFICATION_TIMEOUT_SECONDS , appId ) ;
93+ const port = await this . $iOSDebuggerPortService . getPort ( { projectDir, deviceId : this . deviceInfo . identifier , appId } ) ;
94+ if ( ! port ) {
95+ this . $errors . fail ( "NativeScript debugger was not able to get inspector socket port." ) ;
96+ }
97+
8898 const deviceId = this . deviceInfo . identifier ;
8999 const deviceResponse = _ . first ( ( await this . $iosDeviceOperations . connectToPort ( [ { deviceId : deviceId , port : port } ] ) ) [ deviceId ] ) ;
90100
91- const _socket = new net . Socket ( ) ;
92- _socket . connect ( deviceResponse . port , deviceResponse . host ) ;
93- this . socket = _socket ;
94- _socket . on ( "close" , ( ) => {
101+ this . socket = new net . Socket ( ) ;
102+ this . socket . connect ( deviceResponse . port , deviceResponse . host ) ;
103+ this . socket . on ( "close" , ( ) => {
95104 this . socket = null ;
96- this . $logger . info ( "iOS Device socket closed!" ) ;
97105 } ) ;
98106
99107 this . $processService . attachToProcessExitSignals ( this , this . destroySocket ) ;
0 commit comments