Best JavaScript code snippet using devicefarmer-stf
index.js
Source:index.js
1var Promise = require('bluebird')2var logger = require('../../util/logger')3var wire = require('../../wire')4var wireutil = require('../../wire/util')5var wirerouter = require('../../wire/router')6var lifecycle = require('../../util/lifecycle')7var srv = require('../../util/srv')8var zmqutil = require('../../util/zmqutil')9var zmq = require('zeromq/v5-compat')10var crypto = require('crypto')11module.exports = function(options) {12 var log = logger.createLogger('provider')13 var solo = wireutil.makePrivateChannel()14 var push = setup_zmq('push', options.endpoints.push, "Sending output to" , log )15 var sub = setup_zmq('sub' , options.endpoints.sub , "Receiving input from", log )16 17 // Establish always-on channels18 ;[solo].forEach( function( channel ) {19 log.info('Subscribing to permanent channel "%s"', channel)20 sub.subscribe( channel )21 })22 // Track IOS devices; notifications received over ZeroMQ from coordinator23 var connected = {};24 var sock2 = zmq.socket('sub');25 log.info('Receiving devEvent message from coordinator via tcp://127.0.0.1:7294')26 sock2.connect('tcp://127.0.0.1:7294');27 sock2.subscribe('devEvent');28 sock2.on( 'message', function( topic, msg ) {29 var str = msg.toString();30 if( str == "dummy" ) return;31 log.info('Received message from coordinator: %s', str )32 33 var ob = JSON.parse( str );34 if( ob.Type == 'connect' && !connected[ ob.UUID ] ) {35 connected[ ob.UUID ] = 136 log.info('Tracking iOS device')37 log.info(' UUID: ', ob.UUID )38 log.info(' Name: ', ob.Name )39 log.info(' WDAPort:', ob.WDAPort )40 log.info(' VidPort:', ob.VidPort )41 push.send( [ wireutil.global, wireutil.envelope(42 new wire.DeviceIntroductionMessage( 43 ob.UUID, wireutil.toDeviceStatus('device'), new wire.ProviderMessage(solo, options.name)44 )45 ) ] )46 47 var hash = crypto.createHash('sha1')48 hash.update( ob.UUID )49 var chanId = hash.digest('base64')50 sub.subscribe( chanId )51 log.info(' Channel: ', chanId )52 }53 else if( ob.Type == 'present' ) {54 log.info('IOS Present: ', ob.UUID )55 push.send ( [ wireutil.global, wireutil.envelope( new wire.DevicePresentMessage( ob.UUID ) ) ] )56 }57 else if( ob.Type == 'heartbeat' ) {58 log.info('IOS Heartbeat: ', ob.UUID )59 push.send ( [ wireutil.global, wireutil.envelope( new wire.DeviceHeartbeatMessage( ob.UUID ) ) ] )60 }61 else if( ob.Type == 'disconnect' ) {62 delete connected[ ob.UUID ]63 log.info('IOS Device disconnect', ob.UUID )64 push.send( [ wireutil.global, wireutil.envelope( new wire.DeviceAbsentMessage( ob.UUID ) ) ] )65 }66 } );67 68 setInterval( function() {69 var uuids = Object.keys( connected );70 for( var i=0;i<uuids.length;i++ ) {71 var uuid = uuids[i];72 push.send ( [ wireutil.global, wireutil.envelope( new wire.DeviceHeartbeatMessage( uuid ) ) ] )73 console.log("sending heartbeat for", uuid );74 }75 }, 1000 * 20 ); // every 20 seconds ( reaper is every 30 seconds )76 77 sub.on( 'message', wirerouter().on( wire.DeviceRegisteredMessage,78 function( channel, message ) {79 log.info("sub messsage:", message.serial, 'register' )80 }81 ).handler() );82}83function setup_zmq( type, addr, msg, log ) {84 var sub = zmqutil.socket(type)85 Promise.map(addr, function(endpoint) {86 return srv.resolve(endpoint).then(function(records) {87 return srv.attempt(records, function(record) {88 log.info( msg + ' "%s"', record.url)89 sub.connect(record.url)90 return Promise.resolve(true)91 })92 })93 })94 .catch(function(err) {95 log.fatal('Unable to connect to endpoint', err)96 lifecycle.fatal()97 })98 return sub...
Using AI Code Generation
1var wireutil = require('devicefarmer-stf/lib/util/wireutil');2var wire = wireutil.channel('test');3console.log(wire);4var wireutil = require('stf/lib/util/wireutil');5var wire = wireutil.channel('test');6console.log(wire);
Using AI Code Generation
1var wireutil = require('devicefarmer-stf/wire/util')2var wire = require('devicefarmer-stf/wire')3var zmq = require('zmq')4var socket = zmq.socket('req')5var uuid = require('node-uuid')6var log = require('devicefarmer-stf/logger').createLogger('test')7var req = wireutil.envelope(new wire.DeviceKeepAliveMessage({8}))9socket.send(wireutil.pack(req))10socket.on('message', function(data) {11 var envelope = wireutil.unpack(data)12 var message = wireutil.unwrap(envelope)13 log.info('Got response message', message)14})15socket.on('error', function(err) {16 log.error('Socket error', err.message)17})18var wireutil = require('devicefarmer-stf/wire/util')19var wire = require('devicefarmer-stf/wire')20var zmq = require('zmq')21var socket = zmq.socket('rep')22var uuid = require('node-uuid')23var log = require('devicefarmer-stf/logger').createLogger('test')24socket.on('message', function(data) {25 var envelope = wireutil.unpack(data)26 var message = wireutil.unwrap(envelope)27 log.info('Got request message', message)28 var res = wireutil.envelope(new wire.DeviceKeepAliveResponseMessage({29 }))30 socket.send(wireutil.pack(res))31})32socket.on('error', function(err) {33 log.error('Socket error', err.message)34})
Using AI Code Generation
1var wireutil = require('devicefarmer-stf').wireutil;2var wire = wireutil.fromObject({a: 1, b: 2});3console.log(wireutil.toObject(wire));4var wireutil = require('stf').wireutil;5var wire = wireutil.fromObject({a: 1, b: 2});6console.log(wireutil.toObject(wire));
Using AI Code Generation
1var wireutil = require('wireutil');2var wire = wireutil.encodeMessage('foo', 'bar');3var decoded = wireutil.decodeMessage(wire);4console.log(decoded);5var wireutil = require('stf/node_modules/wire/node_modules/wireutil');6var wire = wireutil.encodeMessage('foo', 'bar');7var decoded = wireutil.decodeMessage(wire);8console.log(decoded);9{ topic: 'foo', payload: 'bar' }10var wireutil = require('stf/node_modules/wire/node_modules/wireutil');11var wire = wireutil.encodeMessage('foo', 'bar');12var wireutil = require('devicefarmer-stf/node_modules/wire/node_modules/wireutil');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!