Search in sources :

Example 21 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class StatisticsModule method start.

public void start() {
    // Retrieve instance of StatisticsManager
    statisticsManager = StatisticsManager.getInstance();
    // Register a packet listener so that we can track packet traffic.
    packetInterceptor = new PacketInterceptor() {

        public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) {
            // Only track processed packets so that we don't count them twice.
            if (processed) {
                packetCount.incrementAndGet();
            }
        }
    };
    InterceptorManager.getInstance().addInterceptor(packetInterceptor);
    // Register all statistics.
    addServerToServerStatistic();
    addActiveSessionsStatistic();
    addPacketStatistic();
}
Also used : Packet(org.xmpp.packet.Packet) PacketInterceptor(org.jivesoftware.openfire.interceptor.PacketInterceptor) Session(org.jivesoftware.openfire.session.Session)

Example 22 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class OutgoingSessionPromise method init.

private void init() {
    serversCache = CacheFactory.createCache(RoutingTableImpl.S2S_CACHE_NAME);
    routingTable = XMPPServer.getInstance().getRoutingTable();
    // Create a pool of threads that will process queued packets.
    int maxThreads = JiveGlobals.getIntProperty(ConnectionSettings.Server.QUEUE_MAX_THREADS, 20);
    int queueSize = JiveGlobals.getIntProperty(ConnectionSettings.Server.QUEUE_SIZE, 50);
    if (maxThreads < 10) {
        // Ensure that the max number of threads in the pool is at least 10
        maxThreads = 10;
    }
    threadPool = new ThreadPoolExecutor(maxThreads / 4, maxThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(queueSize), new ThreadPoolExecutor.CallerRunsPolicy());
    // Start the thread that will consume the queued packets. Each pending packet will
    // be actually processed by a thread of the pool (when available). If an error occurs
    // while creating the remote session or sending the packet then a packet with error 502
    // will be sent to the sender of the packet
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!shutdown) {
                try {
                    if (threadPool.getActiveCount() < threadPool.getMaximumPoolSize()) {
                        // Wait until a packet is available
                        final Packet packet = packets.take();
                        boolean newProcessor = false;
                        PacketsProcessor packetsProcessor;
                        String domain = packet.getTo().getDomain();
                        synchronized (domain.intern()) {
                            packetsProcessor = packetsProcessors.get(domain);
                            if (packetsProcessor == null) {
                                packetsProcessor = new PacketsProcessor(OutgoingSessionPromise.this, domain);
                                packetsProcessors.put(domain, packetsProcessor);
                                newProcessor = true;
                            }
                            packetsProcessor.addPacket(packet);
                        }
                        if (newProcessor) {
                            // Process the packet in another thread
                            threadPool.execute(packetsProcessor);
                        }
                    } else {
                        // No threads are available so take a nap :)
                        Thread.sleep(200);
                    }
                } catch (InterruptedException e) {
                // Do nothing
                } catch (Exception e) {
                    Log.error(e.getMessage(), e);
                }
            }
        }
    }, "Queued Packets Processor");
    thread.setDaemon(true);
    thread.start();
}
Also used : Packet(org.xmpp.packet.Packet) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 23 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class Workgroup method sendOccupantsInfo.

/**
     * Sends information to the agent that requested it about the occupants in the specified
     * room. If the room does no longer exist then no information will be returned. This means
     * that the chat should be happening at the moment of the query.
     *
     * @param packet the request sent by the agent.
     * @param roomID the id of the room that the agent is requesting information
     */
public void sendOccupantsInfo(IQ packet, String roomID) {
    IQ statusPacket = IQ.createResultIQ(packet);
    Element occupantsInfo = statusPacket.setChildElement("occupants-info", "http://jivesoftware.com/protocol/workgroup");
    occupantsInfo.addAttribute("roomID", roomID);
    Map<Packet, java.util.Date> packets = transcripts.get(roomID);
    if (packets != null) {
        Collection<String> processed = new ArrayList<String>();
        for (Packet p : packets.keySet()) {
            if (p instanceof Presence) {
                Presence presence = (Presence) p;
                // Get the JID of the presence's user
                String userJID = presence.getChildElement("x", "http://jabber.org/protocol/muc#user").element("item").attributeValue("jid");
                // occupant joined the room
                if (!processed.contains(userJID)) {
                    processed.add(userJID);
                    Element occupantInfo = occupantsInfo.addElement("occupant");
                    occupantInfo.addElement("jid").setText(userJID);
                    occupantInfo.addElement("nickname").setText(presence.getFrom().getResource());
                    occupantInfo.addElement("joined").setText(UTC_FORMAT.format(packets.get(p)));
                }
            }
        }
    }
    // Send the response
    send(statusPacket);
}
Also used : Packet(org.xmpp.packet.Packet) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) ArrayList(java.util.ArrayList) Presence(org.xmpp.packet.Presence) Date(java.sql.Date)

