/** * Unpack notifications from the binary form and link it with sending node (this information should be transferred * aside from the packed notification to not to produce redundant copies of it, all notifications from the same * node are packed together). Use {@link #pack(Collection)} to get packed binary. * * @param from node that has send notifications * @param packed representation * @return * @throws IOException */ static List<Notification> unpack(NodeInfo from, byte[] packed) throws IOException { ArrayList<Notification> notifications = new ArrayList<>(); Boss.Reader r = new Boss.Reader(packed); try { while (true) { // boss reader throws EOFException Notification n = read(from, r); if( n != null ) notifications.add(n); } } catch (EOFException x) { // normal, all data decoded } catch (InvocationTargetException | IllegalAccessException | InstantiationException | NullPointerException | NoSuchMethodException e) { throw new IOException("Failed to decoded notification", e); } return notifications; }
nn.add(Notification.read(from, r));
private List<Notification> unpack(byte[] packedNotifications) throws IOException { List<Notification> nn = new ArrayList<>(); try { // packet type code Boss.Reader r = new Boss.Reader(packedNotifications); if (r.readInt() != 1) throw new IOException("invalid packed notification type code"); // from node number int number = r.readInt(); NodeInfo from = getInfo(number); if (from == null) throw new IOException("unknown node number: " + number); // number of notifications in the packet int count = r.readInt(); if (count < 0 || count > 1000) throw new IOException("unvalid packed notifications count: " + count); for (int i = 0; i < count; i++) { nn.add(Notification.read(from, r)); } return nn; } catch (Exception e) { e.printStackTrace(); System.err.println("failed to unpack notification: " + e); throw new IOException("failed to unpack notifications"); } }