aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp-wip/src/main/java/com/juick/components/XMPPRouter.java
blob: 47e76cd81cd9e3864f6ca3741305f3e7c640bf56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (C) 2008-2017, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick.components;

import com.juick.components.s2s.BasicXmppSession;
import com.juick.xmpp.Message;
import com.juick.xmpp.StreamComponentServer;
import com.juick.xmpp.StreamListener;
import com.juick.xmpp.extensions.JuickMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.xmlpull.v1.XmlPullParserException;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Stanza;
import rocks.xmpp.util.XmppUtils;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @author ugnich
 */
@Component
public class XMPPRouter implements Message.MessageListener, AutoCloseable, StreamListener {
    private static final Logger logger = LoggerFactory.getLogger(XMPPRouter.class);

    @Inject
    private ExecutorService service;

    private final List<StreamComponentServer> connections = Collections.synchronizedList(new ArrayList<>());

    private ServerSocket listener;

    @Inject
    private BasicXmppSession session;

    @Value("${router_port:5347}")
    private int routerPort;

    @PostConstruct
    public void init() {

        logger.info("component router initialized");
        service.submit(() -> {
            try {
                listener = new ServerSocket(routerPort);
                logger.info("component router listening on {}", routerPort);
                while (!listener.isClosed()) {
                    if (Thread.currentThread().isInterrupted()) break;
                    Socket socket = listener.accept();
                    service.submit(() -> {
                        try {
                            StreamComponentServer client = new StreamComponentServer(socket.getInputStream(), socket.getOutputStream(), "secret");
                            addConnectionIn(client);
                            client.addChildParser(new JuickMessage());
                            client.addListener((Message.MessageListener) this);
                            client.addListener((StreamListener) this);
                            client.connect();
                        } catch (IOException e) {
                            logger.error("component error", e);
                        } catch (XmlPullParserException e) {
                            e.printStackTrace();
                        }
                    });
                }
            } catch (SocketException e) {
                // shutdown
            } catch (IOException e) {
                logger.warn("io exception", e);
            }
        });
    }

    @Override
    public void close() throws Exception {
        if (!listener.isClosed()) {
            listener.close();
        }
        synchronized (getConnections()) {
            for (Iterator<StreamComponentServer> i = getConnections().iterator(); i.hasNext(); ) {
                StreamComponentServer c = i.next();
                c.logoff();
                i.remove();
            }
        }
        service.shutdown();
        logger.info("XMPP router destroyed");
    }

    private void addConnectionIn(StreamComponentServer c) {
        synchronized (getConnections()) {
            getConnections().add(c);
        }
    }

    private void sendOut(Stanza s) {
        try {
            StringWriter stanzaWriter = new StringWriter();
            XMLStreamWriter xmppStreamWriter = XmppUtils.createXmppStreamWriter(
                    session.getConfiguration().getXmlOutputFactory().createXMLStreamWriter(stanzaWriter));
            session.createMarshaller().marshal(s, xmppStreamWriter);
            xmppStreamWriter.flush();
            xmppStreamWriter.close();
            String xml = stanzaWriter.toString();
            logger.info("XMPPRouter (out): {}", xml);
            sendOut(s.getTo().getDomain(), xml);
        } catch (XMLStreamException | JAXBException e1) {
            logger.info("jaxb exception", e1);
        }
    }

    private void sendOut(String hostname, String xml) {
        boolean haveAnyConn = false;

        StreamComponentServer connOut = null;
        synchronized (getConnections()) {
            for (StreamComponentServer c : getConnections()) {
                if (c.to != null && c.to.getDomain().equals(hostname)) {
                    if (c.isLoggedIn()) {
                        connOut = c;
                        break;
                    }
                }
            }
        }
        if (connOut != null) {
            connOut.send(xml);
            return;
        }
        logger.error("component unavailable: {}", hostname);

    }

    public List<StreamComponentServer> getConnections() {
        return connections;
    }

    private Stanza parse(String xml) {
        try {
            Unmarshaller unmarshaller = session.createUnmarshaller();
            return (Stanza)unmarshaller.unmarshal(new StringReader(xml));
        } catch (JAXBException e) {
            logger.error("JAXB exception", e);
        }
        return null;
    }
    @Override
    public void onMessage(Message message) {
        sendOut(parse(message.toString()));
    }

    @Override
    public void ready() {

    }

    @Override
    public void fail(Exception e) {

    }

    @Override
    public boolean filter(Jid jid, Jid jid1) {
        return false;
    }
}