protected void doReadObject(ObjectInputStream s) { try { s.defaultReadObject(); read(s); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void run() { try { out = new ObjectOutputStream(csocket.getOutputStream()); out.flush(); in = new ObjectInputStream(csocket.getInputStream()); // Look for chunks String currentDir = System.getProperty("user.dir"); File folder = new File(currentDir + "/src/srcFile"); File[] listOfFiles = folder.listFiles(); int fileCount = 0; OutputStream os; for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile() && listOfFiles[i] .getName() .toLowerCase() .contains("chunk")) { // 0 1 10 11 12 13 14 2 20 21 22 23 3 4 5 .... fileCount++; String chunkName = listOfFiles[i].getName().toString(); chunkId = chunkName.substring(chunkName.lastIndexOf('.') + 6); xPayload(chunkId); if ((connTo.equals("2") && Integer.parseInt(chunkId) % noDev == 0) || (connTo.equals("3") && (Integer.parseInt(chunkId) - 1) % noDev == 0) || (connTo.equals("4") && (Integer.parseInt(chunkId) - 2) % noDev == 0) || (connTo.equals("5") && (Integer.parseInt(chunkId) - 3) % noDev == 0) || (connTo.equals("6") && (Integer.parseInt(chunkId) - 4) % noDev == 0)) { System.out.println(chunkName); sendFile(chunkName); } } } xPayload("-1"); System.out.println("All chunks sent."); } catch (IOException ioException) { ioException.printStackTrace(); } finally { // Close connections try { in.close(); out.close(); csocket.close(); System.out.println("Thread closed."); } catch (IOException ioException) { System.out.println("Client " + devId + " disconnected."); } } }
// Exchange chunkId, devId, and ownedChunkArray void xPayload(String chunkId) { Socket conn = null; try { // Get message Object[] rPayload = (Object[]) in.readObject(); chunkOwnedClientArray = (String[]) rPayload[1]; connTo = (String) rPayload[0]; chunkOwnedClientList = Arrays.asList(chunkOwnedClientArray); // Send Message Object[] payload = {chunkId, devId, chunkOwnedArray, filename}; out.writeObject(payload); out.flush(); System.out.println("chunkId being sent: " + chunkId + ", connected to: " + connTo); } catch (IOException ioException) { ioException.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }