⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netadapterhost.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      {         System.out.println("   getBlock called, speed=" + adapter.getSpeed());         System.out.println("      len=" + len);      }      // get the bytes      byte[] b = adapter.getBlock(len);      if(DEBUG)      {         System.out.println("      returned: " + Convert.toHexString(b));      }      conn.output.writeByte(RET_SUCCESS);      conn.output.write(b, 0, len);      conn.output.flush();   }   private void adapterDataBlock(Connection conn)      throws IOException, OneWireException   {      if(DEBUG)      {         System.out.println("   DataBlock called, speed=" + adapter.getSpeed());      }      // get the number to block      int len = conn.input.readInt();      // get the bytes to block      byte[] b = new byte[len];      conn.input.readFully(b, 0, len);      if(DEBUG)      {         System.out.println("      " + len + " bytes");         System.out.println("      Send: " + Convert.toHexString(b));      }      // do the block      adapter.dataBlock(b, 0, len);      if(DEBUG)      {         System.out.println("      Recv: " + Convert.toHexString(b));      }      conn.output.writeByte(RET_SUCCESS);      conn.output.write(b, 0, len);      conn.output.flush();   }   //--------   //-------- 1-Wire Network power methods   //--------   private void adapterSetPowerDuration(Connection conn)      throws IOException, OneWireException   {      // get the time factor value      int timeFactor = conn.input.readInt();      if(DEBUG)      {         System.out.println("   setPowerDuration called, speed=" + adapter.getSpeed());         System.out.println("      timeFactor=" + timeFactor);      }      // call setPowerDuration      adapter.setPowerDuration(timeFactor);      conn.output.writeByte(RET_SUCCESS);      conn.output.flush();   }   private void adapterStartPowerDelivery(Connection conn)      throws IOException, OneWireException   {      // get the change condition value      int changeCondition = conn.input.readInt();      if(DEBUG)      {         System.out.println("   startPowerDelivery called, speed=" + adapter.getSpeed());         System.out.println("      changeCondition=" + changeCondition);      }      // call startPowerDelivery      boolean success = adapter.startPowerDelivery(changeCondition);      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(success);      conn.output.flush();   }   private void adapterSetProgramPulseDuration(Connection conn)      throws IOException, OneWireException   {      // get the time factor value      int timeFactor = conn.input.readInt();      if(DEBUG)      {         System.out.println("   setProgramPulseDuration called, speed=" + adapter.getSpeed());         System.out.println("      timeFactor=" + timeFactor);      }      // call setProgramPulseDuration      adapter.setProgramPulseDuration(timeFactor);      conn.output.writeByte(RET_SUCCESS);      conn.output.flush();   }   private void adapterStartProgramPulse(Connection conn)      throws IOException, OneWireException   {      // get the change condition value      int changeCondition = conn.input.readInt();      if(DEBUG)      {         System.out.println("   startProgramPulse called, speed=" + adapter.getSpeed());         System.out.println("      changeCondition=" + changeCondition);      }      // call startProgramPulse();      boolean success = adapter.startProgramPulse(changeCondition);      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(success);      conn.output.flush();   }   private void adapterStartBreak(Connection conn)      throws IOException, OneWireException   {      if(DEBUG)      {         System.out.println("   startBreak called, speed=" + adapter.getSpeed());      }      // call startBreak();      adapter.startBreak();      conn.output.writeByte(RET_SUCCESS);      conn.output.flush();   }   private void adapterSetPowerNormal(Connection conn)      throws IOException, OneWireException   {      if(DEBUG)      {         System.out.println("   setPowerNormal called, speed=" + adapter.getSpeed());      }      // call setPowerNormal      adapter.setPowerNormal();      conn.output.writeByte(RET_SUCCESS);      conn.output.flush();   }   //--------   //-------- 1-Wire Network speed methods   //--------   private void adapterSetSpeed(Connection conn)      throws IOException, OneWireException   {      // get the value of the new speed      int speed = conn.input.readInt();      if(DEBUG)      {         System.out.println("   setSpeed called, speed=" + adapter.getSpeed());         System.out.println("      speed=" + speed);      }      // do the setSpeed      adapter.setSpeed(speed);      conn.output.writeByte(RET_SUCCESS);      conn.output.flush();   }   private void adapterGetSpeed(Connection conn)      throws IOException, OneWireException   {      // get the adapter speed      int speed = adapter.getSpeed();      if(DEBUG)      {         System.out.println("   getSpeed called, speed=" + adapter.getSpeed());         System.out.println("      speed=" + speed);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeInt(speed);      conn.output.flush();   }   //--------   //-------- Adapter feature methods   //--------   private void adapterCanOverdrive (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canOverdrive();      if(DEBUG)      {         System.out.println("   canOverdrive returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   private void adapterCanHyperdrive (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canHyperdrive();      if(DEBUG)      {         System.out.println("   canHyperDrive returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   private void adapterCanFlex (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canFlex();      if(DEBUG)      {         System.out.println("   canFlex returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   private void adapterCanProgram (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canProgram();      if(DEBUG)      {         System.out.println("   canProgram returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   private void adapterCanDeliverPower (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canDeliverPower();      if(DEBUG)      {         System.out.println("   canDeliverPower returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   private void adapterCanDeliverSmartPower (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canDeliverSmartPower();      if(DEBUG)      {         System.out.println("   canDeliverSmartPower returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   private void adapterCanBreak (Connection conn)      throws IOException, OneWireException   {      boolean b = adapter.canBreak();      if(DEBUG)      {         System.out.println("   canBreak returned " + b);      }      conn.output.writeByte(RET_SUCCESS);      conn.output.writeBoolean(b);      conn.output.flush();   }   //--------   //-------- Inner classes   //--------   /**    * Private inner class for servicing new connections.    * Can be run in it's own thread or in the same thread.    */   private class SocketHandler implements Runnable   {      /**       * The connection that is being serviced.       */      private Connection conn;      /**       * indicates whether or not the handler is currently running       */      private volatile boolean handlerRunning = false;      /**       * Constructor for socket servicer.  Creates the input and output       * streams and send's the version of this host to the client       * connection.       */      public SocketHandler(Socket sock)         throws IOException      {         // set socket timeout to 10 seconds         sock.setSoTimeout(timeoutInSeconds*1000);         // create the connection object         conn = new Connection();         conn.sock = sock;         conn.input = new DataInputStream(conn.sock.getInputStream());         if(BUFFERED_OUTPUT)         {            conn.output = new DataOutputStream(new BufferedOutputStream(                                                 conn.sock.getOutputStream()));         }         else         {            conn.output = new DataOutputStream(conn.sock.getOutputStream());         }         // first thing transmitted should be version info         if(!sendVersionUID(conn))         {            throw new IOException("send version failed");         }         // authenticate the client         byte[] chlg = new byte[8];         rand.nextBytes(chlg);         conn.output.write(chlg);         conn.output.flush();         // compute the crc of the secret and the challenge         int crc = CRC16.compute(netAdapterSecret, 0);         crc = CRC16.compute(chlg, crc);         int answer = conn.input.readInt();         if(answer!=crc)         {            conn.output.writeByte(RET_FAILURE);            conn.output.writeUTF("Client Authentication Failed");            conn.output.flush();            throw new IOException("authentication failed");         }         else         {            conn.output.writeByte(RET_SUCCESS);            conn.output.flush();         }      }      /**       * Run method for socket Servicer.       */      public void run()      {         handlerRunning = true;         try         {            while(!hostStopped && conn.sock!=null)            {               processRequests(conn);            }         }         catch(Throwable t)         {            if(DEBUG)               t.printStackTrace();            close(conn);         }         handlerRunning = false;         if(!hostStopped && !singleThreaded)         {            synchronized(hashHandlers)            {               // thread finished running without being stopped.               // politely remove it from the hashtable.               hashHandlers.remove(Thread.currentThread());            }         }      }      /**       * Waits for handler to finish, with a timeout.       */      public void stopHandler()      {         int i = 0;         int timeout = 3000;         while(handlerRunning && i++<timeout)            try{Thread.sleep(10);}catch(Exception e){;}      }   }   //--------   //-------- Default Main Method, for launching server with defaults   //--------   /**    * A Default Main Method, for launching NetAdapterHost getting the    * default adapter with the OneWireAccessProvider and listening on    * the default port specified by DEFAULT_PORT.    */   public static void main(String[] args)      throws Exception   {      DSPortAdapter adapter         = com.dalsemi.onewire.OneWireAccessProvider.getDefaultAdapter();      NetAdapterHost host = new NetAdapterHost(adapter, true);      System.out.println("Starting Multicast Listener");      host.createMulticastListener();      System.out.println("Starting NetAdapter Host");      (new Thread(host)).start();      //if(System.in!=null)      //{      //   System.out.println("\nPress Enter to Shutdown");      //   (new BufferedReader(new InputStreamReader(System.in))).readLine();      //   host.stopHost();      //   System.exit(1);      //}   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -