public boolean sendFile() { try (Socket socket = new Socket(ipAddr, port); OutputStream toServer = socket.getOutputStream()) { // TODO put in a guid Printer.log("Sending file " + path.getFileName() + "on connection " + ipAddr + ":" + port); /* FileChannel fileSrc = FileChannel.open(path); //Send over the file size first; they're waiting on this int fileSize = (int) Files.size(path); ByteBuffer fileSizeBuff = ByteBuffer.allocate(NetworkUtil.numBytesInInt); fileSizeBuff.putInt(fileSize); toServer.write(fileSizeBuff.array()); //Read in the file byte[] fileBytes = new byte[fileSize]; ByteBuffer fileBytesBuff = ByteBuffer.wrap(fileBytes); fileSrc.read(fileBytesBuff); //Send it to the server toServer.write(fileBytesBuff.array()); toServer.flush(); */ FileUtil.getInstance().readFileToStream(path, toServer); Printer.log("File sent!"); return true; } catch (Exception ex) { Printer.logErr("Exception encountered in sending file to server " + ipAddr); Printer.logErr(ex); return false; } }
private void sendMachineInfo(PrintWriter output, BufferedReader in) { MachineInfo info = new MachineInfo(); try { output.println(info.toJSON()); } catch (IOException e) { Printer.logErr(e); output.println(LanguageProtocol.SKIPPED); } }
public void createTalker() { try (Scanner userInput = new Scanner(System.in); Socket sock = new Socket(serverName, port); PrintWriter output = new PrintWriter(sock.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); ) { // TalkerResponder responder = new TalkerResponder(serverName, in, output); Printer.log("Connected to server " + sock.getInetAddress()); String serverMessage = ""; // output.println(LanguageProtocol.SYN); if (jdfsRunningOnServer(output, in)) { int attempts = 0; do { sendMachineInfo(output, in); serverMessage = in.readLine(); } while ((null == serverMessage || !(serverMessage.equals(LanguageProtocol.CONFIRM_ADD_ACCOUNT) || serverMessage.equals(LanguageProtocol.QUERY_ACCOUNT_EXISTS))) && attempts++ < 3); if (LanguageProtocol.keepGoing(serverMessage)) { if (payload instanceof FileSender) { sendFile(output, in); } else if (payload instanceof FileRetriever) { retrieveFile(output, in); } // TODO: Add peer request output.println(LanguageProtocol.CLOSE); } /* String msg = "", recieved = ""; do { //////////////////////////TODO //Drop all this logic. //Instead, We're also gonna pass in an object to create the talker //We'll tell the desired operation from the class type //Then, we'll call a commonly named method from that class if (!msg.equals(LanguageProtocol.INIT_FILE_TRANS)) { msg = userInput.next(); Printer.log("Message to post:"+msg); if (msg != null && !msg.equals("")) { if(msg.contains("send-file")) { msg=LanguageProtocol.INIT_FILE_TRANS; } output.println(msg); //TODO do this in a much cleaner, more modular way Printer.log("Messsage from server:"+recieved); recieved = in.readLine(); Printer.log("Messsage from server:"+recieved); } } else { Printer.log("Calling handle"); //responder.HandleFileSender(path); //TODO figure out how to get the path as input msg = "asd"; } } while(!(null == msg || msg.equals("") || msg.equals("exit") || msg.equals(LanguageProtocol.CLOSE))); */ } } catch (IOException e) { Printer.logErr("Could not listen on port " + port); Printer.logErr(e); } catch (Exception e) { Printer.logErr(e); } }