4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <avahi-core/core.h>
32 static AvahiEntryGroup *group = NULL;
33 static GMainLoop *main_loop = NULL;
34 static gchar *name = NULL;
36 static void create_services(AvahiServer *s);
38 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
42 /* Called whenever the entry group state changes */
44 if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
45 /* The entry group has been established successfully */
46 g_message("Service '%s' successfully established.", name);
48 else if (state == AVAHI_ENTRY_GROUP_COLLISION) {
51 /* A service name collision happened. Let's pick a new name */
52 n = avahi_alternative_service_name(name);
56 g_message("Service name collision, renaming service to '%s'", name);
58 /* And recreate the services */
63 static void create_services(AvahiServer *s) {
67 /* If this is the first time we're called, let's create a new entry group */
69 group = avahi_entry_group_new(s, entry_group_callback, NULL);
71 g_message("Adding service '%s'", name);
73 /* Create some random TXT data */
74 snprintf(r, sizeof(r), "random=%i", rand());
76 /* Add the service for IPP */
77 if (avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL) < 0) {
78 g_message("Failed to add _ipp._tcp service.");
79 g_main_loop_quit(main_loop);
83 /* Add the same service for BSD LPR */
84 if (avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL) < 0) {
85 g_message("Failed to add _printer._tcp service.");
86 g_main_loop_quit(main_loop);
90 /* Tell the server to register the service */
91 avahi_entry_group_commit(group);
94 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
97 /* Called whenever the server state changes */
99 if (state == AVAHI_SERVER_RUNNING)
100 /* The serve has startup successfully and registered its host
101 * name on the network, so it's time to create our services */
104 else if (state == AVAHI_SERVER_COLLISION) {
107 /* A host name collision happened. Let's pick a new name for the server */
108 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
109 g_message("Host name collision, retrying with '%s'", n);
110 avahi_server_set_host_name(s, n);
113 /* Let's drop our registered services. When the server is back
114 * in AVAHI_SERVER_RUNNING state we will register them
115 * again with the new host name. */
117 avahi_entry_group_reset(group);
121 int main(int argc, char*argv[]) {
122 AvahiServerConfig config;
123 AvahiServer *server = NULL;
127 name = g_strdup("MegaPrinter");
129 /* Let's set the host name for this server. */
130 avahi_server_config_init(&config);
131 config.host_name = g_strdup("gurkiman");
132 config.publish_workstation = FALSE;
134 /* Allocate a new server */
135 server = avahi_server_new(NULL, &config, server_callback, NULL);
137 /* Free the configuration data */
138 avahi_server_config_free(&config);
140 /* Run the main loop */
141 main_loop = g_main_loop_new(NULL, FALSE);
142 g_main_loop_run(main_loop);
146 avahi_entry_group_free(group);
149 avahi_server_free(server);
152 g_main_loop_unref(main_loop);