1/* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <stdio.h>			// For printf()
19#include <stdlib.h>			// For exit() etc.
20#include <string.h>			// For strlen() etc.
21#include <unistd.h>			// For select()
22#include <errno.h>			// For errno, EINTR
23#include <netinet/in.h>		// For INADDR_NONE
24#include <arpa/inet.h>		// For inet_addr()
25#include <netdb.h>			// For gethostbyname()
26#include <signal.h>			// For SIGINT, etc.
27
28#include "mDNSEmbeddedAPI.h"  // Defines the interface to the client layer above
29#include "mDNSPosix.h"      // Defines the specific types needed to run mDNS on this platform
30
31//*******************************************************************************************
32// Main
33
34static volatile mDNSBool StopNow;
35
36mDNSlocal void HandleSIG(int signal)
37	{
38	(void)signal;	// Unused
39	debugf("%s","");
40	debugf("HandleSIG");
41	StopNow = mDNStrue;
42	}
43
44mDNSexport void ExampleClientEventLoop(mDNS *const m)
45	{
46	signal(SIGINT, HandleSIG);	// SIGINT is what you get for a Ctrl-C
47	signal(SIGTERM, HandleSIG);
48
49	while (!StopNow)
50		{
51		int nfds = 0;
52		fd_set readfds;
53		struct timeval timeout;
54		int result;
55
56		// 1. Set up the fd_set as usual here.
57		// This example client has no file descriptors of its own,
58		// but a real application would call FD_SET to add them to the set here
59		FD_ZERO(&readfds);
60
61		// 2. Set up the timeout.
62		// This example client has no other work it needs to be doing,
63		// so we set an effectively infinite timeout
64		timeout.tv_sec = 0x3FFFFFFF;
65		timeout.tv_usec = 0;
66
67		// 3. Give the mDNSPosix layer a chance to add its information to the fd_set and timeout
68		mDNSPosixGetFDSet(m, &nfds, &readfds, &timeout);
69
70		// 4. Call select as normal
71		verbosedebugf("select(%d, %d.%06d)", nfds, timeout.tv_sec, timeout.tv_usec);
72		result = select(nfds, &readfds, NULL, NULL, &timeout);
73
74		if (result < 0)
75			{
76			verbosedebugf("select() returned %d errno %d", result, errno);
77			if (errno != EINTR) StopNow = mDNStrue;
78			}
79		else
80			{
81			// 5. Call mDNSPosixProcessFDSet to let the mDNSPosix layer do its work
82			mDNSPosixProcessFDSet(m, &readfds);
83
84			// 6. This example client has no other work it needs to be doing,
85			// but a real client would do its work here
86			// ... (do work) ...
87			}
88		}
89
90	debugf("Exiting");
91	}
92