You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Tyler Pitchford <ty...@gmail.com> on 2006/05/14 18:47:59 UTC
[mina] Changing Filters Durning Application Cycles
I can't seem to find an answer to this and I am getting very spotting
results (looks like threading issues). I'm tyring to build a
BitTorrent Filter for MINA, but I can't seem to get everything working
correctly.
To understand that situation, you'll need a little background on the
BT protocol. When connecting to a peer a "handshake" message is sent,
nothing unusual here, right? Well, sadly the handshake doesn't follow
the wire header format. The Handshake uses a singly byte for it's
header and the wire messages use a 4 byte header.
Anyway, to handle it "cleanly" (probably not the fastest, but nicely
architecturally), I developed a PeerWireHandshakeCodecFactory and a
PeerWireCodecFactory. I could just change the header length, once the
handshake was received, but I don't want to mix the handshake / wire
messages, because in my opinion they should be handled separately.
Anyway, I'm currently trying to do this is once the handshake response
is received (handshake part is working correctly), I call:
public void messageReceived(IoSession session, Object message){
if (message instanceof PeerWireHandshakeMessage) {
synchronized(session){
session.getFilterChain().remove("handshakeFilter");
ProtocolCodecFactory codec = new PeerWireCodecFactory();
session.getFilterChain().addFirst("peerWireFilter", new
ProtocolCodecFilter(codec));
}
}
The problem I'm seeing is that the PeerWire Encoders/Decoders never
seem to get called. I've only seen them called one time, while
debugged, which makes me think it's a threading issue. Any help /
advice would be lovely. I'm not opposed to changing the architecture,
if needed.
Cheers,
Tyler
Re: Best Practice for Inter-Session communications
Posted by Trustin Lee <tr...@gmail.com>.
Hi Cinar,
On 5/15/06, Cinar Eren <CE...@orga-systems.com> wrote:
>
>
> Hi Trustin,
>
> thank you for your prompt response. With the update, i meant the examples
> given in "MINA Tutorial" of Must-reads section of "Getting Started".
> For my 3. question; can you please be more elaborate? You mean an
> instance variable of IoHandler? if we dont have a queue structure in our
> classes, does MINA handle this with an internal queue? if more than one
> session tries to send data to one session, no data is lost, right?
>
MINA VM-pipe sessions doesn't have a queue because all messages exchanged
are just method invocations. To insert a queue between a client and a
server, please insert a thread pool filter. Since 0.9.4, the thread pool
filter is added by default, so we can say that we have a queue between a
client and a server. No data is lost.
HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
Re: Best Practice for Inter-Session communications
Posted by Trustin Lee <tr...@gmail.com>.
Hi Cinar,
On 5/15/06, Cinar Eren <CE...@orga-systems.com> wrote:
>
>
> Hi,
>
> I have been working with MINA for the last a few days. First of all, thank
> you very much for such a great tool.
> I have some questions:
> 1. what i understood from the archive, we dont need to worry about
> creating threads for each session; as it is handled by MINA by default. What
> kind of a need makes us create our own ThreadPoolFilter, etc?
>
You might want multiple thread pool filters to implement SEDA. But it's
rare in practice. :)
2. I know that a large tutorial will not be available until Release 1.0, but
> cant you update the Tutorial in "Getting Started" section at least? :-)
>
Do you mean the SWF screencast? Otherwise, what part of the section has a
problem for you?
3. my most important question: what will be the best practice to make
> communications between different IoSessions, i.e. the data taken from one
> session shall be sent to another session. Is it ok for the IoSession to have
> an attribute of type Queue which has a hasMessage method and in this method
> we can try to send the message to the remote side?
>
If the IoSessions are handled by the same IoHandler instance, then you can
simply use the instance variable. Otherwise, your idea looks fine. Or you
could use VM-pipe transport; please refer to the table tennis example.
HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
Re: [mina] Changing Filters Durning Application Cycles
Posted by Tyler Pitchford <ty...@gmail.com>.
It appears what is happening is this: Sometimes the 67 bytes for the
handshake come in on their own (this is when it works), other times
the 67 for the handshake and the 6 for the bitfield are being combined
when processed (this is when it fails). When the ByteBuffer contains
73, the PeerWireHandshakeHandler processed the 67 for the handshake
and returns true, saying it has finished decoding. Since there are
bytes left in the buffer the when control returns to the underlying
CumulativeProtocolDecoder this code is invoked
CumulativeProtocolDecoder
if( !buf.hasRemaining() )
{
break;
}
resulting in the PeerWireHandshakeDecode attempting to process the
bytes, before the change to PeerWireMessageDecode occurs. Even
changing the filter during the doDecode would fail, because the
CumulativeProtocolDecoder code above will still call the same Decoder.
Doesn't look like a bug if either setup, just need to change my
architecture a little.
Using a SwithingProtocolHandler would work, since the resulting call
would switch how it is handled within the same Decoder, avoiding this
problem. I'll go ahead and move to that kind of design. I didn't think
trying to switch Decoders would be a good idea, but it felt like it
would be the most intuitive to someone working with my code in the
future.
Thanks for all of the help,
Tyler
On 5/14/06, Trustin Lee <tr...@gmail.com> wrote:
> Please try to create a new ProtocolDecoder which wraps two decoders you
> implemented, and switches them with a method call:
>
> public SwitchingProtocolDecoder implements ProtocolDecoder {
> private boolean useDecoderB;
> private final ProtocolDecoder decoderA = ...;
> private final ProtocolDecoder decoderB = ...;
>
> public void decode(...) {
> if (useDecoderB) {
> decoderB.decode(...);
> } else {
> decoderA.decode(...);
> }
> }
> }
>
> Actually we're going to provide this kind of switch soon...
>
> Thanks,
> Trustin
>
> On 5/15/06, Tyler Pitchford <ty...@gmail.com> wrote:
> >
> > RECEIVED: DirectBuffer[pos=0 lim=6 cap=8192: 00 00 00 02 05 FE]
>
>
> It seems like you inserted a LoggingFilter. Am I correct? Were there any
> logged exceptions then?
>
> Which is the message I'm expecting. However, if I do the addFirst,
> > then I get no log printout. I've run Ethereal to check if the packets
> > I'm looking for are being sent and it's definitely sending the packet
> > to me, but it's not always being picked up by MINA, that's for sure.
> > It's very spotty if the message is received (changing of filters or
> > not).
>
>
> Do you mean that you didn't get any messages or exceptions even when you
> didn't remove nor add a codec? What happens if you don't change the codec
> and encounter a unknown data?
>
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP key fingerprints:
> * E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
> * B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
>
>
Re: [mina] Changing Filters Durning Application Cycles
Posted by Trustin Lee <tr...@gmail.com>.
Please try to create a new ProtocolDecoder which wraps two decoders you
implemented, and switches them with a method call:
public SwitchingProtocolDecoder implements ProtocolDecoder {
private boolean useDecoderB;
private final ProtocolDecoder decoderA = ...;
private final ProtocolDecoder decoderB = ...;
public void decode(...) {
if (useDecoderB) {
decoderB.decode(...);
} else {
decoderA.decode(...);
}
}
}
Actually we're going to provide this kind of switch soon...
Thanks,
Trustin
On 5/15/06, Tyler Pitchford <ty...@gmail.com> wrote:
>
> RECEIVED: DirectBuffer[pos=0 lim=6 cap=8192: 00 00 00 02 05 FE]
It seems like you inserted a LoggingFilter. Am I correct? Were there any
logged exceptions then?
Which is the message I'm expecting. However, if I do the addFirst,
> then I get no log printout. I've run Ethereal to check if the packets
> I'm looking for are being sent and it's definitely sending the packet
> to me, but it's not always being picked up by MINA, that's for sure.
> It's very spotty if the message is received (changing of filters or
> not).
Do you mean that you didn't get any messages or exceptions even when you
didn't remove nor add a codec? What happens if you don't change the codec
and encounter a unknown data?
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
Re: [mina] Changing Filters Durning Application Cycles
Posted by Tyler Pitchford <ty...@gmail.com>.
If I do:
session.getFilterChain().addLast("peerWireFilter", new
ProtocolCodecFilter(codec));
instead of
session.getFilterChain().addFirst("peerWireFilter", new
ProtocolCodecFilter(codec));
I get this message (occasionally):
RECEIVED: DirectBuffer[pos=0 lim=6 cap=8192: 00 00 00 02 05 FE]
Which is the message I'm expecting. However, if I do the addFirst,
then I get no log printout. I've run Ethereal to check if the packets
I'm looking for are being sent and it's definitely sending the packet
to me, but it's not always being picked up by MINA, that's for sure.
It's very spotty if the message is received (changing of filters or
not).
I'll keep working on it and let you know if I figure anything out.
Cheers,
Tyler
On 5/14/06, Trustin Lee <tr...@gmail.com> wrote:
> On 5/15/06, Tyler Pitchford <ty...@gmail.com> wrote:
> >
> > I can't seem to find an answer to this and I am getting very spotting
> > results (looks like threading issues). I'm tyring to build a
> > BitTorrent Filter for MINA, but I can't seem to get everything working
> > correctly.
> >
> > To understand that situation, you'll need a little background on the
> > BT protocol. When connecting to a peer a "handshake" message is sent,
> > nothing unusual here, right? Well, sadly the handshake doesn't follow
> > the wire header format. The Handshake uses a singly byte for it's
> > header and the wire messages use a 4 byte header.
> >
> > Anyway, to handle it "cleanly" (probably not the fastest, but nicely
> > architecturally), I developed a PeerWireHandshakeCodecFactory and a
> > PeerWireCodecFactory. I could just change the header length, once the
> > handshake was received, but I don't want to mix the handshake / wire
> > messages, because in my opinion they should be handled separately.
> > Anyway, I'm currently trying to do this is once the handshake response
> > is received (handshake part is working correctly), I call:
> >
> > public void messageReceived(IoSession session, Object message){
> > if (message instanceof PeerWireHandshakeMessage) {
> > synchronized(session){
> > session.getFilterChain().remove("handshakeFilter");
> > ProtocolCodecFactory codec = new PeerWireCodecFactory();
> > session.getFilterChain().addFirst("peerWireFilter", new
> > ProtocolCodecFilter(codec));
> > }
> > }
> >
> > The problem I'm seeing is that the PeerWire Encoders/Decoders never
> > seem to get called. I've only seen them called one time, while
> > debugged, which makes me think it's a threading issue. Any help /
> > advice would be lovely. I'm not opposed to changing the architecture,
> > if needed.
>
>
> Do you get a raw ByteBuffer in messageReceived() when you switch the filter
> instead PeerWireCodecFactory is active? If so, it's not a bug but you need
> a little bit more coding. Otherwise, it can be a bug. Please let us know
> first.
>
> P.S. you should update the mailing list link of the website. Sorry
> > about the email to dev@xxxxxx
>
>
> Thanks for the heads up. We will fix it soon.
>
> HTH,
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP key fingerprints:
> * E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
> * B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
>
>
Re: [mina] Changing Filters Durning Application Cycles
Posted by Trustin Lee <tr...@gmail.com>.
On 5/15/06, Tyler Pitchford <ty...@gmail.com> wrote:
>
> I can't seem to find an answer to this and I am getting very spotting
> results (looks like threading issues). I'm tyring to build a
> BitTorrent Filter for MINA, but I can't seem to get everything working
> correctly.
>
> To understand that situation, you'll need a little background on the
> BT protocol. When connecting to a peer a "handshake" message is sent,
> nothing unusual here, right? Well, sadly the handshake doesn't follow
> the wire header format. The Handshake uses a singly byte for it's
> header and the wire messages use a 4 byte header.
>
> Anyway, to handle it "cleanly" (probably not the fastest, but nicely
> architecturally), I developed a PeerWireHandshakeCodecFactory and a
> PeerWireCodecFactory. I could just change the header length, once the
> handshake was received, but I don't want to mix the handshake / wire
> messages, because in my opinion they should be handled separately.
> Anyway, I'm currently trying to do this is once the handshake response
> is received (handshake part is working correctly), I call:
>
> public void messageReceived(IoSession session, Object message){
> if (message instanceof PeerWireHandshakeMessage) {
> synchronized(session){
> session.getFilterChain().remove("handshakeFilter");
> ProtocolCodecFactory codec = new PeerWireCodecFactory();
> session.getFilterChain().addFirst("peerWireFilter", new
> ProtocolCodecFilter(codec));
> }
> }
>
> The problem I'm seeing is that the PeerWire Encoders/Decoders never
> seem to get called. I've only seen them called one time, while
> debugged, which makes me think it's a threading issue. Any help /
> advice would be lovely. I'm not opposed to changing the architecture,
> if needed.
Do you get a raw ByteBuffer in messageReceived() when you switch the filter
instead PeerWireCodecFactory is active? If so, it's not a bug but you need
a little bit more coding. Otherwise, it can be a bug. Please let us know
first.
P.S. you should update the mailing list link of the website. Sorry
> about the email to dev@xxxxxx
Thanks for the heads up. We will fix it soon.
HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6
[mina] Changing Filters Durning Application Cycles
Posted by Tyler Pitchford <ty...@gmail.com>.
I can't seem to find an answer to this and I am getting very spotting
results (looks like threading issues). I'm tyring to build a
BitTorrent Filter for MINA, but I can't seem to get everything working
correctly.
To understand that situation, you'll need a little background on the
BT protocol. When connecting to a peer a "handshake" message is sent,
nothing unusual here, right? Well, sadly the handshake doesn't follow
the wire header format. The Handshake uses a singly byte for it's
header and the wire messages use a 4 byte header.
Anyway, to handle it "cleanly" (probably not the fastest, but nicely
architecturally), I developed a PeerWireHandshakeCodecFactory and a
PeerWireCodecFactory. I could just change the header length, once the
handshake was received, but I don't want to mix the handshake / wire
messages, because in my opinion they should be handled separately.
Anyway, I'm currently trying to do this is once the handshake response
is received (handshake part is working correctly), I call:
public void messageReceived(IoSession session, Object message){
if (message instanceof PeerWireHandshakeMessage) {
synchronized(session){
session.getFilterChain().remove("handshakeFilter");
ProtocolCodecFactory codec = new PeerWireCodecFactory();
session.getFilterChain().addFirst("peerWireFilter", new
ProtocolCodecFilter(codec));
}
}
The problem I'm seeing is that the PeerWire Encoders/Decoders never
seem to get called. I've only seen them called one time, while
debugged, which makes me think it's a threading issue. Any help /
advice would be lovely. I'm not opposed to changing the architecture,
if needed.
Cheers,
Tyler
P.S. you should update the mailing list link of the website. Sorry
about the email to dev@xxxxxx