File: tutorial-15-name_service.cc

package info (click to toggle)
workflow 0.11.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,744 kB
  • sloc: cpp: 33,792; ansic: 9,393; makefile: 9; sh: 6
file content (175 lines) | stat: -rw-r--r-- 3,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <string>
#include "workflow/WFGlobal.h"
#include "workflow/WFNameService.h"
#include "workflow/WFTaskFactory.h"
#include "workflow/WFFacilities.h"
#include "workflow/HttpUtil.h"

// The example domonstrate the simplest user defined naming policy.

/* 'MyNSPolicy' is a naming policy, which use local file for naming.
 * The format of naming file is similar to 'hosts' file, but we allow
 * domain name and IP address as destination. For example:
 *
 * 127.0.0.1 localhost
 * 127.0.0.1 mydomain  # another alias for 127.0.0.1
 * www.sogou.com sogou # sogou -> www.sogou.com
 */
class MyNSPolicy : public WFNSPolicy
{
public:
	WFRouterTask *create_router_task(const struct WFNSParams *params,
									 router_callback_t callback) override;

private:
	std::string path;

private:
	std::string read_from_fp(FILE *fp, const char *name);
	std::string parse_line(char *p, const char *name);

public:
	MyNSPolicy(const char *naming_file) : path(naming_file) { }
};

std::string MyNSPolicy::parse_line(char *p, const char *name)
{
	const char *dest = NULL;
	char *start;

	start = p;
	while (*start != '\0' && *start != '#')
		start++;
	*start = '\0';

	while (1)
	{
		while (isspace(*p))
			p++;

		start = p;
		while (*p != '\0' && !isspace(*p))
			p++;

		if (start == p)
			break;

		if (*p != '\0')
			*p++ = '\0';

		if (dest == NULL)
		{
			dest = start;
			continue;
		}

		if (strcasecmp(name, start) == 0)
			return std::string(dest);
	}

	return std::string();
}

std::string MyNSPolicy::read_from_fp(FILE *fp, const char *name)
{
	char *line = NULL;
	size_t bufsize = 0;
	std::string result;

	while (getline(&line, &bufsize, fp) > 0)
	{
		result = this->parse_line(line, name);
		if (result.size() > 0)
			break;
	}

	free(line);
	return result;
}

WFRouterTask *MyNSPolicy::create_router_task(const struct WFNSParams *params,
											 router_callback_t callback)
{
	WFDnsResolver *dns_resolver = WFGlobal::get_dns_resolver();

	if (params->uri.host)
	{
		FILE *fp = fopen(this->path.c_str(), "r");
		if (fp)
		{
			std::string dest = this->read_from_fp(fp, params->uri.host);
			if (dest.size() > 0)
			{
				/* Update the uri structure's 'host' field directly.
				 * You can also update the 'port' field if needed. */
				free(params->uri.host);
				params->uri.host = strdup(dest.c_str());
			}

			fclose(fp);
		}
	}

	/* Simply, use the global dns resolver to create a router task. */
	return dns_resolver->create_router_task(params, std::move(callback));
}

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		fprintf(stderr, "USAGE: %s <http url> <naming file>\n", argv[0]);
		exit(1);
	}

	ParsedURI uri;
	URIParser::parse(argv[1], uri);
	char *name = uri.host;
	if (name == NULL)
	{
		fprintf(stderr, "Invalid http URI\n");
		exit(1);
	}

	/* Create an naming policy. */
	MyNSPolicy *policy = new MyNSPolicy(argv[2]);

	/* Get the global name service object.*/
	WFNameService *ns = WFGlobal::get_name_service();

	/* Add the our name with policy to global name service.
	 * You can add mutilply names with one policy object. */
	ns->add_policy(name, policy);

	WFFacilities::WaitGroup wg(1);
	WFHttpTask *task = WFTaskFactory::create_http_task(argv[1], 2, 3,
		[&wg](WFHttpTask *task) {
			int state = task->get_state();
			int error = task->get_error();
			if (state != WFT_STATE_SUCCESS)
			{
				fprintf(stderr, "error: %s\n",
						WFGlobal::get_error_string(state, error));
			}
			else
			{
				auto *r = task->get_resp();
				std::string body = protocol::HttpUtil::decode_chunked_body(r);
				fwrite(body.c_str(), 1, body.size(), stdout);
			}
			wg.done();
		});

	task->start();
	wg.wait();

	/* clean up */
	ns->del_policy(name);
	delete policy;
	return 0;
}