package Ġcom . ust ad mobile . retrie ver Ċ Ċ import Ġcom . ust ad mobile . retrie ver . db . entities . Loc ally Stored File Ċ import Ġcom . ust ad mobile . retrie ver . db . Retriever Database Ċ import Ġcom . ust ad mobile . retrie ver . fetcher . Local Peer Fetcher Ċ import Ġcom . ust ad mobile . retrie ver . fetcher . Origin Server Fetcher Ċ import Ġcom . ust ad mobile . retrie ver . responder . Availability Responder Ċ import Ġcom . ust ad mobile . retrie ver . responder . Z ipped Items Responder Ċ import Ġfi . iki . el onen . router . Router Nano HTTP D Ċ import Ġjava . io . File Ċ import Ġkotlinx . serialization . json . Json Ċ import Ġcom . ust ad mobile . door . ext . with Door Transaction Async Ċ import Ġcom . ust ad mobile . retrie ver . ext . as Loc ally Stored File Async Ċ import Ġcom . ust ad mobile . retrie ver . responder . Ping Responder Ċ import Ġkotlinx . coroutines . Coroutine Scope Ċ import Ġkotlinx . coroutines . Dispatch ers Ċ import Ġkotlinx . coroutines . Comp letable Deferred Ċ import Ġkotlinx . coroutines . launch Ċ Ċ abstract Ġclass ĠRetrie ver Common Jvm ( ĊĠĠĠ Ġdb : ĠRetrie ver Database , ĊĠĠĠ Ġconfig : ĠRetrie ver Config , ĊĠĠĠ Ġavailability Checker : ĠAvailability Checker , ĊĠĠĠ Ġorigin Server Fetcher : ĠOrigin Server Fetcher , ĊĠĠĠ Ġlocal Peer Fetcher : ĠLocal Peer Fetcher , ĊĠĠĠ Ġprotected Ġval Ġjson : ĠJson , ĊĠĠĠ Ġavailability ManagerFactory : ĠAvailability ManagerFactory , ĊĠĠĠ Ġp inger : ĠP inger , ĊĠĠĠ Ġretrie ver Coroutine Scope : ĠCor outine Scope , Ċ ): ĠRetrie ver Common ( ĊĠĠĠ Ġdb , Ġconfig , Ġavailability Checker , Ġorigin Server Fetcher , Ġlocal Peer Fetcher , Ġavailability ManagerFactory , Ġp inger , ĊĠĠĠ Ġretrie ver Coroutine Scope Ċ ) Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġserver Comp letable Ġ= ĠComp letable Deferred < Router Nano HTTP D >() ĊĊĠĠĠ Ġ@ Volatile ĊĠĠĠ Ġprivate Ġvar Ġrunning Server : ĠRouter Nano HTTP D ? Ġ= Ġnull ĊĊĠĠĠ Ġoverride Ġfun Ġstart () Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . start () ĊĠĠĠĠĠĠĠ Ġretrie ver Coroutine Scope . launch ( Dispatch ers . IO ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġchosen Port Ġ= Ġchoose Port () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġnano Http d Server Ġ= ĠRouter Nano HTTP D ( chosen Port ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnano Http d Server . add Route ("/ availability ", ĠAvailability Responder :: class . java , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdb , Ġjson ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnano Http d Server . add Route ("/ z ipped ", ĠZ ipped Items Responder :: class . java , Ġdb ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnano Http d Server . start () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġnano Http d Server . add Route ("/ ping ", ĠPing Responder :: class . java , Ġthis @ Retriever Common Jvm ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġrunning Server Ġ= Ġnano Http d Server ĊĠĠĠĠĠĠĠĠĠĠĠ Ġserver Comp letable . complete ( nano Http d Server ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġ/** ĊĠĠĠĠ Ġ* ĠUse Ġlogic Ġon Ġthe Ġunderlying Ġplatform Ġ( e . g . Ġsettings Ġstorage Ġon ĠAndroid ) Ġto Ġdetermine Ġthe Ġport Ġto Ġstart Ġon ĊĠĠĠĠ Ġ*/ ĊĠĠĠ Ġprotected Ġabstract Ġsuspend Ġfun Ġchoose Port (): ĠInt ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġadd Files ( files : ĠList < Local FileInfo >) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġlocally Stored Files Ġ= Ġfiles . map Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfile Ġ= ĠFile ( it . filePath ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif (! file . exists ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠIllegalArgumentException (" add Files : Ġ${ file . absolute Path } Ġdoes Ġnot Ġexist !") ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġfile . as Loc ally Stored File Async ( it . origin Url , Ġconfig . integrity Checksum Types , Ġit . checksum s ) ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġdb . loc ally Stored File Dao . insert List Async ( loc ally Stored Files ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun ĠgetAll Loc ally Stored Files (): ĠList < Loc ally Stored File >{ ĊĠĠĠĠĠĠĠ Ġreturn Ġdb . loc ally Stored File Dao . findAll Loc ally Stored Files () ĊĠĠĠ Ġ} ĊĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġget Loc ally Stored Files By Urls ( urls : ĠList < String >): ĠList < Loc ally Stored File > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġdb . loc ally Stored File Dao . find Loc ally Stored Files By Url List ( urls ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġdelete Files By Url ( urls : ĠList < String >) Ġ{ ĊĠĠĠĠĠĠĠ Ġ// TODO : ĠChunk Ġthis Ġinto Ġbatches Ġof Ġ 1 0 0 ĊĠĠĠĠĠĠĠ Ġdb . with Door Transaction Async ( Retriever Database :: class ) Ġ{ Ġtx Db Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġlocally Stored Files Ġ= Ġtx Db . loc ally Stored File Dao . find Loc ally Stored Files By Url List ( urls ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlocally Stored Files . forEach Ġ{ Ġstored File Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġstored File . ls f FilePath ?. let Ġ{ ĠFile ( it ) Ġ} ?. delete () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġtx Db . loc ally Stored File Dao . remove File ( stored File . loc ally Stored File Uid ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtx Db . download Job Item Dao . delete By Url List ( urls ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġlistening Port (): ĠInt Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġserver Comp letable . await (). list ening Port ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġinternal Ġsuspend Ġfun Ġawait Server (): ĠRouter Nano HTTP D Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġserver Comp letable . await () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġclose () Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . close () ĊĠĠĠĠĠĠĠ Ġrunning Server ?. stop () ĊĠĠĠ Ġ} Ċ }