SHARE
TWEET

Untitled

a guest Jan 11th, 2017 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. val socketCancelable = SingleAssignmentCancelable()
  2.  
  3.   def doConnection: Option[TLSConnection] = {
  4.  
  5.     def initiateTLSConnection = scala.concurrent.blocking {
  6.       val socket = new Socket(host, port)
  7.       val bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream))
  8.       TLSConnection(socket, bufferedReader)
  9.     }
  10.  
  11.     try {
  12.       val tLSConnection = initiateTLSConnection
  13.       // our cancelable must be able to cleanly dispose of our connection
  14.       socketCancelable := BooleanCancelable { () =>
  15.         blocking {
  16.           println("closing shit !!!!!")
  17.           try tLSConnection.in.close()
  18.           finally tLSConnection.socket.close()
  19.         }
  20.       }
  21.       if (tLSConnection.in.readLine() == null || tLSConnection.in.read() == -1) {
  22.         log.info(s"still connection unavailable... will retry")
  23.         None
  24.       } else {
  25.         log.info(s"successfully connected")
  26.         Some(tLSConnection)
  27.       }
  28.     } catch {
  29.       case NonFatal(ex) =>
  30.         log.error(ex.getMessage)
  31.         socketCancelable.cancel()
  32.         None
  33.     }
  34.   }
RAW Paste Data
Top