File: tutorial-19-custom_filter.cc

package info (click to toggle)
srpc 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,100 kB
  • sloc: cpp: 23,170; python: 10; makefile: 8; sh: 6
file content (131 lines) | stat: -rw-r--r-- 3,235 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
/*
  Copyright (c) 2024 sogou, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include <signal.h>
#include "echo_pb.srpc.h"
#include "workflow/WFFacilities.h"
#include "srpc/rpc_types.h"

#include "srpc/rpc_module.h"
#include "srpc/rpc_basic.h"

using namespace srpc;

static WFFacilities::WaitGroup wait_group(1);

// Filter is available for both server and client.
// Please choose the function to implement the logic at the corresponding time.
class MyFilter : public RPCFilter
{
public:
	MyFilter() : RPCFilter(RPCModuleTypeCustom)
	{
	}

	bool server_begin(SubTask *task, RPCModuleData& data) override
	{	
		auto iter = data.find("my_auth_key");
		if (iter != data.end() && iter->second.compare("my_auth_value") == 0)
		{
			fprintf(stderr, "[FILTER] auth success : %s\n", iter->second.c_str());
			return true;
		}

		fprintf(stderr, "[FILTER] auth failed : %s\n",
				iter == data.end() ? "No auth" : iter->second.c_str());
		return false;
	}
};

class ExampleServiceImpl : public Example::Service
{
public:
	void Echo(EchoRequest *req, EchoResponse *resp, RPCContext *ctx) override
	{
		resp->set_message("Hi back");
		fprintf(stderr, "[SERVER] Echo() get req: %s\n", req->message().c_str());
	}
};

static void sig_handler(int signo)
{
	wait_group.done();
}

void send_client_task()
{
	Example::SRPCClient client("127.0.0.1", 1412);

	auto callback = [](EchoResponse *resp, RPCContext *ctx)
	{
		if (ctx->success())
			fprintf(stderr, "[CLIENT] callback success\n");
		else
			fprintf(stderr, "[CLIENT] callback status[%d] error[%d] errmsg : %s\n",
					ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
	};

	EchoRequest req;
	req.set_name("Tutorial 19");

	// send one task to test the success case
	auto *task = client.create_Echo_task(callback);
	req.set_message("For success case");
	task->serialize_input(&req);
	task->add_baggage("my_auth_key", "my_auth_value");
	task->start();

	// send another task to test the failure case
	task = client.create_Echo_task(callback);
	req.set_message("For failure case");
	task->serialize_input(&req);
	task->add_baggage("my_auth_key", "randomxxx");
	task->start();
}

int main()
{
	GOOGLE_PROTOBUF_VERIFY_VERSION;
	signal(SIGINT, sig_handler);
	signal(SIGTERM, sig_handler);

	// 1. prepare server
	SRPCServer server;
	ExampleServiceImpl impl;
	server.add_service(&impl);

	// 2. add filter into server
	MyFilter my;
	server.add_filter(&my);

	// 3. run server
	if (server.start(1412) == 0)
	{
		fprintf(stderr, "[SERVER] Server with filter is running on 1412\n");

		// 4. send client task to test
		send_client_task();

		wait_group.wait();
		server.stop();
	}
	else
		perror("[SERVER] server start");

	google::protobuf::ShutdownProtobufLibrary();
	return 0;
}