summaryrefslogtreecommitdiff
path: root/Ivy/IvyWatcher.cxx
diff options
context:
space:
mode:
authorfcolin2007-02-01 13:03:38 +0000
committerfcolin2007-02-01 13:03:38 +0000
commit614da7f1a522dc1f234269d3f6422542758ac928 (patch)
treec9d472596842a5d72b23ce471c2e964959544ab5 /Ivy/IvyWatcher.cxx
parent33d722d0f56256e7e0aff49063c95c4d0fe44877 (diff)
downloadivy-cplusplus-614da7f1a522dc1f234269d3f6422542758ac928.zip
ivy-cplusplus-614da7f1a522dc1f234269d3f6422542758ac928.tar.gz
ivy-cplusplus-614da7f1a522dc1f234269d3f6422542758ac928.tar.bz2
ivy-cplusplus-614da7f1a522dc1f234269d3f6422542758ac928.tar.xz
Utilisateur : Fcolin Date : 16/06/00 Heure : 10:14 Créé (vss 1)
Diffstat (limited to 'Ivy/IvyWatcher.cxx')
-rw-r--r--Ivy/IvyWatcher.cxx114
1 files changed, 114 insertions, 0 deletions
diff --git a/Ivy/IvyWatcher.cxx b/Ivy/IvyWatcher.cxx
new file mode 100644
index 0000000..c963968
--- /dev/null
+++ b/Ivy/IvyWatcher.cxx
@@ -0,0 +1,114 @@
+// IvyWatcher.cpp : implementation file
+//
+
+#include "stdafx.h"
+
+#include "IvyWatcher.h"
+#include "IvyApplication.h"
+
+/////////////////////////////////////////////////////////////////////////////
+// IvyWatcher
+
+
+#define VERSION 3
+
+IvyWatcher::IvyWatcher(Ivy * bus)
+{
+ this->bus = bus;
+}
+IvyWatcher::~IvyWatcher()
+{
+}
+
+
+
+/////////////////////////////////////////////////////////////////////////////
+// IvyWatcher member functions
+
+void IvyWatcher::OnReceive(int nErrorCode)
+{
+ int err;
+ int version;
+ char buffer[256];
+ string remotehost;
+ UINT remoteport;
+ UINT serviceport;
+
+
+ err = ReceiveFrom( buffer, sizeof(buffer), remotehost, remoteport );
+ if ( err == SOCKET_ERROR )
+ {
+ TRACE("Receive Broadcast error %d\n",GetLastError());
+ return;
+ }
+ buffer[err] ='\0';
+ err = sscanf(buffer,"%d %u",&version, &serviceport);
+ if ( err != 2 )
+ {
+ /* ignore the message */
+ TRACE(" Bad Supervision message expected 'version port'\n");
+ return;
+ }
+ if ( version != VERSION )
+ {
+ /* ignore the message */
+ TRACE(" Bad Ivy verion number expected %d receive %d from %s:%d\n", VERSION,version, remotehost, remoteport);
+ return;
+ }
+ /* check if we receive our own message should test also the host */
+ if ( serviceport == bus->GetApplicationPort() /*&& remotehost == "localhost"*/) return;
+ TRACE(" Broadcast de %s:%u port %u\n", remotehost.c_str(), remoteport, serviceport );
+
+ /* connect to the service and send the regexp */
+ IvyApplication *newapp = new IvyApplication(bus);
+ // exception to catch
+ newapp->Create(remotehost.c_str(), serviceport);
+ // delete newapp;
+ // return;
+
+ bus->AddApplication( newapp );
+ TRACE(" Connecting to %s:%u\n", remotehost.c_str(), serviceport );
+
+}
+
+void IvyWatcher::start(const char *domainlist)
+{
+ BOOL reuse = TRUE;
+ string domain;
+ UINT port;
+ // determine domain to use
+ domain = bus->GetDomain( domainlist );
+
+ // first find our UDP port
+ int sep_index = domain.rfind( ':' );
+ if ( sep_index != -1 )
+ {
+ port = atoi ( domain.substr( sep_index +1 ).c_str() );
+ // supress port number from end of domain list
+ domain.erase( sep_index, domain.length() - sep_index );
+ }
+ // create UDP receiver
+ // catch exception !!!
+ Socket(SOCK_DGRAM);
+ SetSockOpt( SO_REUSEADDR, &reuse, sizeof(BOOL) );
+ Bind(port);
+
+ string addr;
+ char hello[1024];
+ int len = sprintf( hello, "%d %u\n", VERSION, bus->GetApplicationPort() );
+
+ // send broadcast to domain list
+ while ( !domain.empty() )
+ {
+ // find addr up to separator
+ int index = domain.find_first_of( ", \t" );
+ addr = domain.substr( 0, index );
+ domain.erase( 0, addr.length() +1 );
+ TRACE("Ivy Broadcasting on %s:%d\n", addr.c_str(), port );
+ SendTo( hello, len, port, addr.c_str() );
+ }
+}
+void IvyWatcher::stop()
+{
+ Close();
+}