 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dennisk Guest
|
Posted: Sun Aug 29, 2004 11:13 am Post subject: Design Patterns(GOF)-State and sockets-I think I screwed-up- |
|
|
Hello All,
I made the mistake of applying the State Pattern to socket'ed
connections and I have to figure out a way to recover.
I say mistake, because the State Pattern relies on the Singleton
Pattern which makes heavy use of Static Instances of Connections, i.e.
ONE per main thread.
Well, since there are MANY sockets coming into my server app, I am not
able to create/track more than ONE socket connection, by the State
Pattern, i.e. I have
a problem in tracking more than one server socket using this
Pattern....
The State Pattern uses TCP as its example, which implies that there
can be only ONE TCP Connection per main thread? Is that correct?
anyway.....
Does anyone have a way out of this conumdrum...or goof-up(technical
term)?
-tx
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sun Aug 29, 2004 10:43 pm Post subject: Re: Design Patterns(GOF)-State and sockets-I think I screwe |
|
|
[email]dennisk (AT) pilabs (DOT) com[/email] (Dennisk) writes:
| Quote: | I made the mistake of applying the State Pattern to socket'ed
connections and I have to figure out a way to recover.
I say mistake, because the State Pattern relies on the Singleton
Pattern which makes heavy use of Static Instances of Connections, i.e.
ONE per main thread.
Well, since there are MANY sockets coming into my server app, I am not
able to create/track more than ONE socket connection, by the State
Pattern, i.e. I have a problem in tracking more than one server socket
using this Pattern....
The State Pattern uses TCP as its example, which implies that there
can be only ONE TCP Connection per main thread? Is that correct?
|
If you use stateless State objects, you won't have this problem. I.e. each
State object should only be responsible for determining how to handle
events, but not contain buffers, counters etc., and probably no non-static
data members at all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bronek Kozicki Guest
|
Posted: Sun Aug 29, 2004 10:45 pm Post subject: Re: Design Patterns(GOF)-State and sockets-I think I screwed |
|
|
Dennisk wrote:
| Quote: | I say mistake, because the State Pattern relies on the Singleton
Pattern which makes heavy use of Static Instances of Connections, i.e.
ONE per main thread.
|
just a suggestion, I'm not sure whether good one: keep one connection
*per thread* and make your "singleton" object reside in thread local
storage. It's no longer singleton, of course, but from POV of class user
it looks and behaves like one. It also implies that you can actually use
multithreading *and* have single thread per connection (which is
sometimes sub-optimal)
B.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Charles Rapp Guest
|
Posted: Mon Sep 06, 2004 8:42 pm Post subject: Re: Design Patterns(GOF)-State and sockets-I think I screwed |
|
|
Sorry for the delayed response. I have been on vacation.
I am replying because I do use State Pattern to handle multiple TCP
connections in the same process (both C++ and Java).
[email]dennisk (AT) pilabs (DOT) com[/email] (Dennisk) wrote in message news:<9a4aa272.0408280925.2513d841 (AT) posting (DOT) google.com>...
| Quote: | Hello All,
I made the mistake of applying the State Pattern to socket'ed
connections and I have to figure out a way to recover.
I say mistake, because the State Pattern relies on the Singleton
Pattern which makes heavy use of Static Instances of Connections, i.e.
ONE per main thread.
|
No, the State Pattern does *not* rely on the Singleton Pattern.
Looking at the diagram on GoF p. 306 neither Context or State classes
are singleton. The diagram on GoF p. 305 shows that for each socket
you would instantiate TCPConnection, TCPEstablished, TCPListen and
TCPClosed.
GoF p. 309, item 3 points out that State classes have no member data,
therefore it is possible to make the state classes (TCPEstablished,
TCPListen and TCPClosed) singletons. The Context class (TCPConnection)
then maintains a reference to the singleton current state.
| Quote: |
Well, since there are MANY sockets coming into my server app, I am not
able to create/track more than ONE socket connection, by the State
Pattern, i.e. I have
a problem in tracking more than one server socket using this
Pattern....
The State Pattern uses TCP as its example, which implies that there
can be only ONE TCP Connection per main thread? Is that correct?
|
No, this is not correct. In the GoF p. 305, you instantiate a
TCPConnection object for each socket. Now the GoF example makes no
mention of an actual socket because they are not writing a book on
socket programming. Socket programming is OS and programming
language-specific. Assuming you have that part understood, I imagine
you could place the socket data in TCPConnection class.
I strongly suggest you go over the GoF State Pattern chapter (pp. 305
- 313) again and go over their TCPConnection is detail. Notice how
they pass at least the Context (TCPConnection) object to the state
transition methods. This allows the state's to reference the context
without storing the context within the state object. Also see how it
is possible to pass other arguments to the state transition methods,
e.g. TCPOctetStream.
| Quote: |
anyway.....
Does anyone have a way out of this conumdrum...or goof-up(technical
term)?
|
Your are on the right track. The State Pattern is the correct solution
for dealing with asynchronous sockets.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|