- Tigase is an open source implementation of XMPP in Java, another cool project besides OpenFire.
- Tigase’s GC performance is better than OpenFire’s in a single machine with 500,000 people online at the same time under actual laboratory pressure, and OpenFire’s network layer code borrowings from tigase.
The following content, written during the dissection of Tigase, was used in version 5.2.1 (July 2014).
component
Tigase is a big component box where everything is basically made up of components. Session Manager, Message Router, C2S Connection Manager… All components.
The ServerComponent interface is a minimal interface, but requires a lot of code to write. Tigase provides a lot of off-the-shelf code to reduce the need to implement the interface from scratch.
For example, extends AbstractMessageReceiver can directly generate a component that processes a message. You only need to implement a processPacket method. Implement getDefaults method to handle system variables. Implement the setProperties method to set some variables to use.
multithreading
The processPacket method, which can throw logic to run on different cpus to take advantage of the multicore server. The processingThreads method returns how many cpus were used to process.
While overwriting processingThreads, you need to override the hashCodeForPacket method to ensure that the data for the same user is processed on the same CPU to ensure that messages are not out of order.
The timer
The everySecond everyMinute everyHour logic can be overridden to act as a timer.
There are also two other methods you can override to specify tasks to be completed at a fixed time:
addTimerTask(TimerTask task, long delay, TimeUnit unit);
addTimerTask(TimerTask task, long delay);
Overriding the initializationComplete method, tigase will be called at a specific time, specifically, after all the preparatory work has been done, probably to start a connection or something.
statistics
Overwrite getStatistics, using StatisticsList to hold statistics.
Component life cycle
A component is called as follows:
- The component is loaded and an instance is created using the public constructor with no arguments.
- setName
- start
- getDefaults
- setProperties
- InitializationCompleted is called last, usually to start the socket connection.
ConnectionManager
ConnectionManager is an abstract class that inherits from AbstractMessageReceiver (the previous coding-free component implementation), such as the C2S component.
If you extend the class, you need to know where the data comes from: if it comes from MessageRouter, the abstract void processPacket(Packet Packet) method is called; If the source is a network connection, the abstract Queue processSocketData(XMPPIOService serv) method is called.
The shelf
- ComponentRegistrator: A component of its own, dedicated to registering various components. Registr – > addComponent
- MessageReceiver: a component in itself that is dedicated to receiving messages. Also called Router because message content flows through such components.
- XMPPService: is itself a component that displays the name of serviceDiscovery. No big effect.
- AbstractMessageReceiver: an advanced component based on MessageReceiver and XMPPService that implements a lot of details at the same time and sends and receives from multiple threads.
Start the
- MessageRouter setConfig, enter ConfiguratorAbstract – AbstractComponentRegistrator addComponent.
- Back to ConfiguratorAbstract’s componentAdded, and then setup(Component).
- Component. setProperties Go to messageRouter’s setProperties.
- Comp. InitializationCompleted various socket () launched the connectionManager.
network
- ConnectionManager coordinates both ConnectionOpenThreads and SocketThreads.
- ConnectionOpenThread is a network-layer implementation that operates with selector. It’s responsible for Selector. Open.
- IOService provides a thread-safe call method, which XMPPIOService inherits and holds connection information, one IOService per connection.
- SocketThread, when instantiated, starts multiple threads, eyeing the selector at the same time. Responsible for data processing of each identified IOService.
- Implement the Accept method of the ConnectionOpenListener interface to receive SocketChannel, assemble IOService, and deliver it to SocketThread for processing.
- ConnectionManager logs all connections using ConcurrentHashMap.
Bits and pieces
- Add data AbstractMessageReceiver. AddPacket toward their in_queue, is blocked, if it is full of trouble.
- AbstractMessageReceiver. Add data addPacketNB toward their in_queue, non-blocking, and on a difference is that a is put an offer to the queue.
- AbstractMessageReceiver. AddPackets to a pile of data.
- All the data in the in_queue is processed by the processPacket method.
- For addOutPacket.
- All data in the out_queue is thrown to the parent’s in_queue by default, and if no parent is thrown to its own in_queue.
- All in_queue data is handled by the concrete implementation of processPacket.