Annotation of libaitmqtt/example/cmds.c, revision 1.3.12.1

1.1       misho       1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <string.h>
                      4: #include <sys/types.h>
1.3.12.1! misho       5: #include <limits.h>
1.1       misho       6: #include <aitmqtt.h>
                      7: 
                      8: 
                      9: int
                     10: main()
                     11: {
                     12:        mqtt_msg_t *m;
                     13:        mqtt_subscr_t s[4], *ss;
                     14:        mqtthdr_connflgs_t flg;
                     15:        mqtthdr_connack_t cack;
                     16:        u_short ka, msgID;
                     17:        int i, len;
1.2       misho      18:        char cid[BUFSIZ], user[BUFSIZ], pass[BUFSIZ], topic[BUFSIZ], *t, *msg, *pub;
1.1       misho      19:        struct mqtthdr *hdr;
                     20:        u_char *qoses;
                     21: 
                     22:        m = mqtt_msgAlloc(0);
                     23:        /* conn* */
                     24:        printf("connect=%d/%d\n", m->msg_len, mqtt_msgCONNECT(m, "MRYN", 0, "aaaaa", NULL, "bbb", NULL, 0, 0, 0));
1.3       misho      25: //     for (i = 0; i < m->msg_len; i++)
                     26: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho      27:        t = msg = NULL;
                     28:        cack = mqtt_readCONNECT(m, &ka, cid, sizeof cid, user, sizeof user, pass, sizeof pass, &t, &msg);
                     29:        flg.flags = cack.reserved;
                     30:        printf("read connect %d flags:: clean=%d will=%d qos=%d retain=%d pass=%d user=%d\n", cack.retcode, 
                     31:                        flg.clean_sess, flg.will_flg, flg.will_qos, flg.will_retain, flg.password, flg.username);
                     32:        if (flg.reserved) {
                     33:                printf("Error:: mqtt_readCONNECT() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     34:                return 1;
                     35:        }
                     36:        printf("++> KA=%d sec, ConnID=%s User=%s Pass=%s Will_Topic=%s Will_Message=%s\n", ka, 
                     37:                        cid, user, pass, t, msg);
                     38:        if (t)
                     39:                free(t);
                     40:        if (msg)
                     41:                free(msg);
                     42:        printf("connack=%d/%d\n", m->msg_len, mqtt_msgCONNACK(m, 1));
1.3       misho      43: //     for (i = 0; i < m->msg_len; i++)
                     44: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho      45:        printf("read connack=%d\n", mqtt_readCONNACK(m));
                     46:        getchar();
                     47: 
                     48:        /* pub* */
                     49:        printf("publish=%d/%d\n", m->msg_len, mqtt_msgPUBLISH(m, "AAA/bbb/CCC/ddd", 7, 0, 2, 0, "OLE!!!", 7));
1.3       misho      50: //     for (i = 0; i < m->msg_len; i++)
                     51: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.2       misho      52:        if (!(len = mqtt_readPUBLISH(m, topic, sizeof topic, &msgID, (void**) &pub))) {
1.1       misho      53:                printf("Error:: mqtt_readPUBLISH() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     54:                return 2;
1.3       misho      55:        } else
                     56:                hdr = (struct mqtthdr*) m->msg_base;
1.1       misho      57:        printf("read publish: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
1.2       misho      58:        printf("++> topic=%s MessageID=%d DATA=(%d)%s\n", topic, msgID, len, pub);
                     59:        free(pub);
1.1       misho      60:        printf("puback=%d/%d\n", m->msg_len, mqtt_msgPUBACK(m, 10));
1.3       misho      61: //     for (i = 0; i < m->msg_len; i++)
                     62: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho      63:        printf("read puback=%d\n", mqtt_readPUBACK(m));
                     64:        printf("pubrec=%d/%d\n", m->msg_len, mqtt_msgPUBREC(m, 11));
1.3       misho      65: //     for (i = 0; i < m->msg_len; i++)
                     66: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho      67:        printf("read pubrec=%d\n", mqtt_readPUBREC(m));
                     68:        printf("pubrel=%d/%d\n", m->msg_len, mqtt_msgPUBREL(m, 12));
1.3       misho      69: //     for (i = 0; i < m->msg_len; i++)
                     70: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho      71:        printf("read pubrel=%d\n", mqtt_readPUBREL(m));
                     72:        printf("pubcomp=%d/%d\n", m->msg_len, mqtt_msgPUBCOMP(m, 13));
1.3       misho      73: //     for (i = 0; i < m->msg_len; i++)
                     74: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho      75:        printf("read pubcomp=%d\n", mqtt_readPUBCOMP(m));
                     76:        getchar();
                     77: 
                     78:        /* sub* */
                     79:        memset(s, 0, sizeof s);
                     80:        s[0].sub_topic.msg_len = 3;
                     81:        s[0].sub_topic.msg_base = "a/b";
                     82:        s[0].sub_ret = MQTT_QOS_ACK;
                     83:        s[1].sub_topic.msg_len = 3;
                     84:        s[1].sub_topic.msg_base = "c/d";
                     85:        s[1].sub_ret = MQTT_QOS_ONCE;
                     86:        s[2].sub_topic.msg_len = 7;
                     87:        s[2].sub_topic.msg_base = "x/y/z/Q";
                     88:        s[2].sub_ret = MQTT_QOS_EXACTLY;
                     89:        printf("subscribe=%d/%d\n", m->msg_len, mqtt_msgSUBSCRIBE(m, s, 10, 0, 0));
                     90:        for (i = 0; i < m->msg_len; i++)
                     91:                printf("%d\n", ((u_char*) m->msg_base)[i]);
1.2       misho      92:        if (mqtt_readSUBSCRIBE(m, &msgID, &ss) == -1) {
1.1       misho      93:                printf("Error:: mqtt_readSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                     94:                return 3;
                     95:        }
                     96:        printf("read subscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
                     97:        printf("++> MessageID=%d\n", msgID);
                     98:        for (i = 0; ss[i].sub_topic.msg_base; i++)
                     99:                printf(" >>> QoS=%d Topic(%d)=%s\n", ss[i].sub_ret, ss[i].sub_topic.msg_len, 
                    100:                                (char*) ss[i].sub_topic.msg_base);
                    101:        mqtt_subFree(&ss);
                    102:        printf("suback=%d/%d\n", m->msg_len, mqtt_msgSUBACK(m, s, 10));
1.3       misho     103: //     for (i = 0; i < m->msg_len; i++)
                    104: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho     105:        printf("read suback=%d\n", (len = mqtt_readSUBACK(m, &msgID, &qoses)));
                    106:        for (i = 0; i < len; i++)
                    107:                printf(" >>> QoS=%d\n", qoses[i]);
                    108:        free(qoses);
                    109:        printf("unsubscribe=%d/%d\n", m->msg_len, mqtt_msgUNSUBSCRIBE(m, s, 10, 0, 1));
1.3       misho     110: //     for (i = 0; i < m->msg_len; i++)
                    111: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.2       misho     112:        if (mqtt_readUNSUBSCRIBE(m, &msgID, &ss) == -1) {
1.1       misho     113:                printf("Error:: mqtt_readUNSUBSCRIBE() #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
                    114:                return 3;
                    115:        }
                    116:        printf("read unsubscribe: dup=%d qos=%d retain=%d\n", hdr->mqtt_msg.dup, hdr->mqtt_msg.qos, hdr->mqtt_msg.retain);
                    117:        printf("++> MessageID=%d\n", msgID);
                    118:        for (i = 0; ss[i].sub_topic.msg_base; i++)
                    119:                printf(" >>> Topic(%d)=%s\n", ss[i].sub_topic.msg_len, (char*) ss[i].sub_topic.msg_base);
                    120:        mqtt_subFree(&ss);
                    121:        printf("unsuback=%d/%d\n", m->msg_len, mqtt_msgUNSUBACK(m, 10));
1.3       misho     122: //     for (i = 0; i < m->msg_len; i++)
                    123: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho     124:        printf("read unsuback=%d\n", mqtt_readUNSUBACK(m));
                    125:        getchar();
                    126: 
                    127:        /* ping* */
                    128:        printf("pingreq=%d/%d\n", m->msg_len, mqtt_msgPINGREQ(m));
1.3       misho     129: //     for (i = 0; i < m->msg_len; i++)
                    130: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho     131:        printf("read pingreq=%d\n", mqtt_readPINGREQ(m));
                    132:        printf("pingresp=%d/%d\n", m->msg_len, mqtt_msgPINGRESP(m));
1.3       misho     133: //     for (i = 0; i < m->msg_len; i++)
                    134: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho     135:        printf("read pingresp=%d\n", mqtt_readPINGRESP(m));
                    136: 
                    137:        printf("disconnect=%d/%d\n", m->msg_len, mqtt_msgDISCONNECT(m));
1.3       misho     138: //     for (i = 0; i < m->msg_len; i++)
                    139: //             printf("%d\n", ((u_char*) m->msg_base)[i]);
1.1       misho     140:        printf("read disconnect=%d\n", mqtt_readDISCONNECT(m));
                    141: 
                    142:        mqtt_msgFree(&m, 42);
                    143:        return 0;
                    144: }

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