configuration()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser 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
 * Araknemu 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 Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.realm;
21
22
import fr.quatrevieux.araknemu.core.BootException;
23
import fr.quatrevieux.araknemu.core.InitializableService;
24
import fr.quatrevieux.araknemu.core.Service;
25
import fr.quatrevieux.araknemu.core.event.EventsSubscriber;
26
import fr.quatrevieux.araknemu.core.event.ListenerAggregate;
27
import fr.quatrevieux.araknemu.core.network.Server;
28
import fr.quatrevieux.araknemu.network.realm.RealmSession;
29
import fr.quatrevieux.araknemu.realm.event.AuthStopped;
30
import org.apache.logging.log4j.Logger;
31
32
import java.util.Collection;
33
34
/**
35
 * Service for handling server authentication
36
 */
37
public final class RealmService implements Service {
38
    private final RealmConfiguration configuration;
39
    private final Server<RealmSession> server;
40
    private final Logger logger;
41
    private final Collection<InitializableService> preloadables;
42
    private final ListenerAggregate dispatcher;
43
    private final Collection<EventsSubscriber> subscribers;
44
45 1
    public RealmService(RealmConfiguration configuration, Server<RealmSession> server, Logger logger, ListenerAggregate dispatcher, Collection<InitializableService> preloadables, Collection<EventsSubscriber> subscribers) {
46 1
        this.configuration = configuration;
47 1
        this.server = server;
48 1
        this.logger = logger;
49 1
        this.dispatcher = dispatcher;
50 1
        this.preloadables = preloadables;
51 1
        this.subscribers = subscribers;
52 1
    }
53
54
    @Override
55
    public void boot() throws BootException {
56 1
        logger.info("Starting realm server on port {}", configuration.port());
57
58 1
        for (EventsSubscriber subscriber : subscribers) {
59
            dispatcher.register(subscriber);
60
        }
61
62 1
        for (InitializableService service : preloadables) {
63 1
            service.init(logger);
64 1
        }
65
66
        try {
67 1
            server.start();
68
        } catch (Exception e) {
69
            throw new BootException("Cannot start realm server", e);
70 1
        }
71
72 1
        logger.info("Realm server started");
73 1
    }
74
75
    @Override
76
    public void shutdown() {
77
        logger.info("Stopping realm service...");
78
79
        try {
80
            server.stop();
81
        } catch (Exception e) {
82
            logger.error("Error during stopping the realm service", e);
83
        }
84
85
        dispatcher.dispatch(new AuthStopped());
86
        logger.info("Realm service stopped");
87
    }
88
89
    public RealmConfiguration configuration() {
90 1
        return configuration;
91
    }
92
93
    public Collection<RealmSession> sessions() {
94
        return server.sessions();
95
    }
96
}
97