Upload
fractels
View
219
Download
0
Embed Size (px)
Citation preview
7/31/2019 Mina in Real Life ASEU-2009
1/36
7/31/2019 Mina in Real Life ASEU-2009
2/36
Schedule
Introduction to MINA S imple Us e Ca s e s
A more complex Use Case
Do's and Don'ts
Summary
Q&A
7/31/2019 Mina in Real Life ASEU-2009
3/36
The Business part
public class EchoProtocolHandler extends IoHandlerAdapter {
/**
* This is where we handle incoming messages
*/
public void m e s s a g e R e c e i v e d (IoSe ss ion se ss ion, Object mes sa ge)
throws Exception {
// Write the received data back to remote peer
s es s ion .write(((IoBuffer) me s s ag e).du plica te());
}
}
7/31/2019 Mina in Real Life ASEU-2009
4/36
Key concepts
7/31/2019 Mina in Real Life ASEU-2009
5/36
How it works...
7/31/2019 Mina in Real Life ASEU-2009
6/36
S imple us e ca s e s
7/31/2019 Mina in Real Life ASEU-2009
7/36
A Simple TCP server : EchoServer Based on TCP
Multi-Users
Should be fast
Returns what the users sent without
modification
Ok, let's code it !
7/31/2019 Mina in Real Life ASEU-2009
8/36
The echo server
7/31/2019 Mina in Real Life ASEU-2009
9/36
The Business part
public class EchoProtocolHandler extends IoHandlerAdapter {
/**
* This is where we handle incoming messages
*/
public void m e s s a g e R e c e i v e d (IoSe ss ion se ss ion, Object mes sa ge)
throws Exception {
// Write the received data back to remote peer
s es s ion .write(((IoBuffer) me s s ag e).du plica te());
}
}
7/31/2019 Mina in Real Life ASEU-2009
10/36
And that's it !
We ha ve cre a te d a S ocke tAcce ptor Then we associated a handler to it
And accepted incoming connections
Last, we implemented the logic in the
Handler, in the messageReceived()
method.
7/31/2019 Mina in Real Life ASEU-2009
11/36
What do we have ?
A multithreaded server Accepting many parallel clients
Roughly 4 lines of code !
We ca n e xte nde d the s e rve r e a s ily
For instance, adding a logger
Handling more messages
Or adding SSL support
7/31/2019 Mina in Real Life ASEU-2009
12/36
Adding a logging filter
public static void main(String[] args) throws Exception {
7/31/2019 Mina in Real Life ASEU-2009
13/36
Another simple Use Case
NTP Server UDP (port 123)
Fixed Message size
Binary protocol
Stateless
The code...
7/31/2019 Mina in Real Life ASEU-2009
14/36
A more complex
us e ca s e
7/31/2019 Mina in Real Life ASEU-2009
15/36
A more complex Use Case
Apache Directory Server TCP and UDP
Simple or Two levels protocols
Bina ry me s s a ge s Multiple handlers
Potentially hundred of thousands
connections Has to be fast
7/31/2019 Mina in Real Life ASEU-2009
16/36
Handle many protocols
LDAP (TCP) Kerberos (TCP and UDP)
NTP (UDP)
DHCP (UDP)
DNS (TCP and UDP)
ChangePassword
7/31/2019 Mina in Real Life ASEU-2009
17/36
LDAP protocol
Binary protocol Defined using ASN.1
BER encoded
TCP bas e d
Connected
More than one message type
7/31/2019 Mina in Real Life ASEU-2009
18/36
7/31/2019 Mina in Real Life ASEU-2009
19/36
Decoding
Problem : it's a 2 level protocol TLVs
Ldap
TLV means Type/Length/Value Each of those three elements can be
longer than one byte
A Value can contains other TLVs
7/31/2019 Mina in Real Life ASEU-2009
20/36
LDAP messages
10 different requests Bind, Unbind, Abandon, Add, Compare,
Delete, Modify, ModifyDN, Search,
Extended 11 different responses
Bind, SearchResEntry, SearchResDone,
S e a rchRe s Re f, Add, Compare , De le te ,Modify, ModifyDN, Extended,
Intermediate
7/31/2019 Mina in Real Life ASEU-2009
21/36
Server Side
The chain will contain the SSL filter,plus an executor, and the Ldap
protocol codec
We may have expensive requests We want more than one handler
Ea ch s e s s ion conta ins us e r's da ta s
7/31/2019 Mina in Real Life ASEU-2009
22/36
7/31/2019 Mina in Real Life ASEU-2009
23/36
Handlersc l a s s L d a p P r o t o c o l Ha n d l e r e x t e n d s De mu x i n g I o Ha n d l e r
{
. . .
p u b l i c v o i d me s s a g e Re c e i v e d ( I o S e s s i o n s e s s i o n , Ob j e c t me s s a g e )
{
. . . / / S S L a n d c o n t r o l s Ha n d l i n g
s u p e r . me s s a g e Re c e i v e d ( s e s s i o n , me s s a g e ) ;
}
. . .}
p u b l i c v o i d me s s a g e Re c e i v e d ( I o S e s s i o n s e s s i o n , Ob j e c t me s s a g e )
{
Me s s a g e Ha n d l e r < Ob j e c t > h a n d l e r =
f i n d Re c e i v e d Me s s a g e Ha n d l e r ( me s s a g e . g e t Cl a s s ( ) ) ;
i f ( h a n d l e r ! = n u l l ) {
h a n d l e r . h a n d l e Me s s a g e ( s e s s i o n , me s s a g e ) ;
} e l s e {
t h r o w n e w Un k n o wn Me s s a g e Ty p e Ex c e p t i o n ( . . . ) ; ;
}
}
7/31/2019 Mina in Real Life ASEU-2009
24/36
Back to basic...
7/31/2019 Mina in Real Life ASEU-2009
25/36
What about XML ?
Tagged language Size is unknown
Parser are a bit a pain to use at this
point
Seems like XML is the Lingua Franca
those days... a language used by people of diverse speech to communicate with one another,
often a basic form of speech with simplified grammar.
7/31/2019 Mina in Real Life ASEU-2009
26/36
7/31/2019 Mina in Real Life ASEU-2009
27/36
An XML stripper server
We want to extract the message in anXML message, and return it to the user
The message can be big
The decoder is the main concern...
We have to validate the data before
sending it to the handler.
7/31/2019 Mina in Real Life ASEU-2009
28/36
XML serverpublic static void main( String[] args ) throws Exception {
IoHandler xmlStripperProtocolHandler = new XmlStripperProtocolHandler();
SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress( true );
a cce ptor.s e tHa ndle r( xmlS trippe rProtocolHandler );
// Add the codec filter
a cce ptor.ge tFilterCha in().add La s t( "code c",
new ProtocolCodecFilter( new XmlStripperProtocolCodecFactory() ) );
// S ta rt the lis te ne r
acceptor.bind(new InetSocketAddress(IP_PORT_DEFAULT));
}
7/31/2019 Mina in Real Life ASEU-2009
29/36
XML handler
public void messageReceived( IoSession session, Object message )
{
Document document = (Document)message;
// S trip the XML from the
String result = getChildren( document.getFirstChild() );
session.write( result );
}
7/31/2019 Mina in Real Life ASEU-2009
30/36
XML codec factorypublic c la s s XmlS trippe rProtocolCode cFa ctory imple me nts ProtocolCode cFa ctory
{
rS30B pw q#u o+3C33 $pw q#u @qrStrS @w vSory
7/31/2019 Mina in Real Life ASEU-2009
31/36
XML decoderprotected boolean doDecode( IoSession session, IoBuffer ioBuffer,
ProtocolDecoderOutput decoderOutput ) {
...
dec ode rOutput.write( pa rse rXML( da ta ) );
...
}
public Object parserXML( IoBuffer xmlBuffer ) {
byte[] data = new byte[xmlBuffer.limit()];
xmlBuffer.get( data );
String xml = new String(data).trim();
Document document = DocumentBuilderFactory.newInstance().
newDocumentBuilder().parse(
new ByteArrayInputStream( xml.getBytes() ) );
return (document);
}
7/31/2019 Mina in Real Life ASEU-2009
32/36
Do's and Don'ts
7/31/2019 Mina in Real Life ASEU-2009
33/36
Do's !!!
Follow the KISS principle Keep the chain short
Do not use an executor if not needed
Tune the number of IoProcessors
Use only one codec filter
If you have a problem, then yourcode c/ha ndle r proba bly s ucks ...
7/31/2019 Mina in Real Life ASEU-2009
34/36
DON'Ts !!!
Don't use the logging filter. Use Log4j. Your filter must be thread-safe
Don't expect that you will receive data
in one single block
Don't forget about the negative impact
Nagle's algorithm has on performance
Don't use Direct buffers unless
a bs olute ly ne e de d...
7/31/2019 Mina in Real Life ASEU-2009
35/36
7/31/2019 Mina in Real Life ASEU-2009
36/36