Esempio n. 1
0
 public void run() {
   // read string from spp client
   try {
     BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
     String lineRead;
     while (((lineRead = bReader.readLine()) != null) && connection != null) {
       System.out.println(lineRead);
       synchronized (lock) {
         FileItem input = parser(lineRead);
         if (input.getCmd() == Command.cp) {
           // Open byte stream
         }
         if (input.getCmd() == Command.ack) {
           Scheduler.pollFromQueue();
         }
       }
     }
   } catch (IOException e) {
     System.out.println("Receiver closed");
   }
 }
Esempio n. 2
0
    public void run() {
      while (connection != null) {
        synchronized (lock) {
          FileItem fileItem = Scheduler.peekQueue();
          try {
            Thread.sleep(500);
          } catch (InterruptedException e2) {
            e2.printStackTrace();
          }
          FileInputStream fis = null;
          BufferedInputStream bis = null;
          if (fileItem != null) {
            try {
              try {
                Thread.sleep(100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              sendString(fileItem.toString());
              if (fileItem.getCmd() == Command.cp) {
                File file = new File(fileItem.getName());
                long length = file.length();
                if (length > Integer.MAX_VALUE) {
                  System.out.println("File is too large.");
                }
                byte[] bytes = new byte[buffersize];
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                System.out.println("Length: " + length);
                int count;
                int counter = 0;
                while (length > 0 && ((count = bis.read(bytes)) > 0)) {
                  try {
                    Thread.sleep(100);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                  outStream.write(bytes, 0, count);
                  outStream.flush();
                  length -= count;
                  counter++;
                  System.out.println("Length: " + length);
                }

                System.out.println("Outside file sending loop");
                System.out.println("Counter: " + counter);
                outStream.flush();
                fis.close();
                bis.close();
              }
              outStream.flush();
              Scheduler.pollFromQueue();
            } catch (IOException e) {
              e.printStackTrace();
              try {
                fis.close();
                bis.close();
              } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
              }
            }
          }
        }
      }
    }