File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / client.c
Revision 1.1.2.10: download - view: text, annotated - select for diffs - revision graph
Thu Dec 29 14:13:13 2011 UTC (12 years, 6 months ago) by misho
Branches: mqtt1_0
fix mqttd to handle sigpipe :)
add file mgmt to publish

    1: #include "global.h"
    2: #include "mqtt.h"
    3: 
    4: 
    5: int
    6: InitClient(void)
    7: {
    8: 	int sock;
    9: 
   10: 	sock = socket(args->addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
   11: 	if (sock == -1) {
   12: 		printf("Error:: socket() #%d - %s\n", errno, strerror(errno));
   13: 		return -1;
   14: 	}
   15: 	if (connect(sock, &args->addr.sa, args->addr.sa.sa_len) == -1) {
   16: 		printf("Error:: connect() #%d - %s\n", errno, strerror(errno));
   17: 		return -1;
   18: 	}
   19: 
   20: 	return sock;
   21: }
   22: 
   23: int
   24: ConnectClient(int sock)
   25: {
   26: 	int siz = 0;
   27: 	struct pollfd pfd;
   28: 
   29: 	siz = mqtt_msgCONNECT(args->msg, (char*) AIT_GET_STR(&args->ConnID), args->ka, 
   30: 			(char*) AIT_GET_STR(&args->User), (char*) AIT_GET_STR(&args->Pass), 
   31: 			(char*) args->Will.Topic.val.string, (char*) args->Will.Msg.val.string, 
   32: 			!args->notClear, args->QoS, args->Retain);
   33: 	if (siz == -1) {
   34: 		printf("Error:: msgCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   35: 		return -1;
   36: 	}
   37: 
   38: 	siz = send(sock, args->msg->msg_base, siz, 0);
   39: 	if (siz == -1) {
   40: 		printf("Error:: send() #%d - %s\n", errno, strerror(errno));
   41: 		return -1;
   42: 	} else
   43: 		ioVERBOSE(3) printf("Sended CONNECT %d bytes\n", siz);
   44: 
   45: 	pfd.fd = sock;
   46: 	pfd.events = POLLIN | POLLPRI;
   47: 	switch (poll(&pfd, 1, args->ka * 1000)) {
   48: 		case -1:
   49: 			printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
   50: 			return -1;
   51: 		case 0:
   52: 			ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
   53: 			return -1;
   54: 	}
   55: 	if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
   56: 		return -1;
   57: 
   58: 	siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
   59: 	if (siz == -1) {
   60: 		printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
   61: 		return -1;
   62: 	} else
   63: 		ioVERBOSE(3) printf("Received %d bytes\n", siz);
   64: 
   65: 	return (u_char) mqtt_readCONNACK(args->msg);
   66: }
   67: 
   68: int
   69: CloseClient(int sock)
   70: {
   71: 	int siz = 0;
   72: 
   73: 	siz = mqtt_msgDISCONNECT(args->msg);
   74: 	if (siz == -1) {
   75: 		printf("Error:: msgDISCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   76: 		return -1;
   77: 	}
   78: 
   79: 	siz = send(sock, args->msg->msg_base, siz, 0);
   80: 	if (siz == -1) {
   81: 		printf("Error:: send() #%d - %s\n", errno, strerror(errno));
   82: 		return -1;
   83: 	} else
   84: 		ioVERBOSE(3) printf("Sended DISCONNECT %d bytes\n", siz);
   85: 
   86: 	close(sock);
   87: 	return siz;
   88: }
   89: 
   90: int
   91: KAClient(int sock)
   92: {
   93: 	int siz = 0;
   94: 	struct pollfd pfd;
   95: 
   96: 	siz = mqtt_msgPINGREQ(args->msg);
   97: 	if (siz == -1) {
   98: 		printf("Error:: msgPINGREQ #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
   99: 		return -1;
  100: 	}
  101: 
  102: 	siz = send(sock, args->msg->msg_base, siz, 0);
  103: 	if (siz == -1) {
  104: 		printf("Error:: send() #%d - %s\n", errno, strerror(errno));
  105: 		return -1;
  106: 	} else
  107: 		ioVERBOSE(3) printf("Sended PINGREQ %d bytes\n", siz);
  108: 
  109: 	pfd.fd = sock;
  110: 	pfd.events = POLLIN | POLLPRI;
  111: 	switch (poll(&pfd, 1, args->ka * 1000)) {
  112: 		case -1:
  113: 			printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
  114: 			return -1;
  115: 		case 0:
  116: 			ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
  117: 			return -1;
  118: 	}
  119: 	if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
  120: 		return -1;
  121: 
  122: 	siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
  123: 	if (siz == -1) {
  124: 		printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
  125: 		return -1;
  126: 	} else
  127: 		ioVERBOSE(3) printf("Received %d bytes\n", siz);
  128: 
  129: 	return (u_char) mqtt_readPINGRESP(args->msg);
  130: }
  131: 
  132: FILE *
  133: OpenFile(void)
  134: {
  135: 	if (!args->isFile)
  136: 		return NULL;
  137: 
  138: 	args->isFile = fopen(AIT_GET_STR(&args->Value), "r");
  139: 	if (!args->isFile) {
  140: 		printf("Error:: in open file #%d - %s\n", errno, strerror(errno));
  141: 		return NULL;
  142: 	}
  143: 
  144: 	return args->isFile;
  145: }
  146: 
  147: void
  148: CloseFile(void)
  149: {
  150: 	if (args->isFile)
  151: 		fclose(args->isFile);
  152: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>