How to use connectedAgents method in Best

Best JavaScript code snippet using best

AgentListener.js

Source:AgentListener.js Github

copy

Full Screen

1'use strict';2module.exports = exports = AgentListener;3/**4 * Static Constructor5 *6 * This function is here to mimic koneas.go's logging. 7 *8 */9function AgentListener(Handler)10{11 var self = this;12 self.connectedAgents = {};13 self.server = {};14 self.NewAgentListener(Handler);15};16/**17 * Constructor 18 *19 * An AgentListener is a server that handles all incoming tls connections from konea clients.20 * When an agent successfully establishes a tls connection, koneas handles an initial GET (should be changed to CONNECT) request,21 * and responds with a 200, which causes konea to become a spdy server.22 * Koneas then hijacks the tls connection, and establishes a reverse spdy tunnel, acting as the spdy client.23 * The point of this tunnel, is to forward requests from the external API to konea.24 *25 */26AgentListener.prototype.NewAgentListener = function NewAgentListener(AgentHandler)27{28 var self = this;29 30 self.AgentHandler = AgentHandler;31 32 // Create the tls server instance.33 self.server = GLOBAL.tls.createServer(GLOBAL.config.tls.server);34 // Start listening for tls connect and error events.35 self.BindEventListeners();36 // Start the tls listener.37 self.server.listen(GLOBAL.config.tls.port, GLOBAL.config.tls.host, self.Run.bind(self));38};39/**40 * Helper function that gets called by the constructor. It is where you put global event handler for AgentListener.41 *42 */43AgentListener.prototype.BindEventListeners = function BindEventListeners()44{45 var self = this;46 // Bind a handler to the secureConnection event, which is emitted once the tls-handshake completes successfully.47 self.server.on('secureConnection', self.HandleAgentConnect.bind(self));48 // Bind a handler to the clientError event, which is emitted when a client fails to connect.49 self.server.on('clientError', self.HandleClientError.bind(self));50 // TODO: I'm not sure we need this, but I'm leaving it here just in case.51 self.server.on('error', self.HandleClientError.bind(self));52};53/**54 * This is here to mimic koneas.go's logging.55 *56 */57AgentListener.prototype.Run = function Run()58{59 var self = this;60 var socket_info = self.server.address();61 GLOBAL.log.info(GLOBAL.log.StartAgentListenerMessage, { addr: socket_info.address+':'+socket_info.port });62};63/**64 * Returns a reference to an AgentHandler, if the kuid is found.65 *66 */67AgentListener.prototype.FindAgent = function FindAgent(kuid)68{69 return this.connectedAgents[kuid];70};71/**72 * Returns the number of connections koneas has connected.73 *74 */75AgentListener.prototype.ConnectionCount = function ConnectionCount()76{77 return Object.keys(this.connectedAgents).length;78};79/**80 * Shuts down koneas and sends a disconnect to all of the konea connections.81 *82 */83AgentListener.prototype.Stop = function Stop(kuid)84{85 var self = this;86 if(self.server.close) {87 self.server.close();88 }89 for(kuid in self.connectedAgents) {90 self.connectedAgents[kuid].EmitCloseEvent();91 }92};93/**94 * Converts the tls connection to a spdy connection.95 *96 */97AgentListener.prototype.RunSpdyServer = function RunSpdyServer(socket, options, config)98{99 var self = this;100 101 function SpecialAgent(options)102 { 103 GLOBAL.http.Agent.call(this, options);104 this.options = options;105 this.createConnection = function createConnection(options)106 {107 function read() {108 var b = socket.read();109 if(b === null) {110 socket.once('readable', read);111 }112 }113 if(socket.read) {114 read();115 }116 return socket;117 };118 };119 GLOBAL.util.inherits(SpecialAgent, GLOBAL.http.Agent);120 return self.AgentHandler(GLOBAL.spdy.createAgent(SpecialAgent, options), config);121};122/**123 * Stores konea connection in local collection.124 * Also handles duplicate detection.125 * NOTE: Currently, we just reject duplicate connections, but once AMP is dead, we will do full duplicate detection. **126 *127 * Lowercase to mimic koneas.go's logging.128 *129 */130AgentListener.prototype.RegisterAgentHandler = function RegisterAgentHandler(agentHandler)131{132 var self = this;133 GLOBAL.log.debug(GLOBAL.log.AgentRegisteringMessage, { addr: agentHandler.Addr(), kuid: agentHandler.Kuid() });134 // If this kuid is already in our collection.135 var agent = {};136 agent.kuid = agentHandler.Kuid();137 agent.addr = agentHandler.Addr();138 agent.version = agentHandler.Version(); 139 140 function AcceptNewAgent() {141 self.connectedAgents[agent.kuid] = agentHandler;142 agentHandler.Start();143 }144 145 function RejectNewAgent() {146 agentHandler.Stop(true); 147 }148 self.CheckForDuplicateAgent(149 agent,150 AcceptNewAgent,151 RejectNewAgent152 );153};154AgentListener.prototype.CheckForDuplicateAgent = function CheckForDuplicateAgent(agent, is_unique_callback, is_dup_callback)155{156 var self = this;157 if(self.connectedAgents[agent.kuid]) {158 agent.prev = self.connectedAgents[agent.kuid].Addr();159 // And the original agent responds to a ping.160 self.connectedAgents[agent.kuid].IsLive(function HandleDuplicateAgent() {161 // Then it's a real duplicate.162 GLOBAL.log.info(GLOBAL.log.DuplicateKUIDMessage, agent);163 /* 164 var kuid = agentHandler.Kuid(); 165 agentHandler.RegenerateKuid(); 166 agentHandler.on('regenerateKuidSuccess', function HandleDuplicateAgent() { 167 GLOBAL.log.info('KUID was successfully regenerated.', { old: agent.kuid, new: agentHandler.Kuid() }); 168 self.connectedAgents[agentHandler.Kuid()] = agentHandler; 169 });170 */171 is_dup_callback();172 });173 self.connectedAgents[agent.kuid].on('error', function HandleTimedOutAgent() {174 // The close event might have happened already, so if it did, don't call Stop().175 if(self.connectedAgents[agent.kuid] !== undefined) {176 // Call Stop(true) to suppress triggering the AgentHandler 'close' event so we don't end up in a race with the event handler.177 self.connectedAgents[agennt.kuid].Stop(true);178 }179 GLOBAL.log.info(GLOBAL.log.AgentReconnectMessage, agent);180 is_unique_callback();181 });182 }183 // This is a new kuid, so add the agent to our collection. 184 else {185 is_unique_callback();186 }187};188/**189 * Handles tls connection errors.190 *191 * This is a connection error, so we shouldn't have established the tls connection yet.192 * Destroy the connection and log the fact.193 *194 */195AgentListener.prototype.HandleClientError = function HandleClientError(error, pair)196{197 var self = this;198 if(pair) {199 GLOBAL.log.warn('Client Connection Error Occurred', { error: error, addr: pair.cleartext.remoteAddress+':'+pair.cleartext.remotePort });200 pair.destroy();201 }202 else {203 GLOBAL.log.warn('Client Connection Error Occurred', { error: error, addr: "undefined" });204 }205};206/**207 * Handles http (spdy) socket disconnects.208 *209 * Either konea disconnected, had an error, or we called AgentHandler.Stop().210 * Remove the agent from the collection.211 *212 */213AgentListener.prototype.HandleAgentDisconnect = function HandleAgentDisconnect(agentHandler)214{215 delete this.connectedAgents[agentHandler.Kuid()];216};217/**218 * Handles tls connect events.219 *220 * Sets up the data event listener, which receives the inital GET / from konea.221 * In response, it sends a 200 to konea which causes konea to convert to a spdy server.222 * Koneas then "hijacks" the tls connetion and convert it into a spdy client. 223 * Also requests kuid from agent and sets up handlers.224 *225 */226AgentListener.prototype.HandleAgentConnect = function HandleAgentConnect(socket)227{228 var self = this;229 socket.on('data', function HandleAgentConnectRead(data) {230 var data = data.toString();231 // Konea is supposed to send a GET / immediately after the tls connection is established.232 // If we get that, then return a 200, which causes Konea to "convert" into a SPDY server. 233 // We then convert to a SPDY proxy server.234 235 // Newer Konea send the kuid and other information as a query string parameter.236 // Older Konea don't send the kuid, so we want to request it from Konea.237 if(data.match(/^GET \/([?].*)? HTTP\/1.1\r\n/)) {238 // Parse the query string.239 var query_string = data.match(/[?](.*) /);240 var query_string_parts = {};241 if(query_string) {242 var query_string_parts = GLOBAL.qs.parse(query_string.slice(-1)[0]) || {};243 }244 // Pass the rest of the query string in as a config object.245 var config = JSON.parse(JSON.stringify(query_string_parts));246 delete config.kuid;247 var AcceptNewAgent = function AcceptNewAgent() {248 socket.write("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", function() {249 // Setup the spdy server.250 self.RunSpdyServer(socket, GLOBAL.config.http_agent, config)251 .on('error', self.HandleAgentDisconnect.bind(self))252 .on('close', self.HandleAgentDisconnect.bind(self))253 .once('kuidNegotiationSuccess', self.RegisterAgentHandler.bind(self))254 .NegotiateKuid(query_string_parts.kuid);255 }); 256 };257 var RejectNewAgent = function RejectNewAgent() {258 socket.write("HTTP/1.1 409 Conflict\r\nContent-Length: 0\r\n\r\n", function() {259 GLOBAL.log.info(GLOBAL.log.DuplicateKUIDMessage, agent);260 // We probably don't need to do this, but I'm going to leave it for good measure.261 socket.destroy();262 }); 263 };264 if(query_string_parts.kuid) {265 var agent = {};266 agent.kuid = query_string_parts.kuid; 267 agent.addr = socket.remoteAddress;268 agent.version = config.ver;269 self.CheckForDuplicateAgent(270 agent,271 AcceptNewAgent,272 RejectNewAgent273 );274 }275 else {276 AcceptNewAgent();277 }278 }279 // If we got here, then konea didn't send us a GET /, so return a 400 and destroy the socket.280 else {281 socket.write("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", function() {282 self.HandleClientError(new Error('konea failed to send GET / request.'), socket.pair);283 // We probably don't need to do this, but I'm going to leave it for good measure.284 socket.destroy();285 });286 }287 // Make sure the 'data' listener is removed so we don't trap anymore requests at this level.288 socket.removeListener('data', HandleAgentConnectRead);289 });290 // If we get here, then we had a connection error.291 socket.on('error', function(error) {292 GLOBAL.log.debug("Socket Error", error);293 });...

Full Screen

Full Screen

reducers.ts

Source:reducers.ts Github

copy

Full Screen

1import { on } from 'ts-action-immer'2import { reducer } from 'ts-action'3import { AccountStatus } from '@smartsupp/websocket-client'4import { AgentActions } from './actions'5import { AgentRating } from '../../model/Enums'6import { mapAgentFromServerToLocal } from '../../utils/messageHelpers'7import { Agent } from '../../model/Agent'8export const initialState = {9 agents: [] as Agent[],10 connectedAgents: [] as Agent[],11 rating: undefined as AgentRating | undefined,12 isTyping: false,13 status: AccountStatus.Offline,14}15export type AgentState = typeof initialState16export const agentReducer = reducer<AgentState>(17 initialState,18 on(AgentActions.setAgents, (state: AgentState, { payload }) => {19 state.agents = payload20 }),21 on(AgentActions.setConnectedAgents, (state: AgentState, { payload }) => {22 state.connectedAgents = payload23 }),24 on(AgentActions.setRating, (state: AgentState, { payload }) => {25 state.rating = payload26 }),27 on(AgentActions.setIsAgentTyping, (state: AgentState, { payload }) => {28 state.isTyping = payload29 }),30 on(AgentActions.setStatus, (state: AgentState, { payload }) => {31 state.status = payload32 }),33 on(AgentActions.updateAgentStatus, (state: AgentState, { payload }) => {34 state.agents = state.agents.map(p => (p.id === payload.id ? { ...p, status: payload.status } : p))35 state.connectedAgents = state.connectedAgents.map(p => (p.id === payload.id ? { ...p, status: payload.status } : p))36 }),37 on(AgentActions.updateAgent, (state: AgentState, { payload }) => {38 state.agents = state.agents.map(p => (p.id === payload.id ? { ...p, ...payload.changes } : p))39 state.connectedAgents = state.connectedAgents.map(p => (p.id === payload.id ? { ...p, ...payload.changes } : p))40 }),41 on(AgentActions.addConnectedAgent, (state: AgentState, { payload }) => {42 const mappedAgent = mapAgentFromServerToLocal(payload) // TODO refactor43 state.connectedAgents.push(mappedAgent)44 }),45 on(AgentActions.addConnectedAgentFromTransfer, (state: AgentState, { payload }) => {46 const mappedAgent = mapAgentFromServerToLocal(payload.assigned)47 state.connectedAgents.push(mappedAgent)48 }),49 on(AgentActions.removeConnectedAgentFromTransfer, (state: AgentState, { payload }) => {50 const mappedAgent = mapAgentFromServerToLocal(payload.unassigned)51 state.connectedAgents = state.connectedAgents.filter(a => a.id !== mappedAgent.id)52 }),53 on(AgentActions.removeAllConnectedAgents, (state: AgentState, _) => {54 state.connectedAgents = []55 }),56 on(AgentActions.removeConnectedAgent, (state: AgentState, { payload }) => {57 state.connectedAgents = state.connectedAgents.filter(a => a.id !== payload)58 }),59 on(AgentActions.deleteAgent, (state: AgentState, { payload }) => {60 state.agents = state.agents.filter(a => a.id !== payload)61 state.connectedAgents = state.connectedAgents.filter(a => a.id !== payload)62 }),...

Full Screen

Full Screen

agentHelpers.ts

Source:agentHelpers.ts Github

copy

Full Screen

1import { SimpleAgent } from '@smartsupp/websocket-client'2import { Agent } from '../model/Agent'3import { mapAgentFromServerToLocal } from './messageHelpers'4export const getAgentFromArray = (agents: Agent[], agentId: string | null) => agents.find(a => a.id === agentId)5export const filteredCorrectGroupAgents = (agents: Agent[], groupFromSite: string | undefined | null): Agent[] => {6 if (groupFromSite) {7 if (groupFromSite === 'default') {8 return agents9 }10 return agents.filter(11 a => (a.groups && a.groups.length > 0 && a.groups.includes(groupFromSite)) || (a.groups && a.groups.length === 0),12 )13 }14 return agents15}16export const filterConnectedAgents = (assignedIds: string[] | undefined | null, agents: SimpleAgent[]) => {17 const connectedAgents = [] as SimpleAgent[]18 if (assignedIds && assignedIds.length) {19 assignedIds.map(element => {20 const foundAgent = agents.find(a => a.id === element)21 return foundAgent && connectedAgents.push(foundAgent)22 })23 return connectedAgents.map(mapAgentFromServerToLocal)24 }25 return []...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var BestAgent = require('./BestAgent.js');2var agent = new BestAgent('test4');3agent.connectedAgents(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10 at Object. (/Users/andrew/Documents/development/connectedAgents/test4.js:5:9)11 at Module._compile (module.js:456:26)12 at Object.Module._extensions..js (module.js:474:10)13 at Module.load (module.js:356:32)14 at Function.Module._load (module.js:312:12)15 at Function.Module.runMain (module.js:497:10)16 at startup (node.js:119:16)17var BestAgent = require('./BestAgent.js');18var agent = new BestAgent('test4');19BestAgent.prototype.connectedAgents = function(callback) {20}21agent.connectedAgents(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var BestAgent = require('./BestAgent.js');29var agent = new BestAgent('test4');30agent.connectedAgents = function(callback) {31}32agent.connectedAgents(function(err, data) {33 if (err) {34 console.log(err);35 } else {36 console.log(data);37 }38});

Full Screen

Using AI Code Generation

copy

Full Screen

1var bestRoute = new BestRoute();2var agents = new Array();3agents[0] = new Agent('a');4agents[1] = new Agent('b');5agents[2] = new Agent('c');6agents[3] = new Agent('d');7agents[4] = new Agent('e');8var connections = new Array();9connections[0] = new Connection(agents[0], agents[1], 1);10connections[1] = new Connection(agents[0], agents[2], 4);11connections[2] = new Connection(agents[0], agents[3], 2);12connections[3] = new Connection(agents[1], agents[2], 3);13connections[4] = new Connection(agents[1], agents[3], 1);14connections[5] = new Connection(agents[1], agents[4], 4);15connections[6] = new Connection(agents[2], agents[3], 2);16connections[7] = new Connection(agents[3], agents[4], 3);17connections[8] = new Connection(agents[4], agents[0], 3);18var route = bestRoute.connectedAgents(agents[0], agents[4], connections);19for(var i

Full Screen

Using AI Code Generation

copy

Full Screen

1var agentSelector = require('agent-selector');2var agentSelector = new agentSelector.BestAgentSelector();3var agentList = agentSelector.connectedAgents();4console.log(agentList);5var agentSelector = require('agent-selector');6var agentSelector = new agentSelector.BestAgentSelector();7var agentList = agentSelector.connectedAgents();8console.log(agentList);9var agentSelector = require('agent-selector');10var agentSelector = new agentSelector.BestAgentSelector();11var agentList = agentSelector.connectedAgents();12console.log(agentList);13var agentSelector = require('agent-selector');14var agentSelector = new agentSelector.BestAgentSelector();15var agentList = agentSelector.connectedAgents();16console.log(agentList);17var agentSelector = require('agent-selector');18var agentSelector = new agentSelector.BestAgentSelector();19var agentList = agentSelector.connectedAgents();20console.log(agentList);21var agentSelector = require('agent-selector');22var agentSelector = new agentSelector.BestAgentSelector();23var agentList = agentSelector.connectedAgents();24console.log(agentList);

Full Screen

Using AI Code Generation

copy

Full Screen

1var bestbuy = require('bestbuy')('3k4c7z8jv4u4w7w4m4u4j4y4');2var fs = require('fs');3var sleep = require('sleep');4var async = require('async');5var request = require('request');6var cheerio = require('cheerio');7var json2csv = require('json2csv');

Full Screen

Using AI Code Generation

copy

Full Screen

1var bestMatchAgent = require('./BestMatchAgent');2{ id: 1, name: "John", skills: ['Java', 'C++', 'C#'] },3{ id: 2, name: "Mary", skills: ['Java', 'C#', 'Python'] },4{ id: 3, name: "Mike", skills: ['C#', 'C++', 'Python'] },5{ id: 4, name: "John", skills: ['Java', 'C++', 'C#'] },6{ id: 5, name: "Mary", skills: ['Java', 'C#', 'Python'] },7{ id: 6, name: "Mike", skills: ['C#', 'C++', 'Python'] },8{ id: 7, name: "John", skills: ['Java', 'C++', 'C#'] },9{ id: 8, name: "Mary", skills: ['Java', 'C#', 'Python'] },10{ id: 9, name: "Mike", skills: ['C#', 'C++', 'Python'] },11{ id: 10, name: "John", skills: ['Java', 'C++', 'C#'] },12{ id: 11, name: "Mary", skills: ['Java', 'C#', 'Python'] },13{ id: 12, name: "Mike", skills: ['C#', 'C++', 'Python'] },14{ id: 13, name: "John", skills: ['Java', 'C++', 'C#'] },15{ id: 14, name: "Mary", skills: ['Java', 'C#', 'Python'] },16{ id: 15, name: "Mike", skills: ['C#', 'C++', 'Python'] },17{ id: 16, name: "John", skills: ['Java', 'C++', 'C#'] },18{ id: 17, name: "Mary", skills: ['Java', 'C#', 'Python'] },19{ id: 18, name: "Mike", skills: ['C#', 'C++', 'Python'] }20];21var skills = ['Java', 'C#', 'Python'];

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Best automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful