🎨 Replaced IdentityManager with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityManagerImpl.kt
1 /*
2  * Sone - IdentityManagerImpl.java - Copyright Â© 2010–2019 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
20 import com.google.common.eventbus.*
21 import com.google.inject.*
22 import net.pterodactylus.util.service.*
23 import java.util.logging.*
24 import java.util.logging.Logger.*
25
26 /**
27  * The identity manager takes care of loading and storing identities, their
28  * contexts, and properties. It does so in a way that does not expose errors via
29  * exceptions but it only logs them and tries to return sensible defaults.
30  *
31  *
32  * It is also responsible for polling identities from the Web of Trust plugin
33  * and sending events to the [EventBus] when [Identity]s and
34  * [OwnIdentity]s are discovered or disappearing.
35  */
36 @Singleton
37 class IdentityManagerImpl @Inject constructor(
38                 private val eventBus: EventBus,
39                 private val webOfTrustConnector: WebOfTrustConnector,
40                 private val identityLoader: IdentityLoader
41 ) : AbstractService("Sone Identity Manager", false), IdentityManager {
42
43         private val currentOwnIdentities = mutableSetOf<OwnIdentity>()
44
45         override val isConnected: Boolean
46                 get() = notThrowing { webOfTrustConnector.ping() }
47
48         override val allOwnIdentities: Set<OwnIdentity>
49                 get() = synchronized(currentOwnIdentities) {
50                         currentOwnIdentities.toSet()
51                 }
52
53         override fun serviceRun() {
54                 var oldIdentities = mapOf<OwnIdentity, Collection<Identity>>()
55
56                 while (!shouldStop()) {
57                         try {
58                                 val currentIdentities = identityLoader.loadIdentities()
59
60                                 val identityChangeEventSender = IdentityChangeEventSender(eventBus, oldIdentities)
61                                 identityChangeEventSender.detectChanges(currentIdentities)
62
63                                 oldIdentities = currentIdentities
64
65                                 synchronized(currentOwnIdentities) {
66                                         currentOwnIdentities.clear()
67                                         currentOwnIdentities.addAll(currentIdentities.keys)
68                                 }
69                         } catch (wote1: WebOfTrustException) {
70                                 logger.log(Level.WARNING, "WoT has disappeared!", wote1)
71                         }
72
73                         /* wait a minute before checking again. */
74                         sleep((60 * 1000).toLong())
75                 }
76         }
77
78 }
79
80 private val logger: Logger = getLogger(IdentityManagerImpl::class.java.name)
81
82 private fun notThrowing(action: () -> Unit): Boolean =
83                 try {
84                         action()
85                         true
86                 } catch (e: Exception) {
87                         false
88                 }