1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file rpc_server_env.cc
22 * \brief Server environment of the RPC.
23 */
24#include <tvm/runtime/registry.h>
25
26#include "../file_utils.h"
27
28namespace tvm {
29namespace runtime {
30
31std::string RPCGetPath(const std::string& name) {
32 // do live lookup everytime as workpath can change.
33 const PackedFunc* f = runtime::Registry::Get("tvm.rpc.server.workpath");
34 ICHECK(f != nullptr) << "require tvm.rpc.server.workpath";
35 return (*f)(name);
36}
37
38TVM_REGISTER_GLOBAL("tvm.rpc.server.upload").set_body([](TVMArgs args, TVMRetValue* rv) {
39 std::string file_name = RPCGetPath(args[0]);
40 std::string data = args[1];
41 SaveBinaryToFile(file_name, data);
42});
43
44TVM_REGISTER_GLOBAL("tvm.rpc.server.download").set_body([](TVMArgs args, TVMRetValue* rv) {
45 std::string file_name = RPCGetPath(args[0]);
46 std::string data;
47 LoadBinaryFromFile(file_name, &data);
48 TVMByteArray arr;
49 arr.data = data.c_str();
50 arr.size = data.length();
51 LOG(INFO) << "Download " << file_name << "... nbytes=" << arr.size;
52 *rv = arr;
53});
54
55TVM_REGISTER_GLOBAL("tvm.rpc.server.remove").set_body([](TVMArgs args, TVMRetValue* rv) {
56 std::string file_name = RPCGetPath(args[0]);
57 RemoveFile(file_name);
58});
59
60} // namespace runtime
61} // namespace tvm
62