Update year in copyright lines
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityManagerImpl.java
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 static java.util.logging.Logger.getLogger;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.freenet.plugin.PluginException;
31 import net.pterodactylus.util.service.AbstractService;
32
33 import com.google.common.collect.Sets;
34 import com.google.common.eventbus.EventBus;
35 import com.google.inject.Inject;
36 import com.google.inject.Singleton;
37
38 /**
39  * The identity manager takes care of loading and storing identities, their
40  * contexts, and properties. It does so in a way that does not expose errors via
41  * exceptions but it only logs them and tries to return sensible defaults.
42  * <p>
43  * It is also responsible for polling identities from the Web of Trust plugin
44  * and sending events to the {@link EventBus} when {@link Identity}s and
45  * {@link OwnIdentity}s are discovered or disappearing.
46  */
47 @Singleton
48 public class IdentityManagerImpl extends AbstractService implements IdentityManager {
49
50         /** The logger. */
51         private static final Logger logger = getLogger(IdentityManagerImpl.class.getName());
52
53         /** The event bus. */
54         private final EventBus eventBus;
55
56         private final IdentityLoader identityLoader;
57
58         /** The Web of Trust connector. */
59         private final WebOfTrustConnector webOfTrustConnector;
60
61         /** The currently known own identities. */
62         private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
63
64         /**
65          * Creates a new identity manager.
66          *
67          * @param eventBus
68          *            The event bus
69          * @param webOfTrustConnector
70          *            The Web of Trust connector
71          */
72         @Inject
73         public IdentityManagerImpl(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, IdentityLoader identityLoader) {
74                 super("Sone Identity Manager", false);
75                 this.eventBus = eventBus;
76                 this.webOfTrustConnector = webOfTrustConnector;
77                 this.identityLoader = identityLoader;
78         }
79
80         //
81         // ACCESSORS
82         //
83
84         /**
85          * Returns whether the Web of Trust plugin could be reached during the last
86          * try.
87          *
88          * @return {@code true} if the Web of Trust plugin is connected,
89          *         {@code false} otherwise
90          */
91         @Override
92         public boolean isConnected() {
93                 try {
94                         webOfTrustConnector.ping();
95                         return true;
96                 } catch (PluginException pe1) {
97                         /* not connected, ignore. */
98                         return false;
99                 }
100         }
101
102         /**
103          * Returns all own identities.
104          *
105          * @return All own identities
106          */
107         @Override
108         public Set<OwnIdentity> getAllOwnIdentities() {
109                 synchronized (currentOwnIdentities) {
110                         return new HashSet<>(currentOwnIdentities);
111                 }
112         }
113
114         //
115         // SERVICE METHODS
116         //
117
118         /**
119          * {@inheritDoc}
120          */
121         @Override
122         protected void serviceRun() {
123                 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<>();
124
125                 while (!shouldStop()) {
126                         try {
127                                 Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
128
129                                 IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
130                                 identityChangeEventSender.detectChanges(currentIdentities);
131
132                                 oldIdentities = currentIdentities;
133
134                                 synchronized (currentOwnIdentities) {
135                                         currentOwnIdentities.clear();
136                                         currentOwnIdentities.addAll(currentIdentities.keySet());
137                                 }
138                         } catch (WebOfTrustException wote1) {
139                                 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
140                         }
141
142                         /* wait a minute before checking again. */
143                         sleep(60 * 1000);
144                 }
145         }
146
147 }