candidate.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 150 行

C
150
字号
#ifndef lintstatic	char	*sccsid = "@(#)candidate.c	4.1	(ULTRIX)	7/2/90";#endif lint/************************************************************************ *									* *			Copyright (c) 1988 by				* *		Digital Equipment Corporation, Maynard, MA		* *			All rights reserved.				* *									* *   This software is furnished under a license and may be used and	* *   copied  only  in accordance with the terms of such license and	* *   with the  inclusion  of  the  above  copyright  notice.   This	* *   software  or  any  other copies thereof may not be provided or	* *   otherwise made available to any other person.  No title to and	* *   ownership of the software is hereby transferred.			* *									* *   This software is  derived  from  software  received  from  the	* *   University    of   California,   Berkeley,   and   from   Bell	* *   Laboratories.  Use, duplication, or disclosure is  subject  to	* *   restrictions  under  license  agreements  with  University  of	* *   California and with AT&T.						* *									* *   The information in this software is subject to change  without	* *   notice  and should not be construed as a commitment by Digital	* *   Equipment Corporation.						* *									* *   Digital assumes no responsibility for the use  or  reliability	* *   of its software on equipment which is not supplied by Digital.	* *									* ************************************************************************//* * Copyright (c) 1983 Regents of the University of California. * All rights reserved.  The Berkeley software License Agreement * specifies the terms and conditions for redistribution. *static char sccsid[] = "@(#)candidate.c	2.3 (Berkeley) 4/21/86"; */#include "globals.h"#include <protocols/timed.h>#define ELECTIONWAIT	3	/* seconds *//* * `election' candidates a host as master: it is called by a slave  * which runs with the -M option set when its election timeout expires.  * Note the conservative approach: if a new timed comes up, or another * candidate sends an election request, the candidature is withdrawn. */election(net)struct netinfo *net;{	int ret;	struct tsp *resp, msg, *readmsg();	struct timeval wait;	struct tsp *answer, *acksend();	long casual();	struct sockaddr_in server;	syslog(LOG_INFO, "THIS MACHINE IS A CANDIDATE");	if (trace) {		fprintf(fd, "THIS MACHINE IS A CANDIDATE\n");	}	ret = MASTER;	slvcount = 1;	msg.tsp_type = TSP_ELECTION;	msg.tsp_vers = TSPVERSION;	(void)strcpy(msg.tsp_name, hostname);	bytenetorder(&msg);	if (sendto(sock, (char *)&msg, sizeof(struct tsp), 0, &net->dest_addr,	    sizeof(struct sockaddr_in)) < 0) {		syslog(LOG_ERR, "sendto: %m");		exit(1);	}	do {		wait.tv_sec = ELECTIONWAIT;		wait.tv_usec = 0;		resp = readmsg(TSP_ANY, (char *)ANYADDR, &wait, net);		if (resp != NULL) {			switch (resp->tsp_type) {			case TSP_ACCEPT:				(void) addmach(resp->tsp_name, &from);				break;			case TSP_MASTERUP:			case TSP_MASTERREQ:				/*				 * If a timedaemon is coming up at the same time,				 * give up the candidature: it will be the master.				 */				ret = SLAVE;				break;			case TSP_QUIT:			case TSP_REFUSE:				/*				 * Collision: change value of election timer 				 * using exponential backoff.				 * The value of timer will be recomputed (in slave.c)				 * using the original interval when election will 				 * be successfully completed.				 */				backoff *= 2;				delay2 = casual((long)MINTOUT, 							(long)(MAXTOUT * backoff));				ret = SLAVE;				break;			case TSP_ELECTION:				/* no master for another round */				msg.tsp_type = TSP_REFUSE;				(void)strcpy(msg.tsp_name, hostname);				server = from;				answer = acksend(&msg, &server, resp->tsp_name,				    TSP_ACK, (struct netinfo *)NULL);				if (answer == NULL) {					syslog(LOG_ERR, "error in election");				} else {					(void) addmach(resp->tsp_name, &from);				}				break;			case TSP_SLAVEUP:				(void) addmach(resp->tsp_name, &from);				break;			case TSP_SETDATE:			case TSP_SETDATEREQ:				break;			default:				if (trace) {					fprintf(fd, "candidate: ");					print(resp, &from);				}				break;			}		} else {			break;		}	} while (ret == MASTER);	return(ret);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?