Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / apps / gdp-find-xname.c @ master

History | View | Annotate | Download (3.42 KB)

1
/* vim: set ai sw=4 sts=4 ts=4 : */
2

    
3

    
4
/*
5
**  GDP-FIND-XNAME --- find the human name of a log
6
**
7
**                ... as long as it is listed in the metadata.
8
**
9
**        ----- BEGIN LICENSE BLOCK -----
10
**        Applications for the Global Data Plane
11
**        From the Ubiquitous Swarm Lab, 490 Cory Hall, U.C. Berkeley.
12
**
13
**        Copyright (c) 2015-2019, Regents of the University of California.
14
**        All rights reserved.
15
**
16
**        Permission is hereby granted, without written agreement and without
17
**        license or royalty fees, to use, copy, modify, and distribute this
18
**        software and its documentation for any purpose, provided that the above
19
**        copyright notice and the following two paragraphs appear in all copies
20
**        of this software.
21
**
22
**        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
23
**        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
24
**        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
25
**        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
**
27
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
28
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29
**        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
30
**        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
31
**        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
32
**        OR MODIFICATIONS.
33
**        ----- END LICENSE BLOCK -----
34
*/
35

    
36
#include <gdp/gdp.h>
37

    
38
#include <ep/ep_app.h>
39
#include <ep/ep_dbg.h>
40

    
41
#include <sysexits.h>
42

    
43
/*
44
**  Given an internal (base64-encoded) log name, check the metadata
45
**  and print the external (human) name.
46
*/
47

    
48
void
49
usage(void)
50
{
51
        fprintf(stderr,
52
                        "Usage: %s [-D dbg_spec] [-G router_addr] log-name\n"
53
                        "    -D  set debugging flags\n"
54
                        "    -G  IP host to contact for gdp_router\n",
55
                        ep_app_getprogname());
56
        exit(EX_USAGE);
57
}
58

    
59

    
60
int
61
main(int argc, char **argv)
62
{
63
        int opt;
64
        bool show_usage = false;
65
        EP_STAT estat;
66
        gdp_gin_t *gin;
67
        char *gdpd_addr = NULL;
68
        char buf[200];
69
        gdp_name_t gdpiname;
70

    
71
        while ((opt = getopt(argc, argv, "D:G:")) > 0)
72
        {
73
                switch (opt)
74
                {
75
                 case 'D':
76
                        ep_dbg_set(optarg);
77
                        break;
78

    
79
                 case 'G':
80
                        gdpd_addr = optarg;
81
                        break;
82

    
83
                 default:
84
                        show_usage = true;
85
                        break;
86
                }
87
        }
88
        argc -= optind;
89
        argv += optind;
90

    
91
        if (show_usage || argc != 1)
92
                usage();
93

    
94
        // initialize GDP library
95
        estat = gdp_init(gdpd_addr);
96
        if (!EP_STAT_ISOK(estat))
97
        {
98
                ep_app_error("GDP Initialization failed");
99
                goto fail0;
100
        }
101

    
102
        // allow thread to settle to avoid interspersed debug output
103
        ep_time_nanosleep(INT64_C(100000000));
104

    
105
        estat = gdp_parse_name(argv[0], gdpiname);
106
        if (EP_STAT_ISFAIL(estat))
107
                ep_app_fatal("Cannot parse log name %s", argv[0]);
108
        estat = gdp_gin_open(gdpiname, GDP_MODE_RO, NULL, &gin);
109
        EP_STAT_CHECK(estat, goto fail1);
110

    
111
        gdp_md_t *md;
112
        estat = gdp_gin_getmetadata(gin, &md);
113
        EP_STAT_CHECK(estat, goto fail2);
114

    
115
        int indx;
116
        for (indx = 0; EP_STAT_ISOK(estat); indx++)
117
        {
118
                gdp_md_id_t id;
119
                size_t len;
120
                const void *data;
121

    
122
                estat = gdp_md_get(md, indx, &id, &len, &data);
123
                if (EP_STAT_ISOK(estat) && id == GDP_MD_XID)
124
                {
125
                        // found the name
126
                        printf("%.*s\n", (int) len, (const char *) data);
127
                        break;
128
                }
129
        }
130

    
131
        if (!EP_STAT_ISOK(estat))
132
                printf("No name found\n");
133

    
134
fail2:
135
        // close logs / release resources
136
        //XXX someday
137

    
138
        if (false)
139
        {
140
fail1:
141
                ep_app_error("Could not open log %s: %s",
142
                                argv[0], ep_stat_tostr(estat, buf, sizeof buf));
143
        }
144

    
145
fail0:
146
        ep_app_message(estat, "exiting with status");
147
        return EP_STAT_ISOK(estat) ? EX_OK : EX_UNAVAILABLE;
148
}