[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-ha-dev
Subject:    [Linux-ha-dev] More spam :) (nonblocking readmsg?)
From:       bmartin () penguincomputing ! com
Date:       2001-02-27 0:39:41
[Download RAW message or body]

Well, I guess I'm attachment impared or something, but while I was 
discovering that the nonblocking readmsg wasn't doing what I wanted,
I remembered that I forgot to attach the files again :(

So how about that nonblocking readmsg?

From the code, if there is not already a queued message, then read_ha_msg
will return NULL.  

Maybe I don't correctly understand the queuing, but wouldn't it be more
useful if readmsg did a nonblocking read in this case?

Brian

P.S. - Yes, I remembered the files this time :)
["hbtest_working.c" (text/x-csrc)]

/* 
 * api_test: Test program for testing the heartbeat API
 *
 * Copyright (C) 2000 Alan Robertson <alanr@unix.sh>
 * 
 * This library 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.1 of the License, or (at your option) any later version.
 * 
 * This software 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <heartbeat.h>
#include <hb_api_core.h>
#include <hb_api.h>

/*
 * A heartbeat API test program...
 */

void NodeStatus(const char * node, const char * status, void * priv);
void LinkStatus(const char * node, const char *, const char *, void*);
void gotsig(int nsig);

void
NodeStatus(const char * node, const char * status, void * priv)
{
  fprintf(stderr, "Status update: Node %s now has status %s\n"
	  ,	node, status);
}

void
LinkStatus(const char * node, const char * lnk, const char * status
	   ,	void * priv)
{
  fprintf(stderr, "Link Status update: Link %s/%s now has status %s\n"
	  ,	node, lnk, status);
}

int quitnow = 0;
void gotsig(int nsig)
{
  (void)nsig;
  quitnow = 1;
}



