netadapter.java
来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 2,064 行 · 第 1/5 页
JAVA
2,064 行
// figure out what the multicast group is if(multicastGroup==null) { String group = null; try { group = OneWireAccessProvider.getProperty("NetAdapter.MulticastGroup"); } catch(Throwable t){;} if(group==null) multicastGroup = DEFAULT_MULTICAST_GROUP; else multicastGroup = group; } MulticastSocket socket = null; InetAddress group = null; try { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { System.out.println("DEBUG: Opening multicast on port: " + datagramPort); System.out.println("DEBUG: joining group: " + multicastGroup); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // create the multi-cast socket socket = new MulticastSocket(datagramPort); // create the group's InetAddress group = InetAddress.getByName(multicastGroup); // join the group socket.joinGroup(group); // convert the versionUID to a byte[] byte[] versionBytes = Convert.toByteArray(versionUID); // send a packet with the versionUID DatagramPacket outPacket = new DatagramPacket(versionBytes, 4, group, datagramPort); socket.send(outPacket); // set a timeout of 1/2 second for the receive socket.setSoTimeout(500); byte[] receiveBuffer = new byte[32]; for(;;) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) System.out.println("DEBUG: waiting for multicast packet"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// DatagramPacket inPacket = new DatagramPacket(receiveBuffer, receiveBuffer.length); socket.receive(inPacket); int length = inPacket.getLength(); byte[] data = inPacket.getData(); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { System.out.println("DEBUG: packet.length=" + length); System.out.println("DEBUG: expecting=" + 5); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(length == 5 && data[4]==(byte)0xFF) { int listenPort = Convert.toInt(data, 0, 4); v.addElement(inPacket.getAddress().getHostName() + ":" + listenPort); } } } catch(Exception e) {/*drain*/;} finally { try { socket.leaveGroup(group); socket.close(); } catch(Exception e) {/*drain*/;} } } // get all servers from the properties file String server = ""; try { for(int i=0; server!=null; i++) { server = OneWireAccessProvider.getProperty("NetAdapter.host"+i); if(server!=null) v.addElement(server); } } catch(Throwable t){;} return v.elements(); } /** * Specifies a platform appropriate port name for this adapter. Note that * even though the port has been selected, it's ownership may be relinquished * if it is not currently held in a 'exclusive' block. This class will then * try to re-aquire the port when needed. If the port cannot be re-aquired * ehen the exception <code>PortInUseException</code> will be thrown. * * @param portName Address to connect this NetAdapter to, in the form of * "hostname:port". For example, "shughes.dalsemi.com:6161", where 6161 * is the port number to connect to. The use of NetAdapter.DEFAULT_PORT * is recommended. * * @return <code>true</code> if the port was aquired, <code>false</code> * if the port is not available. * * @throws OneWireIOException If port does not exist, or unable to communicate with port. * @throws OneWireException If port does not exist */ public boolean selectPort (String portName) throws OneWireIOException, OneWireException { synchronized(conn) { Socket s = null; try { int port = DEFAULT_PORT; // should be of the format "hostname:port" or hostname int index = portName.indexOf(':'); if(index>=0) { int index2 = portName.indexOf(':', index+1); if(index2<0) // no custom secret specified { port = Integer.parseInt(portName.substring(index+1)); // reset the secret to default resetSecret(); useCustomSecret = false; } else { // custom secret is specified setSecret(portName.substring(index2+1)); useCustomSecret = true; if(index < index2-1) // port number is specified port = Integer.parseInt(portName.substring(index+1, index2)); } portName = portName.substring(0, index); } else { // reset the secret resetSecret(); useCustomSecret = false; } s = new Socket(portName, port); } catch(IOException ioe) { throw new OneWireIOException("Can't reach server: "+ioe.getMessage()); } return selectPort(s); } } /** * New method, unique to NetAdapter. Sets the "port", i.e. the connection * to the server via an already established socket connection. * * @param sock Socket connection to NetAdapterHost * * @return <code>true</code> if connection to host was successful * * @throws OneWireIOException If port does not exist, or unable to communicate with port. * @throws OneWireException If port does not exist */ public boolean selectPort (Socket sock) throws OneWireIOException, OneWireException { boolean bSuccess = false; synchronized(conn) { Connection tmpConn = new Connection(); tmpConn.sock = sock; try { tmpConn.input = new DataInputStream(sock.getInputStream()); if(BUFFERED_OUTPUT) { tmpConn.output = new DataOutputStream(new BufferedOutputStream( sock.getOutputStream())); } else { tmpConn.output = new DataOutputStream(sock.getOutputStream()); } // check host version int hostVersionUID = tmpConn.input.readInt(); if(hostVersionUID==versionUID) { // tell the server that the versionUID matched tmpConn.output.writeByte(RET_SUCCESS); tmpConn.output.flush(); // if the versionUID matches, we need to authenticate ourselves // using the challenge from the server. byte[] chlg = new byte[8]; tmpConn.input.read(chlg, 0, 8); // compute the crc of the secret and the challenge int crc = CRC16.compute(netAdapterSecret, 0); crc = CRC16.compute(chlg, crc); // and send it back to the server tmpConn.output.writeInt(crc); tmpConn.output.flush(); // check to see if it matched checkReturnValue(tmpConn); bSuccess = true; } else { tmpConn.output.writeByte(RET_FAILURE); tmpConn.output.flush(); tmpConn = null; } } catch(IOException e) { bSuccess = false; tmpConn = null; } if(bSuccess) { portNameForReconnect = sock.getInetAddress().getHostName() + ":" + sock.getPort(); conn = tmpConn; } } // invalid response or version number return bSuccess; } /** * Frees ownership of the selected port, if it is currently owned, back * to the system. This should only be called if the recently * selected port does not have an adapter, or at the end of * your application's use of the port. * * @throws OneWireException If port does not exist */ public void freePort () throws OneWireException { try { synchronized(conn) { conn.output.writeByte(CMD_CLOSECONNECTION); conn.output.flush(); conn.sock.close(); conn = EMPTY_CONNECTION; } } catch(Exception e) { throw new OneWireException(COMM_FAILED + e.getMessage()); } } /** * Retrieves the name of the selected port as a <code>String</code>. * * @return <code>String</code> of selected port * * @throws OneWireException if valid port not yet selected */ public String getPortName () throws OneWireException { synchronized(conn) { if(!adapterDetected()) return "Not Connected"; else if (useCustomSecret) return conn.sock.getInetAddress().getHostName() + ":" + conn.sock.getPort() + ":" + new String(this.netAdapterSecret); else return conn.sock.getInetAddress().getHostName() + ":" + conn.sock.getPort(); } } /** * Returns whether adapter can physically support overdrive mode. * * @return <code>true</code> if this port adapter can do OverDrive, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canOverdrive () throws OneWireIOException, OneWireException { try { synchronized(conn) { // send beginExclusive command conn.output.writeByte(CMD_CANOVERDRIVE); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from beginExclusive return conn.input.readBoolean(); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Returns whether the adapter can physically support hyperdrive mode. * * @return <code>true</code> if this port adapter can do HyperDrive, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canHyperdrive () throws OneWireIOException, OneWireException { try { synchronized(conn) { // send beginExclusive command conn.output.writeByte(CMD_CANHYPERDRIVE); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from beginExclusive return conn.input.readBoolean(); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Returns whether the adapter can physically support flex speed mode. * * @return <code>true</code> if this port adapter can do flex speed, * <code>false</code> otherwise. * * @throws OneWireIOException on a 1-Wire communication error with the adapter * @throws OneWireException on a setup error with the 1-Wire * adapter */ public boolean canFlex () throws OneWireIOException, OneWireException { try { synchronized(conn) { // send beginExclusive command conn.output.writeByte(CMD_CANFLEX); conn.output.flush(); // check return value for success checkReturnValue(conn); // next parameter should be the return from beginExclusive return conn.input.readBoolean(); } } catch(IOException ioe) { throw new OneWireException(COMM_FAILED + ioe.getMessage()); } } /** * Returns whether adapter can physically support 12 volt power mode. * * @return <code>true</code> if this port adapter can do Program voltage,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?