2
* Copyright (c) 2016-present, Facebook, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// This binary provides an easy way to open a zeromq server and feeds data to
18
// clients connect to it. It uses the Caffe2 db as the backend, thus allowing
19
// one to convert any db-compliant storage to a zeromq service.
21
#include "caffe2/core/db.h"
22
#include "caffe2/core/init.h"
23
#include "caffe2/core/logging.h"
24
#include "caffe2/utils/zmq_helper.h"
26
C10_DEFINE_string(server, "tcp://*:5555", "The server address.");
27
C10_DEFINE_string(input_db, "", "The input db.");
28
C10_DEFINE_string(input_db_type, "", "The input db type.");
31
int main(int argc, char** argv) {
32
caffe2::GlobalInit(&argc, &argv);
34
LOG(INFO) << "Opening DB...";
35
auto in_db = caffe2::db::CreateDB(
36
FLAGS_input_db_type, FLAGS_input_db, caffe2::db::READ);
39
"Cannot load input db " + FLAGS_input_db + " of expected type " +
41
auto cursor = in_db->NewCursor();
42
LOG(INFO) << "DB opened.";
44
LOG(INFO) << "Starting ZeroMQ server...";
46
// Socket to talk to clients
47
caffe2::ZmqSocket sender(ZMQ_PUSH);
48
sender.Bind(FLAGS_server);
49
LOG(INFO) << "Server created at " << FLAGS_server;
52
VLOG(1) << "Sending " << cursor->key();
53
sender.SendTillSuccess(cursor->key(), ZMQ_SNDMORE);
54
sender.SendTillSuccess(cursor->value(), 0);
56
if (!cursor->Valid()) {
57
cursor->SeekToFirst();
60
// We do not do an elegant quit since this binary is going to be terminated by