// ĠCopyright Ġ 2 0 0 0 - 2 0 1 9 ĠJetBrains Ġs . r . o . ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe ĠLICENSE Ġfile . Ċ package Ġorg . jetbrains . built In WebServer Ċ Ċ import Ġcom . intellij . execution . process . OS Process Handler Ċ import Ġcom . intellij . openapi . project . Project Ċ import Ġcom . intellij . util . Consumer Ċ import Ġcom . intellij . util . io . Connect To Channel Result Ċ import Ġcom . intellij . util . io . add Channel Listener Ċ import Ġcom . intellij . util . io . close And Shutdown EventLoop Ċ import Ġcom . intellij . util . io . connect Retry ing Ċ import Ġcom . intellij . util . net . loop back SocketAddress Ċ import Ġio . netty . bootstrap . Bootstrap Ċ import Ġio . netty . channel . Channel Ċ import Ġorg . jetbrains . concurrency . Async Promise Ċ import Ġorg . jetbrains . concurrency . Promise Ċ import Ġorg . jetbrains . concurrency . catch Error Ċ import Ġorg . jetbrains . concurrency . resolved Promise Ċ import Ġorg . jetbrains . ide . BuiltIn Server Manager Ċ import Ġjava . util . concurrent . atomic . Atomic Reference Ċ Ċ abstract Ġclass ĠSingle Connection Net Service ( project : ĠProject ) Ġ: ĠNet Service ( project ) Ġ{ ĊĠ Ġprotected Ġval Ġprocess Channel Ġ= ĠAtomic Reference < Channel >() ĊĊĠ Ġ@ Volatile ĊĠ Ġprivate Ġvar Ġport Ġ= Ġ- 1 ĊĠ Ġ@ Volatile ĊĠ Ġprivate Ġvar Ġbootstrap : ĠBootstrap ? Ġ= Ġnull ĊĊĠ Ġprotected Ġabstract Ġfun Ġconfigure Bootstrap ( bootstrap : ĠBootstrap , Ġerror Output Consumer : ĠConsumer < String >) ĊĊĠ Ġfinal Ġoverride Ġfun Ġconnect To Process ( promise : ĠAsync Promise < OS Process Handler >, Ġport : ĠInt , Ġprocess Handler : ĠOS Process Handler , Ġerror Output Consumer : ĠConsumer < String >) Ġ{ ĊĠĠĠ Ġval Ġbootstrap Ġ= ĠBuilt In Server Manager . getInstance (). create Client Bootstrap () ĊĠĠĠ Ġconfigure Bootstrap ( bootstrap , Ġerror Output Consumer ) ĊĊĠĠĠ Ġthis . bootstrap Ġ= Ġbootstrap ĊĠĠĠ Ġthis . port Ġ= Ġport ĊĊĠĠĠ Ġval Ġconnect Result Ġ= Ġbootstrap . connect Retry ing ( loop back SocketAddress ( port )) ĊĠĠĠ Ġconnect Result . channel ?. let Ġ{ ĊĠĠĠĠĠ Ġpromise . catch Error Ġ{ ĊĠĠĠĠĠĠĠ Ġprocess Channel . set ( it ) ĊĠĠĠĠĠĠĠ Ġadd Close Listener ( it ) ĊĠĠĠĠĠĠĠ Ġpromise . set Result ( process Handler ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġhandle Errors ( connect Result , Ġpromise ) ĊĠ Ġ} ĊĊĠ Ġprotected Ġfun Ġconnect Again (): ĠPromise < Channel > Ġ{ ĊĠĠĠ Ġval Ġchannel Ġ= Ġprocess Channel . get () ĊĠĠĠ Ġif Ġ( channel Ġ!= Ġnull ) Ġ{ ĊĠĠĠĠĠ Ġreturn Ġresolved Promise ( channel ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġpromise Ġ= ĠAsync Promise < Channel >() ĊĠĠĠ Ġval Ġconnect Result Ġ= Ġbootstrap !!. connect Retry ing ( loop back SocketAddress ( port )) ĊĠĠĠ Ġconnect Result . channel ?. let Ġ{ ĊĠĠĠĠĠ Ġpromise . catch Error Ġ{ ĊĠĠĠĠĠĠĠ Ġprocess Channel . set ( it ) ĊĠĠĠĠĠĠĠ Ġadd Close Listener ( it ) ĊĠĠĠĠĠĠĠ Ġpromise . set Result ( it ) ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġhandle Errors ( connect Result , Ġpromise ) ĊĊĠĠĠ Ġreturn Ġpromise ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġhandle Errors ( connect Result : ĠConnect To Channel Result , Ġpromise : ĠAsync Promise <* >) Ġ{ ĊĠĠĠ Ġconnect Result ĊĠĠĠĠĠ Ġ. handleError ( java . util . function . Consumer Ġ{ ĊĠĠĠĠĠĠĠ Ġpromise . setError ( it ) ĊĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠ Ġ. handle Throwable ( java . util . function . Consumer Ġ{ ĊĠĠĠĠĠĠĠ Ġpromise . setError ( it ) ĊĠĠĠĠĠ Ġ}) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġadd Close Listener ( it : ĠChannel ) Ġ{ ĊĠĠĠ Ġit . close Future (). add Channel Listener Ġ{ ĊĠĠĠĠĠ Ġprocess Channel . compare AndSet ( it . channel (), Ġnull ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġclose Process Connections () Ġ{ ĊĠĠĠ Ġprocess Channel . get AndSet ( null )?. close And Shutdown EventLoop () ĊĠ Ġ} Ċ }