Overview
Features
Download
Documentation
Community
Add-Ons & Services

trigger event on TCPServer threads

Please post support and help requests here.

trigger event on TCPServer threads

Postby cosu2004 » 05 Jul 2012, 11:04

Hello guys, I'm new to poco and not to good with C++ but I am learning. I have to create a server-client based application in windows.
The problem that I have now is that I need to send repeatedly from minute to minute some data to the clients. i need to do this for the clients that have an active tcp connection with the server. I don't know how can I create an event, or something that is triggered in a thread and starts all the active threads to send data to the clients.
My first idea is that I have to rewrite, or extend the TCPServerDispatcher Class :cry: . And I don't know how can I identify the active threads from the ThreadPool.

Do you have any ideas, or maybe suggestions, or a tutorial, something?
I can't figure it out how to do it...

Hope somebody can give me an idea, or some code example. Thank you.
cosu2004
 
Posts: 14
Joined: 05 Jul 2012, 10:50

Re: trigger event on TCPServer threads

Postby alex » 05 Jul 2012, 18:55

Use NotificationQueue. For details, see example and Notifications and Events presentation.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: trigger event on TCPServer threads

Postby alex » 05 Jul 2012, 18:59

cosu2004 wrote:I'm new to poco and not to good with C++

Not sure where you are in regards to networking and threading, but you may be taking too big of a bite for start. My advice would be to start learning with something simpler.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: trigger event on TCPServer threads

Postby cosu2004 » 11 Jul 2012, 11:29

Ok, well I;m not so bad. I'm at a medium level. I've done C++ applications before.
Can PLEASE some one help me with this problem? I don't know exactly how to make the separate thread to send notifications to all the TCPServer active threads. And the documentation doesn't help to much. how can I pass the queue to the thread class initialization (in the dispatcher) without modifying, or rewriting the TCPServer class... Is there any other way?

PLEASE HELP! :cry:
cosu2004
 
Posts: 14
Joined: 05 Jul 2012, 10:50

Re: trigger event on TCPServer threads

Postby alex » 11 Jul 2012, 20:59

Ok, let me try to help you. TCPServer and Dispatcher were not meant for end users to modify. What you have available is connection and connection factory. So, you'll want to:

1) Create your own connection
Code: Select all
class MyConnection: public TCPServerConnection
{
public:
   MyConnection(const StreamSocket& s, NotificationCenter& nc): TCPServerConnection(s), _ss(socket())
   {
      Observer<MyConnection, Notification> observer(*this, &MyConnection::handleNotification);
      nc.addObserver(observer);
   }
   
   void run()
   {
      while (true /* your criteria here to keep connection alive */)
      { Thread::sleep(1000); }
   }

private:
   void handleNotification(Notification* pNf)
   {
      AutoPtr<Notification> nf = pNf;  // do this or call release() explicitly
      _ss.sendBytes("hello", 5);
   }

   StreamSocket&      _ss;
};

2) Create the factory with NotificationCenter member
Code: Select all
class MyConnectionFactory: public TCPServerConnectionFactory
{
public:
   TCPServerConnection* createConnection(const StreamSocket& socket)
   { return new MyConnection(socket, _nc); }

   void notify(Notification* pN)
   { _nc.postNotification(pN); }

private:
   NotificationCenter _nc;
};

3) Create and run the server
Code: Select all
ServerSocket ss(0);
MyConnectionFactory mcf;
TCPServer ts(&mcf, ss);
ts.start();

4) Connect the client to server
Code: Select all
SocketAddress sa("localhost", ss.address().port());
StreamSocket ss1(sa);
while (0 == ts.currentConnections ()) // to make sure connection is established
Thread::sleep(100);

5) Notify the connection and receive response on the client side
Code: Select all
mcf.notify(new Notification);
char buffer[6] = {0};
int n = ss1.receiveBytes(buffer, 5);
std::cout << buffer << std::endl;


Hope this helps.

FYI, typing ALL CAPS in online forums is considered shouting, i.e. rude; please refrain from it. We are not obligated to respond to your requests, we do it because we are kind. Please be kind back to us. Thanks.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: trigger event on TCPServer threads

Postby alex » 11 Jul 2012, 21:01

P.S. If you wish to avoid spinning in MyConnection, you may consider using Poco::Condition.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: trigger event on TCPServer threads

Postby cosu2004 » 12 Jul 2012, 07:31

Alex, thank you so much for your solution. This morning this idea came to me in the bus, after some reading last night on the forum.
Thank you so much again! Sorry for the caps :roll:
cosu2004
 
Posts: 14
Joined: 05 Jul 2012, 10:50

Re: trigger event on TCPServer threads

Postby cosu2004 » 12 Jul 2012, 07:37

oh, I was thinking at NotificationQueue But now I see that NotificationCenter is better for what I need.
cosu2004
 
Posts: 14
Joined: 05 Jul 2012, 10:50

Re: trigger event on TCPServer threads

Postby alex » 12 Jul 2012, 22:56

I forgot to mention, you'll have to unregister observers in the connection destructor:
Code: Select all
~MyConnection()
{
      _nc.removeObserver(Observer<MyConnection, Notification>(*this, &MyConnection::handleNotification));
}

To be able to do that, you'll obviously need NotificationCenter& _nc member in MyConnection to retain the reference.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests

cron