/** * @return * @throws IOException */ public boolean onConnect() throws IOException { /* variaveis locais */ boolean back = false; ip = conteudo.getServerIp(); port = conteudo.getServerPort(); user = conteudo.getUser(); password = conteudo.getPassword(); ManagerConnectionFactory factory = new ManagerConnectionFactory(ip, user, password); this.managerConnection = factory.createManagerConnection(); // registro para eventos this.managerConnection.addEventListener(this); txtEvents = "Trying Connection to: " + ip + ":" + port; conteudo.setJlConnection(txtEvents); telaPrincipal.changeJlConnection(); this.changeInfo(txtEvents); try { // efetiva login this.managerConnection.login(); } catch (IllegalStateException | AuthenticationFailedException | TimeoutException ex) { Logger.getLogger(Aplicacao.class.getName()).log(Level.SEVERE, null, ex); } if (this.managerConnection.getState() == ManagerConnectionState.CONNECTED) { registrarClasses(); txtEvents = "Connected on: " + ip + ":" + port; conteudo.setJlConnection(txtEvents); this.changeInfo(txtEvents); back = true; } return back; }
/** @return */ public boolean sendPing() { ip = conteudo.getServerIp(); port = conteudo.getServerPort(); boolean retv = false; try { System.out.println("Sending a Ping."); InputStream ins = Runtime.getRuntime().exec("ping -n 1 -w 2000 " + ip).getInputStream(); Thread.sleep(3000); byte[] prsbuf = new byte[ins.available()]; ins.read(prsbuf); String parsstr = new StringTokenizer(new String(prsbuf), "%").nextToken().trim(); if (!parsstr.endsWith("100")) { retv = true; } // System.out.println(parsstr); this.changeInfo(parsstr); } catch (IOException | InterruptedException e) { System.out.println("IOException" + e.getMessage()); retv = false; } return retv; }