/** * Sends the data contained in the passed buffer to the client * * @param dataBuffer The data to send to the client */ private void sendDataPacket(User user, byte[] dataBuffer) { DatagramPacket outputPacket = new DatagramPacket(dataBuffer, dataBuffer.length, user.getAddress(), mediaPort); try { if (serverSocket != null) { serverSocket.send(outputPacket); log.info("Sent data packet to: " + user.getAddress().getHostAddress() + ":" + mediaPort); } } catch (IOException e) { log.error( "Error sending image frame to client: " + user.getAddress().getHostAddress() + ":" + mediaPort); e.printStackTrace(); } }
/** * Adds a new user to the list of users that should receive a media stream. * * @param user The user to stream media to * @return True if the user was successfully added, false if not (duplicate record existed) */ public boolean addUser(User user) { if (user == null) return false; synchronized (clients) { for (User u : clients) { if (u == user) { return false; } } clients.add(user); log.info("Added user \"" + user.getUsername() + "\" to streaming media clients."); return true; } }
/** * Removes a user from the list of users that should receive a media stream. * * @param user The user to stop streaming media to * @return True if the user was successfully removed, false if not (user could not be found in * existing client list) */ public boolean removeUser(User user) { synchronized (clients) { log.info("Removing user \"" + user.getUsername() + "\" from streaming media clients."); return clients.remove(user); } }