Beispiel #1
0
  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;
    }
  }
Beispiel #2
0
  private void retrieveFile(PrintWriter output, BufferedReader in)
      throws IOException, PortMgrException {
    FileRetriever fileRetriever = (FileRetriever) payload;

    output.println(LanguageProtocol.INIT_FILE_RETRIEVE);
    String response = in.readLine();

    if (null != response && response.equals(LanguageProtocol.ACK)) {
      String jsonOut = JDFSUtil.toJSON(fileRetriever.criteria);

      Printer.log("JSON of criteria:" + jsonOut);
      output.println(jsonOut);

      response = in.readLine();
      Printer.log("Got response " + response);

      if (null != response && response.equals(LanguageProtocol.ACCEPT_FILE_TRANS)) {
        output.println(LanguageProtocol.ACK);

        String json = in.readLine();
        ObjectMapper mapper = new ObjectMapper();
        FileRetrieverInfo incomingInfo = mapper.convertValue(json, FileRetrieverInfo.class);

        Printer.log("Got json:" + json);

        fileRetriever.setPort(PortMgr.getNextAvailablePort());

        Thread thread = new Thread(fileRetriever);
        thread.run();

        output.println(fileRetriever.port);

        /*
        response = in.readLine();
        if (null != response && response.equals(LanguageProtocol.ACK)) {
        	Thread thread = new Thread(fileRetriever);
        	thread.run();
        }*/

        FileHandler.getInstance().manageRecievedFile(fileRetriever.storeLocation, incomingInfo);
      }
    }
  }
Beispiel #3
0
  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);
    }
  }
Beispiel #4
0
  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);
    }
  }