Example 24 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class MainInterceptor method presencePush.

/**
	 * Just pushes a available presence, we need this for GAJIM Client as it does not push available presence after
	 * registering We also wait a little so after register transport is on users roster. Really didnt wanted this here
	 * but doesnt really belong anywhere else
	 * 
	 * @param to
	 * @param from
	 * @param delay MS until the job should be done
	 */
private void presencePush(final JID to, final JID from, int delay) {
    TimerTask pushPresenceTask = new TimerTask() {

        @Override
        public void run() {
            PacketRouter router = server.getPacketRouter();
            Packet presence = new Presence();
            presence.setTo(to);
            presence.setFrom(from);
            router.route(presence);
        }
    };
    Timer timer = new Timer();
    timer.schedule(pushPresenceTask, delay);
}
Also used : Packet(org.xmpp.packet.Packet) TimerTask(java.util.TimerTask) Timer(java.util.Timer) PacketRouter(org.jivesoftware.openfire.PacketRouter) Presence(org.xmpp.packet.Presence)

Example 25 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class BaseMUCTransport method processPacket.

/**
     * Handles all incoming message stanzas.
     *
     * @param packet The message packet to be processed.
     * @return list of packets that will be sent back to the message sender.
     */
private List<Packet> processPacket(Message packet) {
    Log.debug("Received message packet: " + packet.toXML());
    List<Packet> reply = new ArrayList<Packet>();
    JID from = packet.getFrom();
    JID to = packet.getTo();
    try {
        TransportSession<B> session = getTransport().getSessionManager().getSession(from);
        if (!session.isLoggedIn()) {
            Message m = new Message();
            m.setError(Condition.service_unavailable);
            m.setTo(from);
            m.setFrom(getJID());
            m.setBody(LocaleUtils.getLocalizedString("gateway.base.notloggedin", "kraken", Arrays.asList(getTransport().getType().toString().toUpperCase())));
            reply.add(m);
        } else if (to.getNode() == null) {
        // Message to gateway itself.  Throw away for now.
        } else {
            MUCTransportSession mucSession = session.getMUCSessionManager().getSession(to.getNode());
            if (packet.getBody() != null) {
                if (to.getResource() == null) {
                    // Message to room
                    mucSession.sendMessage(packet.getBody());
                } else {
                    // Private message
                    mucSession.sendPrivateMessage(to.getResource(), packet.getBody());
                }
            } else {
                if (packet.getSubject() != null) {
                    // Set topic of room
                    mucSession.updateTopic(packet.getSubject());
                }
            }
        }
    } catch (NotFoundException e) {
        Log.debug("Unable to find session.");
        Message m = new Message();
        m.setError(Condition.service_unavailable);
        m.setTo(from);
        m.setFrom(getJID());
        m.setBody(LocaleUtils.getLocalizedString("gateway.base.notloggedin", "kraken", Arrays.asList(getTransport().getType().toString().toUpperCase())));
        reply.add(m);
    }
    return reply;
}
Also used : Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) ArrayList(java.util.ArrayList) NotFoundException(org.jivesoftware.util.NotFoundException)

Aggregations

Packet (org.xmpp.packet.Packet)33 JID (org.xmpp.packet.JID)17 Element (org.dom4j.Element)16 Message (org.xmpp.packet.Message)16 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)10 IQ (org.xmpp.packet.IQ)9 Presence (org.xmpp.packet.Presence)7 NotFoundException (org.jivesoftware.util.NotFoundException)5 PacketInterceptor (org.jivesoftware.openfire.interceptor.PacketInterceptor)3 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)3 Session (org.jivesoftware.openfire.session.Session)3 Date (java.sql.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 DefaultElement (org.dom4j.tree.DefaultElement)2 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)2