private LinkedList < Device > getDevices ( SSLSocket socket ) throws CommunicationException { // Compute LinkedList < Device > listDev = null ; try { InputStream socketStream = socket . getInputStream ( ) ; // Read bytes byte [ ] b = new byte [ 1024 ] ; ByteArrayOutputStream message = new ByteArrayOutputStream ( ) ; int nbBytes = 0 ; // socketStream.available can return 0 // http://forums.sun.com/thread.jspa?threadID=5428561 while ( ( nbBytes = socketStream . read ( b , 0 , 1024 ) ) != - 1 ) { message . write ( b , 0 , nbBytes ) ; } listDev = new LinkedList < Device > ( ) ; byte [ ] listOfDevices = message . toByteArray ( ) ; int nbTuples = listOfDevices . length / FEEDBACK_TUPLE_SIZE ; logger . debug ( "Found: [" + nbTuples + "]" ) ; for ( int i = 0 ; i < nbTuples ; i ++ ) { int offset = i * FEEDBACK_TUPLE_SIZE ; // Build date int index = 0 ; int firstByte = 0 ; int secondByte = 0 ; int thirdByte = 0 ; int fourthByte = 0 ; long anUnsignedInt = 0 ; firstByte = ( 0x000000FF & ( ( int ) listOfDevices [ offset ] ) ) ; secondByte = ( 0x000000FF & ( ( int ) listOfDevices [ offset + 1 ] ) ) ; thirdByte = ( 0x000000FF & ( ( int ) listOfDevices [ offset + 2 ] ) ) ; fourthByte = ( 0x000000FF & ( ( int ) listOfDevices [ offset + 3 ] ) ) ; index = index + 4 ; anUnsignedInt = ( ( long ) ( firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte ) ) & 0xFFFFFFFF L ; Timestamp timestamp = new Timestamp ( anUnsignedInt * 1000 ) ; // Build device token length int deviceTokenLength = listOfDevices [ offset + 4 ] << 8 | listOfDevices [ offset + 5 ] ; // Build device token String deviceToken = "" ; int octet = 0 ; for ( int j = 0 ; j < 32 ; j ++ ) { octet = ( 0x000000FF & ( ( int ) listOfDevices [ offset + 6 + j ] ) ) ; deviceToken = deviceToken . concat ( String . format ( "%02x" , octet ) ) ; } // Build device and add to list /* Create a basic device, as we do not want to go through the factory and create a device in the actual database... */ Device device = new BasicDevice ( ) ; device . setToken ( deviceToken ) ; device . setLastRegister ( timestamp ) ; listDev . add ( device ) ; logger . info ( "FeedbackManager retrieves one device : " + timestamp + ";" + deviceTokenLength + ";" + deviceToken + "." ) ; } // Close the socket and return the list } catch ( Exception e ) { logger . debug ( "Caught exception fetching devices from Feedback Service" ) ; throw new CommunicationException ( "Problem communicating with Feedback service" , e ) ; } finally { try { socket . close ( ) ; } catch ( Exception e ) { } } return listDev ; }
Retrieves the list of devices from an established SSLSocket .