int
main(int argc, char ** argv)
{
  struct ha_msg*	reply;
  struct ha_msg * m;
  unsigned	fmask;
  ll_cluster_t*	hb;
  const char *	node;
  const char *	intf;
  char * nodes[15];		
  int num_nodes = 0;
  int num_replies = 0;
  int master = 0;

  if(argc > 1) master = 1;

  (void)_heartbeat_h_Id;
  (void)_ha_msg_h_Id;

  hb = ll_cluster_new("heartbeat");
  fprintf(stderr, "PID=%d\n", getpid());
  fprintf(stderr, "Signing in with heartbeat\n");
  if (hb->llc_ops->signon(hb, "harry")!= HA_OK) {
    fprintf(stderr, "Cannot sign on with heartbeat\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(1);
  }

#if 0
  fmask = LLC_FILTER_RAW;
#else
  fmask = LLC_FILTER_DEFAULT;
#endif
  fprintf(stderr, "Setting message filter mode\n");
  if (hb->llc_ops->setfmode(hb, fmask) != HA_OK) {
    fprintf(stderr, "Cannot set filter mode\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(4);
  }

  fprintf(stderr, "Starting node walk\n");
  if (hb->llc_ops->init_nodewalk(hb) != HA_OK) {
    fprintf(stderr, "Cannot start node walk\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(5);
  }
  while((node = hb->llc_ops->nextnode(hb))!= NULL) {
    nodes[num_nodes++] = strdup(node);
    fprintf(stderr, "Cluster node: %s: status: %s\n", node
	    ,	hb->llc_ops->node_status(hb, node));
  }
  if (hb->llc_ops->end_nodewalk(hb) != HA_OK) {
    fprintf(stderr, "Cannot end node walk\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(8);
  }

  siginterrupt(SIGINT, 1);
  signal(SIGINT, gotsig);

  fprintf(stderr, "Setting message signal\n");
  if (hb->llc_ops->setmsgsignal(hb, 0) != HA_OK) {
    fprintf(stderr, "Cannot set message signal\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(9);
  }
				/* first broadcast    */
  if(master) {
    fprintf(stderr, "broadcasting challenge\n");
    m = ha_msg_new(1);
    ha_msg_add(m, F_TYPE, "challenge");
    hb->llc_ops->sendclustermsg(hb, m);
    ha_msg_del(m);
  }

  fprintf(stderr, "Waiting for messages...\n");
  errno = 0;

  for(; !quitnow && (reply=hb->llc_ops->readmsg(hb, 1)) != NULL;) {
    const char *	type;
    const char *	orig;
    if ((type = ha_msg_value(reply, F_TYPE)) == NULL) {
      type = "?";
    }
    if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
      orig = "?";
    }
    fprintf(stderr, "Got a message of type [%s] from [%s]\n", type, orig);
    if(strcmp(type, "challenge")==0) {
      struct ha_msg * msg = ha_msg_new(1);
      fprintf(stderr, "sending message test reply message\n"); 
      ha_msg_add(msg, F_TYPE, "reply");
      hb->llc_ops->sendnodemsg(hb, msg, orig);
      if(!master) quitnow=1;
    } else if(strcmp(type, "reply")==0) {
      if(++num_replies == num_nodes) {
	fprintf(stderr, "got all replies\n");
	quitnow = 1;
      }
    }
    ha_msg_del(reply); reply=NULL;
  }

  if (!quitnow) {
    perror("read_hb_msg returned NULL");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
  }
  if (hb->llc_ops->signoff(hb) != HA_OK) {
    fprintf(stderr, "Cannot sign off from heartbeat.\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(10);
  }
  if (hb->llc_ops->delete(hb) != HA_OK) {
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    fprintf(stderr, "Cannot delete API object.\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(11);
  }
  return 0;
}

["hbtest_not_working.c" (text/x-csrc)]

/* 
 * api_test: Test program for testing the heartbeat API
 *
 * Copyright (C) 2000 Alan Robertson <alanr@unix.sh>
 * 
 * This library 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.1 of the License, or (at your option) any later version.
 * 
 * This software 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 *
 *  broken test case 
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <stdarg.h>
#include <heartbeat.h>
#include <hb_api_core.h>
#include <hb_api.h>

/*
 * A heartbeat API test program...
 */

void NodeStatus(const char * node, const char * status, void * priv);
void LinkStatus(const char * node, const char *, const char *, void*);
void gotsig(int nsig);

void
NodeStatus(const char * node, const char * status, void * priv)
{
  fprintf(stderr, "Status update: Node %s now has status %s\n"
	  ,	node, status);
}

void
LinkStatus(const char * node, const char * lnk, const char * status
	   ,	void * priv)
{
  fprintf(stderr, "Link Status update: Link %s/%s now has status %s\n"
	  ,	node, lnk, status);
}

int quitnow = 0;
void gotsig(int nsig)
{
  (void)nsig;
  quitnow = 1;
}



int
main(int argc, char ** argv)
{
  int fd;
  struct ha_msg*	reply;
  struct ha_msg * m;
  unsigned	fmask;
  ll_cluster_t*	hb;
  const char *	node;
  const char *	intf;
  char * nodes[15];		
  int num_nodes = 0;
  int num_replies = 0;
  int master = 0;

  if(argc > 1) master = 1;

  (void)_heartbeat_h_Id;
  (void)_ha_msg_h_Id;

  hb = ll_cluster_new("heartbeat");
  fprintf(stderr, "PID=%d\n", getpid());
  fprintf(stderr, "Signing in with heartbeat\n");
  if (hb->llc_ops->signon(hb, "harry")!= HA_OK) {
    fprintf(stderr, "Cannot sign on with heartbeat\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(1);
  }

#if 0
  fmask = LLC_FILTER_RAW;
#else
  fmask = LLC_FILTER_DEFAULT;
#endif
  fprintf(stderr, "Setting message filter mode\n");
  if (hb->llc_ops->setfmode(hb, fmask) != HA_OK) {
    fprintf(stderr, "Cannot set filter mode\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(4);
  }

  fprintf(stderr, "Starting node walk\n");
  if (hb->llc_ops->init_nodewalk(hb) != HA_OK) {
    fprintf(stderr, "Cannot start node walk\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(5);
  }
  while((node = hb->llc_ops->nextnode(hb))!= NULL) {
    nodes[num_nodes++] = strdup(node);
    fprintf(stderr, "Cluster node: %s: status: %s\n", node
	    ,	hb->llc_ops->node_status(hb, node));
  }
  if (hb->llc_ops->end_nodewalk(hb) != HA_OK) {
    fprintf(stderr, "Cannot end node walk\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(8);
  }

  siginterrupt(SIGINT, 1);
  signal(SIGINT, gotsig);

  fprintf(stderr, "Setting message signal\n");
  if (hb->llc_ops->setmsgsignal(hb, 0) != HA_OK) {
    fprintf(stderr, "Cannot set message signal\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(9);
  }
				/* first broadcast    */
  if(master) {
    fprintf(stderr, "broadcasting challenge\n");
    m = ha_msg_new(1);
    ha_msg_add(m, F_TYPE, "challenge");
    hb->llc_ops->sendclustermsg(hb, m);
    ha_msg_del(m);
  }

  fprintf(stderr, "Waiting for messages...\n");
  errno = 0;

  fd = hb->llc_ops->inputfd(hb);

  while(!quitnow) {
    fd_set rset;
    FD_ZERO(&rset);
    FD_SET(fd, &rset);
    if(select(fd+1, &rset, NULL, NULL, NULL) == -1) {
      perror("select error:");
    } else if(FD_ISSET(fd, &rset)) {
      while((reply=hb->llc_ops->readmsg(hb, 0))!=NULL) {
	if(reply) {
	  const char *	type;
	  const char *	orig;
	  if ((type = ha_msg_value(reply, F_TYPE)) == NULL) {
	    type = "?";
	  }
	  if ((orig = ha_msg_value(reply, F_ORIG)) == NULL) {
	    orig = "?";
	  }
	  fprintf(stderr, "Got a message of type [%s] from [%s]\n", type, orig);
	  if(strcmp(type, "challenge")==0) {
	    struct ha_msg * msg = ha_msg_new(1);
	    fprintf(stderr, "sending message test reply message\n"); 
	    ha_msg_add(msg, F_TYPE, "reply");
	    hb->llc_ops->sendnodemsg(hb, msg, orig);
	    if(!master) quitnow=1;
	  } else if(strcmp(type, "reply")==0) {
	    if(++num_replies == num_nodes) {
	      fprintf(stderr, "got all replies\n");
	      quitnow = 1;
	    }
	  }
	  ha_msg_del(reply); reply=NULL;
	}
      }
    }
  }

  if (!quitnow) {
    perror("read_hb_msg returned NULL");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
  }
  if (hb->llc_ops->signoff(hb) != HA_OK) {
    fprintf(stderr, "Cannot sign off from heartbeat.\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(10);
  }
  if (hb->llc_ops->delete(hb) != HA_OK) {
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    fprintf(stderr, "Cannot delete API object.\n");
    fprintf(stderr, "REASON: %s\n", hb->llc_ops->errmsg(hb));
    exit(11);
  }
  return 0;
}

_______________________________________________________
Linux-HA-Dev: Linux-HA-Dev@lists.community.tummy.com
http://lists.community.tummy.com/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic