Example #1
0
  public static int loadURIResource(URI uri, CPU cpu, int addr) {
    int i;

    System.out.println("loadURL: " + uri.toString());

    try {
      DataInputStream s = new DataInputStream(new BufferedInputStream(uri.toURL().openStream()));

      i = 0;
      try {
        while (true) {
          cpu.write8_a32(addr + i, s.readByte());
          i++;
        }
      } catch (EOFException e) {
        // end
      }
    } catch (IOException e) {
      e.printStackTrace(System.err);
      throw new IllegalArgumentException(e);
    }

    System.out.printf("loadURL: '%s' done, %dbytes.\n", uri.toString(), i);

    return i;
  }
  public static void send_JPIPstream(
      byte[] jpipstream, String j2kfilename, String tid, String cid) {
    try {
      Socket imgdecSocket = new Socket("localhost", 50000);
      DataOutputStream os = new DataOutputStream(imgdecSocket.getOutputStream());
      DataInputStream is = new DataInputStream(imgdecSocket.getInputStream());
      int length = 0;

      if (jpipstream != null) length = jpipstream.length;

      System.err.println("Sending " + length + "Data Bytes to decodingServer");

      os.writeBytes("JPIP-stream\n");
      os.writeBytes("version 1.2\n");
      os.writeBytes(j2kfilename + "\n");
      if (tid == null) os.writeBytes("0\n");
      else os.writeBytes(tid + "\n");
      if (cid == null) os.writeBytes("0\n");
      else os.writeBytes(cid + "\n");
      os.writeBytes(length + "\n");
      os.write(jpipstream, 0, length);

      byte signal = is.readByte();

      if (signal == 0) System.err.println("    failed");
    } catch (UnknownHostException e) {
      System.err.println("Trying to connect to unknown host: " + e);
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }
  }
  /* Réception d'un message du client */
  public String ReceiveMsg() {
    byte b;
    StringBuffer taille = new StringBuffer();
    StringBuffer message = new StringBuffer();

    try {
      while ((b = dis.readByte()) != (byte) '#') {
        if (b != (byte) '#') taille.append((char) b);
      }

      for (int i = 0; i < Integer.parseInt(taille.toString()); i++) {
        b = dis.readByte();
        message.append((char) b);
      }
    } catch (IOException e) {
      System.err.println("RunnableTraitement : Erreur de reception de msg (IO) : " + e);
    }

    return message.toString();
  }
Example #4
0
  public void readIn(DataInputStream din) throws IOException {
    lastActive = din.readLong();
    inBytes = din.readLong();
    outBytes = din.readLong();
    total = din.readLong();
    rmt_ip = din.readLong();
    lcl_port = din.readInt();
    rmt_port = din.readInt();

    for (int i = 0; i < tfPercent.length; ++i) {
      int num = (int) (din.readByte() & 0xFF);
      tfPercent[i] = ((double) num) / 100.0;
    }
  }
  public static void destroy_cid(String cid) {
    try {
      Socket imgdecSocket = new Socket("localhost", 50000);
      DataOutputStream os = new DataOutputStream(imgdecSocket.getOutputStream());
      DataInputStream is = new DataInputStream(imgdecSocket.getInputStream());

      os.writeBytes("CID destroy\n");
      os.writeBytes(cid + "\n");

      byte signal = is.readByte();

      if (signal == 0) System.err.println("    failed");
    } catch (UnknownHostException e) {
      System.err.println("Trying to connect to unknown host: " + e);
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }
  }
Example #6
0
 public void processMessage(DataInputStream dataIn) throws IOException {
   count = 0;
   int msgsize = dataIn.readInt();
   count = 1;
   int msgtype = 0;
   if (msgsize >= 2) msgtype = dataIn.readUnsignedShort();
   else {
     if (msgsize == 1) dataIn.readByte();
     ExpCoordinator.printer.print(
         new String("ERROR:NCCPConnection.run msgsize(" + msgsize + ") < 2 msgtype = " + msgtype));
     ExpCoordinator.printer.printHistory();
     return;
   }
   count = 2;
   switch (msgtype) {
     case NCCP.MessageResponse:
       ExpCoordinator.print(
           new String(
               "NCCPConnection::run message is  NCCP.MessageResponse " + msgsize + " " + msgtype),
           3);
       MessageRunnable tmp_msg = new MessageRunnable(msgsize, msgtype, dataIn);
       tmp_msg.print(5);
       SwingUtilities.invokeLater(tmp_msg);
       break;
     case NCCP.MessagePeriodic:
       ExpCoordinator.printer.print(
           new String(
               "NCCPConnection::run message is  NCCP.MessagePeriodic " + msgsize + " " + msgtype),
           3);
       processPeriodicMsg(msgsize, msgtype, dataIn);
       break;
     default:
       ExpCoordinator.printer.print(
           new String("NCCPConnection::run message is  Other " + msgsize + " " + msgtype));
       SwingUtilities.invokeLater(new MessageRunnable(msgsize, msgtype, dataIn));
   }
 }
 public byte readByte() throws IOException {
   return in.readByte();
 }
Example #8
0
  /** Method to keep the connection alive with the name server */
  public void run() {
    boolean connected = false;
    int ticket = 0;
    int serial = -1;
    int time = 1000;

    DatagramPacket inPack = new DatagramPacket(new byte[1024], 1024);
    DatagramPacket outPack = new DatagramPacket(new byte[1024], 1024);

    ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
    DataInputStream inData;
    DataOutputStream outData = new DataOutputStream(outBuf);

    while (running) {
      if (!connected) { // Thoust ought Register thine self
        try {
          outBuf.reset();
          outData.writeByte(0);
          outData.writeUTF(userName);
          outData.writeInt(portNum);
          outData.flush();
          outPack.setData(outBuf.toByteArray());
          nameServer.send(outPack);
        } catch (IOException e) {
          System.err.println("LeetActive: " + e);
        }
      } else { // Thoust ought Renew thine self
        try {
          outBuf.reset();
          outData.writeByte(2);
          outData.writeInt(ticket);
          outData.flush();
          outPack.setData(outBuf.toByteArray());
          nameServer.send(outPack);
        } catch (IOException e) {
          System.err.println(e);
        }
      }

      // Now we will receive a packet...
      try {
        nameServer.receive(inPack);
        inData = new DataInputStream(new ByteArrayInputStream(inPack.getData()));

        byte type = inData.readByte();

        if (type == 1) { // Twas a ticket packet
          try {
            ticket = inData.readInt();
            if (ticket > -1) { // Make sure its not evil
              connected = true;
            } else {
              connected = false;
            }
            time = inData.readInt();
          } catch (IOException e) {
            System.err.println(e);
          }
        }

        if (type == 5) { // Twas an update packet
          try {
            int s = inData.readInt();
            if (s > serial) { // Make sure its not old
              serial = s;
              int size = inData.readInt();
              ArrayList newList = new ArrayList(size);

              for (int x = 0; x < size; x++) {
                newList.add(
                    new String(
                        "" + inData.readUTF() + "@" + inData.readUTF() + ":" + inData.readInt()));
              }

              if (!newList.equals(hostList)) {
                hostList = newList;
                updated = true;
              }
            }
          } catch (IOException e) {
            System.err.println(e);
          }
        }
      } catch (SocketTimeoutException e) { // Server hates you
        connected = false;
        System.err.println(e);
      } catch (IOException e) {
        System.err.println(e);
      }

      try { // Take a nap
        sleep(time / 4);
      } catch (InterruptedException e) {
      }
    }
  }