cyphesis/server/CommMDNSPublisher.cpp

416 lines
12 KiB
C++
Raw Permalink Normal View History

2006-01-19 Al Riddoch <alriddoch@zepler.org> * aiclient/ClientAccount.cpp, aiclient/ClientAccount.h, aiclient/ClientConnection.cpp, aiclient/ClientConnection.h, aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp, client/BaseClient.h, client/CharacterClient.cpp, client/CharacterClient.h, client/ClientConnection.cpp, client/ClientConnection.h, client/CreatorClient.cpp, client/CreatorClient.h, client/ObserverClient.cpp, client/ObserverClient.h, client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, client/client.cpp, client/define_world.cpp, client/define_world.h, common/Add.h, common/Affect.h, common/AtlasFileLoader.cpp, common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp, common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h, common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp, common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h, common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h, common/Eat.h, common/FormattedXMLWriter.cpp, common/FormattedXMLWriter.h, common/Generic.h, common/Load.h, common/Monitor.h, common/Mow.h, common/Nourish.h, common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp, common/Property.h, common/Property_impl.h, common/Save.h, common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h, common/Use.h, common/Wield.h, common/accountbase.cpp, common/accountbase.h, common/const.cpp, common/const.h, common/custom.cpp, common/debug.h, common/globals.cpp, common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp, common/inheritance.h, common/inheritance_impl.h, common/log.cpp, common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h, common/operations.cpp, common/operations.h, common/random.h, common/refno.h, common/serialno.cpp, common/serialno.h, common/system.cpp, common/system.h, common/terrain_utils.cpp, common/terrain_utils.h, common/type_utils.h, common/types.h, common/utility.cpp, common/utility.h, modules/DateTime.cpp, modules/DateTime.h, modules/Location.cpp, modules/Location.h, modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp, physics/BBox.h, physics/Collision.cpp, physics/Collision.h, physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp, physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp, rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h, rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h, rulesets/BaseMind.cpp, rulesets/BaseMind.h, rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h, rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp, rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h, rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityProperties.cpp, rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h, rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp, rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h, rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h, rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h, rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp, rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h, rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h, rulesets/Py_Property.cpp, rulesets/Py_Property.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h, rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h, rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonArithmeticScript.cpp, rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp, rulesets/PythonMindScript.h, rulesets/PythonScript.cpp, rulesets/PythonScript.h, rulesets/PythonThingScript.cpp, rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp, rulesets/PythonWrapper.h, rulesets/Python_API.cpp, rulesets/Python_API.h, rulesets/Python_Script_Utils.h, rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp, rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h, rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h, rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp, rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h, rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h, rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp, rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h, rulesets/attributes.h, server/Account.cpp, server/Account.h, server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp, server/ArithmeticFactory.h, server/CommClient.cpp, server/CommClient.h, server/CommIdleSocket.cpp, server/CommIdleSocket.h, server/CommListener.cpp, server/CommListener.h, server/CommLocalClient.cpp, server/CommLocalClient.h, server/CommMDNSPublisher.cpp, server/CommMDNSPublisher.h, server/CommMaster.cpp, server/CommMaster.h, server/CommMetaClient.cpp, server/CommMetaClient.h, server/CommPSQLSocket.cpp, server/CommPSQLSocket.h, server/CommPeer.cpp, server/CommPeer.h, server/CommPeerListener.cpp, server/CommPeerListener.h, server/CommRemoteClient.cpp, server/CommRemoteClient.h, server/CommServer.cpp, server/CommServer.h, server/CommSlaveClient.cpp, server/CommSlaveClient.h, server/CommSlaveListener.cpp, server/CommSlaveListener.h, server/CommSocket.cpp, server/CommSocket.h, server/CommUnixListener.cpp, server/CommUnixListener.h, server/Connection.cpp, server/Connection.h, server/Connection_methods.h, server/EntityFactory.cpp, server/EntityFactory.h, server/ExternalMind.cpp, server/ExternalMind.h, server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h, server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h, server/Persistance.cpp, server/Persistance.h, server/PersistantThingFactory.cpp, server/PersistantThingFactory.h, server/PersistantThingFactory_impl.h, server/Persistor.cpp, server/Persistor.h, server/Persistor_impl.h, server/Player.cpp, server/Player.h, server/Restoration.cpp, server/Restoration.h, server/Restorer.cpp, server/Restorer.h, server/Restorer_impl.h, server/ScriptFactory.cpp, server/ScriptFactory.h, server/ServerRouting.cpp, server/ServerRouting.h, server/ServerRouting_methods.h, server/SlaveClientConnection.cpp, server/SlaveClientConnection.h, server/TaskFactory.cpp, server/TaskFactory.h, server/TrustedConnection.cpp, server/TrustedConnection.h, server/WorldRouter.cpp, server/WorldRouter.h, server/server.cpp, server/server.h, server/slave.cpp, tests/Areatest.cpp, tests/BaseEntitytest.cpp, tests/Charactertest.cpp, tests/Containertest.cpp, tests/Creatortest.cpp, tests/EntityExerciser.h, tests/EntityFactorytest.cpp, tests/Entitytest.cpp, tests/Foodtest.cpp, tests/IGEntityExerciser.h, tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp, tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp, tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h, tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp, tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp, tests/distancetest.cpp, tests/emergencetest.cpp, tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp, tests/systemtest.cpp, tests/transformtest.cpp, tests/type_utilstest.cpp, tests/utilitytest.cpp, tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp, tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp, tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp, tools/cywatchdog.cpp: Replace the copyright and license statements with new versions that include disclaimer of warranty, and permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00
// Cyphesis Online RPG Server and AI Engine
2011-09-08 13:30:25 +01:00
// Copyright (C) 2004-2011 Alistair Riddoch
2006-01-19 Al Riddoch <alriddoch@zepler.org> * aiclient/ClientAccount.cpp, aiclient/ClientAccount.h, aiclient/ClientConnection.cpp, aiclient/ClientConnection.h, aiclient/client.cpp, aiclient/client.h, client/BaseClient.cpp, client/BaseClient.h, client/CharacterClient.cpp, client/CharacterClient.h, client/ClientConnection.cpp, client/ClientConnection.h, client/CreatorClient.cpp, client/CreatorClient.h, client/ObserverClient.cpp, client/ObserverClient.h, client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, client/client.cpp, client/define_world.cpp, client/define_world.h, common/Add.h, common/Affect.h, common/AtlasFileLoader.cpp, common/AtlasFileLoader.h, common/Attack.h, common/BaseEntity.cpp, common/BaseEntity.h, common/BaseWorld.cpp, common/BaseWorld.h, common/Burn.h, common/Chop.h, common/Connect.h, common/CustomOp.cpp, common/CustomOp.h, common/CustomOp_impl.h, common/Cut.h, common/Database.cpp, common/Database.h, common/Delve.h, common/Dig.h, common/Eat.h, common/FormattedXMLWriter.cpp, common/FormattedXMLWriter.h, common/Generic.h, common/Load.h, common/Monitor.h, common/Mow.h, common/Nourish.h, common/OOGThing.cpp, common/OOGThing.h, common/Property.cpp, common/Property.h, common/Property_impl.h, common/Save.h, common/Setup.h, common/Tick.h, common/Unseen.h, common/Update.h, common/Use.h, common/Wield.h, common/accountbase.cpp, common/accountbase.h, common/const.cpp, common/const.h, common/custom.cpp, common/debug.h, common/globals.cpp, common/globals.h, common/id.cpp, common/id.h, common/inheritance.cpp, common/inheritance.h, common/inheritance_impl.h, common/log.cpp, common/log.h, common/newid.cpp, common/nls.h, common/op_switch.h, common/operations.cpp, common/operations.h, common/random.h, common/refno.h, common/serialno.cpp, common/serialno.h, common/system.cpp, common/system.h, common/terrain_utils.cpp, common/terrain_utils.h, common/type_utils.h, common/types.h, common/utility.cpp, common/utility.h, modules/DateTime.cpp, modules/DateTime.h, modules/Location.cpp, modules/Location.h, modules/WorldTime.cpp, modules/WorldTime.h, physics/BBox.cpp, physics/BBox.h, physics/Collision.cpp, physics/Collision.h, physics/Quaternion.cpp, physics/Quaternion.h, physics/Vector3D.cpp, physics/Vector3D.h, physics/VectorPol.h, rulesets/Area.cpp, rulesets/Area.h, rulesets/AreaProperty.cpp, rulesets/AreaProperty.h, rulesets/ArithmeticScript.cpp, rulesets/ArithmeticScript.h, rulesets/BaseMind.cpp, rulesets/BaseMind.h, rulesets/CalendarProperty.cpp, rulesets/CalendarProperty.h, rulesets/Character.cpp, rulesets/Character.h, rulesets/Combat.cpp, rulesets/Combat.h, rulesets/Container.cpp, rulesets/Container.h, rulesets/Creator.cpp, rulesets/Creator.h, rulesets/Entity.cpp, rulesets/Entity.h, rulesets/EntityProperties.cpp, rulesets/Entity_getLocation.h, rulesets/Fell.cpp, rulesets/Fell.h, rulesets/Food.cpp, rulesets/Food.h, rulesets/Line.cpp, rulesets/Line.h, rulesets/LineProperty.cpp, rulesets/LineProperty.h, rulesets/MemEntity.cpp, rulesets/MemEntity.h, rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h, rulesets/MindFactory.cpp, rulesets/MindFactory.h, rulesets/Missile.h, rulesets/Motion.cpp, rulesets/Motion.h, rulesets/Movement.cpp, rulesets/Movement.h, rulesets/Pedestrian.cpp, rulesets/Pedestrian.h, rulesets/Plant.cpp, rulesets/Plant.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_EntityWrapper.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Point3D.cpp, rulesets/Py_Point3D.h, rulesets/Py_Property.cpp, rulesets/Py_Property.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_RootEntity.cpp, rulesets/Py_RootEntity.h, rulesets/Py_Statistics.cpp, rulesets/Py_Statistics.h, rulesets/Py_Task.cpp, rulesets/Py_Task.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonArithmeticScript.cpp, rulesets/PythonArithmeticScript.h, rulesets/PythonMindScript.cpp, rulesets/PythonMindScript.h, rulesets/PythonScript.cpp, rulesets/PythonScript.h, rulesets/PythonThingScript.cpp, rulesets/PythonThingScript.h, rulesets/PythonWrapper.cpp, rulesets/PythonWrapper.h, rulesets/Python_API.cpp, rulesets/Python_API.h, rulesets/Python_Script_Utils.h, rulesets/Script.cpp, rulesets/Script.h, rulesets/Stackable.cpp, rulesets/Stackable.h, rulesets/Statistics.cpp, rulesets/Statistics.h, rulesets/StatisticsProperty.cpp, rulesets/StatisticsProperty.h, rulesets/Structure.cpp, rulesets/Structure.h, rulesets/Task.cpp, rulesets/Task.h, rulesets/TaskScript.cpp, rulesets/TaskScript.h, rulesets/TerrainProperty.cpp, rulesets/TerrainProperty.h, rulesets/Thing.cpp, rulesets/Thing.h, rulesets/ThingFactory.cpp, rulesets/ThingFactory.h, rulesets/World.cpp, rulesets/World.h, rulesets/attributes.h, server/Account.cpp, server/Account.h, server/Admin.cpp, server/Admin.h, server/ArithmeticFactory.cpp, server/ArithmeticFactory.h, server/CommClient.cpp, server/CommClient.h, server/CommIdleSocket.cpp, server/CommIdleSocket.h, server/CommListener.cpp, server/CommListener.h, server/CommLocalClient.cpp, server/CommLocalClient.h, server/CommMDNSPublisher.cpp, server/CommMDNSPublisher.h, server/CommMaster.cpp, server/CommMaster.h, server/CommMetaClient.cpp, server/CommMetaClient.h, server/CommPSQLSocket.cpp, server/CommPSQLSocket.h, server/CommPeer.cpp, server/CommPeer.h, server/CommPeerListener.cpp, server/CommPeerListener.h, server/CommRemoteClient.cpp, server/CommRemoteClient.h, server/CommServer.cpp, server/CommServer.h, server/CommSlaveClient.cpp, server/CommSlaveClient.h, server/CommSlaveListener.cpp, server/CommSlaveListener.h, server/CommSocket.cpp, server/CommSocket.h, server/CommUnixListener.cpp, server/CommUnixListener.h, server/Connection.cpp, server/Connection.h, server/Connection_methods.h, server/EntityFactory.cpp, server/EntityFactory.h, server/ExternalMind.cpp, server/ExternalMind.h, server/Idle.cpp, server/Idle.h, server/Lobby.cpp, server/Lobby.h, server/Master.cpp, server/Master.h, server/Peer.cpp, server/Peer.h, server/Persistance.cpp, server/Persistance.h, server/PersistantThingFactory.cpp, server/PersistantThingFactory.h, server/PersistantThingFactory_impl.h, server/Persistor.cpp, server/Persistor.h, server/Persistor_impl.h, server/Player.cpp, server/Player.h, server/Restoration.cpp, server/Restoration.h, server/Restorer.cpp, server/Restorer.h, server/Restorer_impl.h, server/ScriptFactory.cpp, server/ScriptFactory.h, server/ServerRouting.cpp, server/ServerRouting.h, server/ServerRouting_methods.h, server/SlaveClientConnection.cpp, server/SlaveClientConnection.h, server/TaskFactory.cpp, server/TaskFactory.h, server/TrustedConnection.cpp, server/TrustedConnection.h, server/WorldRouter.cpp, server/WorldRouter.h, server/server.cpp, server/server.h, server/slave.cpp, tests/Areatest.cpp, tests/BaseEntitytest.cpp, tests/Charactertest.cpp, tests/Containertest.cpp, tests/Creatortest.cpp, tests/EntityExerciser.h, tests/EntityFactorytest.cpp, tests/Entitytest.cpp, tests/Foodtest.cpp, tests/IGEntityExerciser.h, tests/Linetest.cpp, tests/Locationtest.cpp, tests/OOGThingtest.cpp, tests/Planttest.cpp, tests/Propertytest.cpp, tests/Stackabletest.cpp, tests/Structuretest.cpp, tests/Tasktest.cpp, tests/TestWorld.h, tests/Thingtest.cpp, tests/WorldTimetest.cpp, tests/Worldtest.cpp, tests/allOperations.h, tests/collisiontest.cpp, tests/consttest.cpp, tests/distancetest.cpp, tests/emergencetest.cpp, tests/inheritancetest.cpp, tests/logtest.cpp, tests/randomtest.cpp, tests/systemtest.cpp, tests/transformtest.cpp, tests/type_utilstest.cpp, tests/utilitytest.cpp, tools/AdminClient.cpp, tools/AdminClient.h, tools/cyaddrules.cpp, tools/cycmd.cpp, tools/cyconfig.cpp, tools/cyconvertrules.cpp, tools/cyctrl.cpp, tools/cydbload.cpp, tools/cydumprules.cpp, tools/cyisoload.cpp, tools/cyloadrules.cpp, tools/cypasswd.cpp, tools/cywatchdog.cpp: Replace the copyright and license statements with new versions that include disclaimer of warranty, and permit distribution under later versions of the GPL.
2006-01-19 02:17:06 +00:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2004-11-11 14:26:45 +00:00
2004-11-11 14:26:45 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#undef PACKAGE
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef PACKAGE_BUGREPORT
2004-11-11 14:26:45 +00:00
#undef VERSION
#include "CommMDNSPublisher.h"
#include "ServerRouting.h"
#include "common/log.h"
#include "common/const.h"
#include "common/debug.h"
#include "common/BaseWorld.h"
#include "common/compose.hpp"
#include "common/sockets.h"
#include <iostream>
#if defined(HAVE_AVAHI)
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/error.h>
#include <cassert>
static const bool debug_flag = false;
static void client_callback(AvahiClient * s,
AvahiClientState state,
void * userdata)
{
2012-05-15 11:57:52 +01:00
CommMDNSPublisher * cmp = static_cast<CommMDNSPublisher*>(userdata);
switch (state) {
case AVAHI_CLIENT_S_RUNNING:
// Check we have not already started them
if (cmp->m_group == 0) {
cmp->setup_service(s);
}
break;
case AVAHI_CLIENT_S_COLLISION:
log(WARNING, "Name collision while publishing using avahi MDNS");
break;
case AVAHI_CLIENT_FAILURE:
log(WARNING, "Failure while publishing using avahi MDNS");
break;
case AVAHI_CLIENT_CONNECTING:
2011-09-09 16:48:33 +01:00
log(NOTICE, "Avahi returned connecting");
break;
case AVAHI_CLIENT_S_REGISTERING:
log(WARNING, "Avahi registering");
break;
default:
log(WARNING, "Unknown state");
break;
}
}
2011-09-07 11:10:38 +01:00
static void group_callback(AvahiEntryGroup * g,
AvahiEntryGroupState state,
void * userdata)
{
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED :
/* The entry group has been established successfully */
break;
case AVAHI_ENTRY_GROUP_COLLISION : {
log(NOTICE, "Avahi callback reported group collision");
/* A service name collision happened. Let's pick a new name */
// char * n = avahi_alternative_service_name(name);
// avahi_free(name);
// name = n;
// fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
/* And recreate the services */
// create_services(avahi_entry_group_get_client(g));
break;
}
case AVAHI_ENTRY_GROUP_FAILURE :
log(NOTICE, "Avahi callback reported group failure");
/* Some kind of failure happened while we were registering our services */
// avahi_simple_poll_quit(simple_poll);
break;
case AVAHI_ENTRY_GROUP_UNCOMMITED:
case AVAHI_ENTRY_GROUP_REGISTERING:
// This will happen when the group is created or registering,
// and is not noteworthy
break;
}
}
struct AvahiWatch {
CommMDNSPublisher * m_publisher;
AvahiWatchEvent m_requiredEvent;
AvahiWatchCallback m_callback;
AvahiWatchEvent m_events;
void * m_userdata;
};
static AvahiWatch* watch_new(const AvahiPoll *api,
int fd,
AvahiWatchEvent event,
AvahiWatchCallback callback,
void *userdata)
{
2011-09-07 18:53:49 +01:00
debug(std::cout << "avahi_watch_new " << fd << " " << callback
<< std::endl << std::flush;);
2012-05-15 11:57:52 +01:00
CommMDNSPublisher * cmp = static_cast<CommMDNSPublisher*>(api->userdata);
2014-01-31 13:36:03 +01:00
if (cmp->m_avahiFd != -1) {
log(ERROR, "Avahi asked for multiple fds. Unable to comply.");
} else {
cmp->m_avahiFd = fd;
}
2011-09-07 18:53:49 +01:00
if (~event & AVAHI_WATCH_IN) {
log(ERROR, "Avahi watcher does not require read events.");
}
if (event & ~AVAHI_WATCH_IN) {
log(WARNING, "Avahi watcher requires unsupported events.");
}
AvahiWatch * aw = new AvahiWatch;
aw->m_publisher = cmp;
aw->m_requiredEvent = event;
aw->m_callback = callback;
aw->m_events = (AvahiWatchEvent)0;
aw->m_userdata = userdata;
cmp->m_avahiWatch = aw;
return aw;
}
static void watch_update(AvahiWatch *w, AvahiWatchEvent event)
{
debug(std::cout << "avahi_watch_update" << std::endl << std::flush;);
w->m_requiredEvent = event;
}
static AvahiWatchEvent watch_get_events(AvahiWatch *w)
{
debug(std::cout << "avahi_watch_get_events" << std::endl << std::flush;);
return w->m_events;
}
static void watch_free(AvahiWatch *w)
{
debug(std::cout << "avahi_watch_free" << std::endl << std::flush;);
log(WARNING, "avahi watch_free handler called");
}
struct AvahiTimeout {
CommMDNSPublisher * m_publisher;
struct timeval m_expiry;
enum { NEW = 0, ENABLED, EXPIRED, DEAD } m_state;
AvahiTimeoutCallback m_callback;
void * m_userdata;
};
static AvahiTimeout* timeout_new(const AvahiPoll * api,
const struct timeval * tv,
AvahiTimeoutCallback callback,
void *userdata)
{
debug(std::cout << "avahi_timeout_new("
<< (tv ? tv->tv_sec : -1) << "," << callback << ")"
<< std::endl << std::flush;);
2012-05-15 11:57:52 +01:00
CommMDNSPublisher * cmp = static_cast<CommMDNSPublisher*>(api->userdata);
AvahiTimeout * at = new AvahiTimeout;
at->m_publisher = cmp;
if (tv != 0) {
at->m_expiry = *tv;
at->m_state = AvahiTimeout::ENABLED;
} else {
at->m_state = AvahiTimeout::NEW;
}
at->m_callback = callback;
at->m_userdata = userdata;
cmp->m_avahiTimeouts.insert(at);
debug(std::cout << "avahi_timeout_new " << at
<< std::endl << std::flush;);
return at;
}
static void timeout_update(AvahiTimeout * at, const struct timeval *tv)
{
CommMDNSPublisher * cmp = at->m_publisher;
debug(std::cout << "avahi_timeout_update(" << at << ","
<< (tv ? tv->tv_sec : -1) << ")"
<< std::endl << std::flush;);
if (tv != 0) {
at->m_expiry = *tv;
at->m_state = AvahiTimeout::ENABLED;
if (tv->tv_sec == 0) {
cmp->setImmediate();
}
} else {
at->m_state = AvahiTimeout::NEW; // FIXME Really, new again?
}
}
static void timeout_free(AvahiTimeout * at)
{
debug(std::cout << "avahi_timeout_free(" << at << ")"
2011-09-10 13:28:54 +01:00
<< std::endl << std::flush;);
at->m_state = AvahiTimeout::DEAD;
at->m_publisher->m_avahiTimeouts.erase(at);
delete at;
}
2014-01-31 13:36:03 +01:00
CommMDNSPublisher::CommMDNSPublisher(boost::asio::io_service& io_service,
ServerRouting & s) : m_io_service(io_service),
m_socket(io_service),
m_timers_check_timer(io_service),
2012-05-15 13:10:33 +01:00
m_avahiClient(0),
m_avahiError(0),
m_server(s),
m_immediate(false),
m_avahiFd(-1),
2012-05-15 13:10:33 +01:00
m_avahiWatch(0),
m_group(0)
{
}
CommMDNSPublisher::~CommMDNSPublisher()
{
}
int CommMDNSPublisher::setup()
{
static AvahiPoll poll = { this,
watch_new,
watch_update,
watch_get_events,
watch_free,
timeout_new,
timeout_update,
timeout_free
};
2011-09-06 14:14:05 +01:00
m_avahiClient = avahi_client_new(&poll,
2011-09-09 10:53:37 +01:00
(AvahiClientFlags)AVAHI_CLIENT_NO_FAIL,
&client_callback,
2011-09-06 14:14:05 +01:00
this,
&m_avahiError);
if (m_avahiClient == 0) {
log(ERROR, String::compose("Avahi client creation failed: %1",
avahi_strerror(m_avahiError)));
return -1;
}
while (m_immediate) {
m_immediate = false;
checkTimers(0);
}
2014-01-31 13:36:03 +01:00
do_timer_check();
if (m_avahiFd == -1) {
log(ERROR, "Avahi client has not registed a file descriptor");
return -1;
}
2014-01-31 13:36:03 +01:00
m_socket.assign(boost::asio::ip::tcp::v4(), m_avahiFd);
do_read();
return 0;
}
2014-01-31 13:36:03 +01:00
void CommMDNSPublisher::do_read()
{
//only use asio to poll for data available; use the PG* functions
//to do the actual reading
m_socket.async_read_some(boost::asio::null_buffers(),
[this](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
this->read();
}
this->do_read();
});
}
void CommMDNSPublisher::setup_service(AvahiClient * client)
{
if (m_group == 0) {
m_group = avahi_entry_group_new(client, &group_callback, this);
}
if (m_group == 0) {
log(ERROR, String::compose("Avahi group creation failure. %1",
avahi_strerror(avahi_client_errno(client))));
return;
}
AvahiStringList * txt;
2011-09-06 14:14:05 +01:00
txt = avahi_string_list_new(
String::compose("builddate=%1",
std::string(consts::buildTime) + ", " +
std::string(consts::buildDate)).c_str(),
String::compose("clients=%1", m_server.getClients()).c_str(),
String::compose("ruleset=%1", m_server.getRuleset()).c_str(),
2011-09-06 14:14:05 +01:00
String::compose("server=%1", "cyphesis").c_str(),
String::compose("uptime=%1", m_server.m_world.upTime()).c_str(),
2011-09-06 14:14:05 +01:00
String::compose("version=%1", std::string(consts::version)).c_str(),
NULL);
int ret;
2011-09-06 14:14:05 +01:00
ret = avahi_entry_group_add_service_strlst(
m_group,
AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags)0,
m_server.getName().c_str(),
2011-09-06 14:14:05 +01:00
"_worldforge._tcp", NULL, NULL,
client_port_num, txt);
avahi_string_list_free(txt);
if (ret < 0) {
log(ERROR, "Avahi service publish failed");
return;
}
ret = avahi_entry_group_commit(m_group);
if (ret < 0) {
log(ERROR, "Avahi service commit failed");
return;
}
}
void CommMDNSPublisher::checkTimers(time_t t)
{
std::set<AvahiTimeout *>::const_iterator I = m_avahiTimeouts.begin();
std::set<AvahiTimeout *>::const_iterator Iend = m_avahiTimeouts.end();
for (; I != Iend; ++I) {
if ((*I)->m_state == AvahiTimeout::ENABLED &&
(*I)->m_expiry.tv_sec <= t) {
debug(std::cout << "TImeout " << (*I) << " is now due at " << t
2011-09-06 14:14:05 +01:00
<< std::endl << std::flush;);
(*I)->m_state = AvahiTimeout::EXPIRED;
(*I)->m_callback(*I, (*I)->m_userdata);
}
}
}
2014-01-31 13:36:03 +01:00
void CommMDNSPublisher::do_timer_check()
{
m_timers_check_timer.expires_from_now(boost::posix_time::milliseconds(500));
2014-01-31 13:36:03 +01:00
m_timers_check_timer.async_wait([this](boost::system::error_code ec)
{
this->checkTimers(0);
this->do_timer_check();
});
}
int CommMDNSPublisher::read()
{
assert(m_avahiWatch != 0);
m_avahiWatch->m_events = AVAHI_WATCH_IN;
2011-09-06 14:14:05 +01:00
m_avahiWatch->m_callback(m_avahiWatch,
m_avahiFd,
AVAHI_WATCH_IN,
m_avahiWatch->m_userdata);
m_avahiWatch->m_events = (AvahiWatchEvent)0;
return 0;
}
#endif // defined(HAVE_AVAHI)