// Start chatting via console. static void startIO() { System.out.println("Write messages divided by Enter. Write \"exit\" to close app."); while (true) { line = consoleReader.nextLine(); if (line.equalsIgnoreCase("exit")) { break; } try { client.send(new TextMessage(line)); } catch (IOException e) { ChatLogger.error("Can't send message"); ChatLogger.error(e); } catch (WrongMessageTypeException e) { ChatLogger.error("Wrong message type"); ChatLogger.error(e); } } System.exit(0); }
// Reads IP addresses from file. static void getServersFromRes() { ClassLoader classLoader = MainProvider.class.getClassLoader(); File file = new File(classLoader.getResource("addresses.txt").getFile()); try { fileReader = new Scanner(file); } catch (FileNotFoundException e) { ChatLogger.error("Can't open file with ips"); } while (fileReader.hasNext()) { line = fileReader.nextLine(); servers.add(line); } }
// Initializes local server. static void initNode() { List<InetAddress> temp = new ArrayList<>(servers.size()); try { for (String serv : servers) { InetAddress addressByName = InetAddress.getByName(serv); if (Config.isThisMyIpAddress(addressByName)) continue; temp.add(addressByName); } UDPServer server = new UDPServer(); ChatRoom chatRoom = new ChatRoom(server, temp); chatRoom.start(); client = new UDPClient(InetAddress.getByName("127.0.0.1")); } catch (IOException e) { ChatLogger.error(e); } }