public String sendCommand(String name, String command, bool append) { Router targetRouter = GetRouter(name); RouterSocket targetSocket = null; byte [] bytes = new byte[100000]; string response = null; try { targetSocket = targetRouter.socket; targetSocket.InitConversation(); }catch (Exception e) { rList.Remove(targetRouter); cList.Remove(targetRouter); nodelist.Remove(targetRouter.identifier); mainWindow.nodeBox.ItemsSource = nodelist; int selected = mainWindow.selectionBox.SelectedIndex; mainWindow.selectionBox.SelectedIndex = (selected == 1 ? 0 : 1); targetRouter.connected = false; mainWindow.appendConsole(string.Format("{0} nie odpowiada. Spróbuj ponownie po odświeżeniu.", name), null, null); return(null); } Socket conversationSocket = targetSocket.GetSocket(); try { byte[] msg = Encoding.ASCII.GetBytes(command); conversationSocket.Send(msg); //while (true) { bytes = new byte[1024]; int bytesRec = conversationSocket.Receive(bytes); response += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (append) { mainWindow.appendConsole(response, name, command); } } } catch (Exception e) { mainWindow.appendConsole("Polecenie nie mogło być zrealizowane. Spróbuj ponownie", null, null); } // TODO zmienić to return(response); }
private string identify(Router r) { RouterSocket targetSocket = r.socket; byte[] bytes = new byte[100000]; string response = null; Socket conversationSocket = null; try { targetSocket.InitConversation(); conversationSocket = targetSocket.GetSocket(); } catch (Exception e) { mainWindow.appendConsole("Could not connect to specified endpoint", null, null); } try { byte[] msg = Encoding.ASCII.GetBytes("identify|"); conversationSocket.Send(msg); bytes = new byte[1024]; int bytesrec = 0; if (conversationSocket.Poll(1000000, SelectMode.SelectRead)) { bytesrec = conversationSocket.Receive(bytes); // This call will not block } else { mainWindow.appendConsole("Could not reach endpoint on port " + r.socket.port + ". Try again", null, null); } response += Encoding.ASCII.GetString(bytes, 0, bytesrec); r.connected = true; string[] responseArray = response.Split('|'); if (responseArray[0].Equals("router")) { string routerName = responseArray[1]; r.nodetype = "router"; return(routerName); } else if (responseArray[0].Equals("client")) { string clientName = responseArray[1]; r.nodetype = "client"; return(clientName); } else { mainWindow.appendConsole("Endpoint " + r.port + " provided corrupted response. Try again", null, null); return(null); } } catch (Exception e) { return(null); } }
private string identify(Router r) { RouterSocket targetSocket = r.socket; byte[] bytes = new byte[100000]; string response = null; Socket conversationSocket = null; try { targetSocket.InitConversation(); conversationSocket = targetSocket.GetSocket(); } catch (Exception e) { mainWindow.appendConsole("Nie można było nawiązać połączania na porcie " + r.port + " ,błąd: " + e.Message, null, null); } try { byte[] msg = Encoding.ASCII.GetBytes("identify|"); conversationSocket.Send(msg); bytes = new byte[1024]; int bytesrec = 0; if (conversationSocket.Poll(1000000, SelectMode.SelectRead)) { bytesrec = conversationSocket.Receive(bytes); // This call will not block } else { mainWindow.appendConsole("Nie można było nawiązać połączenia na porcie " + r.socket.port + ". Spróbuj ponownie", null, null); } response += Encoding.ASCII.GetString(bytes, 0, bytesrec); r.connected = true; string[] responseArray = response.Split('|'); if (responseArray[0].Equals("router")) { string routerName = responseArray[1]; r.nodetype = "router"; return(routerName); } else if (responseArray[0].Equals("client")) { string clientName = responseArray[1]; r.nodetype = "client"; return(clientName); } else { mainWindow.appendConsole("Endpoint " + r.port + " podał nieprawidłową odpowiedź. Spróbuj ponownie", null, null); return(null); } } catch (Exception e) { mainWindow.appendConsole("Błąd podczas identyfikacji: " + e.Message, null, null); return(null); } }