diff options
author | 2018-05-11 12:20:11 -0700 | |
---|---|---|
committer | 2018-05-11 12:20:11 -0700 | |
commit | 4fad281ce8affe27fb7428f264d2c3b9dfc45f2f (patch) | |
tree | ca96c9efd69afec56aa2e5fe072a9f758247d0a3 /examples | |
parent | ec445cc2bb270ed4acb1c710c3533fca14a50019 (diff) | |
parent | 61fdb46ac456027c79841949272ec540f66d2317 (diff) |
Merge remote-tracking branch 'upstream/master' into fork_exec_ctx_check
Diffstat (limited to 'examples')
17 files changed, 259 insertions, 154 deletions
diff --git a/examples/android/helloworld/app/CMakeLists.txt b/examples/android/helloworld/app/CMakeLists.txt index 6ee18daaab..f449a5ce2e 100644 --- a/examples/android/helloworld/app/CMakeLists.txt +++ b/examples/android/helloworld/app/CMakeLists.txt @@ -12,36 +12,6 @@ file(MAKE_DIRECTORY ${GRPC_BUILD_DIR}) add_subdirectory(${GRPC_SRC_DIR} ${GRPC_BUILD_DIR}) -include_directories(${GRPC_SRC_DIR}/include) - -add_library(libgrpc STATIC IMPORTED) -set_target_properties(libgrpc PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/libgrpc.a) - -add_library(libgrpc++ STATIC IMPORTED) -set_target_properties(libgrpc++ PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/libgrpc++.a) - -add_library(libgpr STATIC IMPORTED) -set_target_properties(libgpr PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/libgpr.a) - -add_library(libcares STATIC IMPORTED) -set_target_properties(libcares PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/third_party/cares/cares/lib/libcares.a) - -add_library(libzlib STATIC IMPORTED) -set_target_properties(libzlib PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/third_party/zlib/libz.a) - -add_library(libcrypto STATIC IMPORTED) -set_target_properties(libcrypto PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/third_party/boringssl/crypto/libcrypto.a) - -add_library(libssl STATIC IMPORTED) -set_target_properties(libssl PROPERTIES IMPORTED_LOCATION - ${GRPC_BUILD_DIR}/third_party/boringssl/ssl/libssl.a) - set(GRPC_PROTO_GENS_DIR ${CMAKE_BINARY_DIR}/gens) file(MAKE_DIRECTORY ${GRPC_PROTO_GENS_DIR}) include_directories(${GRPC_PROTO_GENS_DIR}) @@ -96,8 +66,8 @@ add_library(helloworld_proto_lib SHARED ${HELLOWORLD_PROTO_HDRS} ${HELLOWORLD_PROTO_SRCS}) target_link_libraries(helloworld_proto_lib + grpc++ libprotobuf - libgrpc++ android log) @@ -111,13 +81,6 @@ target_include_directories(grpc-helloworld PRIVATE ${HELLOWORLD_PROTO_HEADERS}) target_link_libraries(grpc-helloworld - libgrpc++ - libgrpc - libzlib - libcares - libssl - libcrypto helloworld_proto_lib - libgpr android ${log-lib}) diff --git a/examples/android/helloworld/app/build.gradle b/examples/android/helloworld/app/build.gradle index c6ab7308ae..1d09fdc180 100644 --- a/examples/android/helloworld/app/build.gradle +++ b/examples/android/helloworld/app/build.gradle @@ -11,6 +11,13 @@ android { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { + // The paths to the protoc and grpc_cpp_plugin binaries on the host system (codegen + // is not cross-compiled to Android) + def protoc = project.hasProperty('protoc') ? + project.property('protoc') : '/usr/local/bin/protoc' + def grpc_cpp_plugin = project.hasProperty('grpc_cpp_plugin') ? + project.property('grpc_cpp_plugin') : '/usr/local/bin/grpc_cpp_plugin' + cppFlags "-std=c++14 -frtti -fexceptions" arguments '-DANDROID_STL=c++_static' arguments '-DRUN_HAVE_POSIX_REGEX=0' @@ -18,12 +25,8 @@ android { arguments '-DRUN_HAVE_STEADY_CLOCK=0' arguments '-Dprotobuf_BUILD_PROTOC_BINARIES=off' arguments '-DgRPC_BUILD_CODEGEN=off' - // Set this to the path to the protoc binary on the host system (codegen is not - // cross-compiled to Android) - arguments '-Dhelloworld_PROTOBUF_PROTOC_EXECUTABLE=/usr/local/bin/protoc' - // Set this to the path to the gRPC C++ protoc plugin binary on the host system - // (codegen is not cross-compiled to Android) - arguments '-Dhelloworld_GRPC_CPP_PLUGIN_EXECUTABLE=/usr/local/bin/grpc_cpp_plugin' + arguments '-Dhelloworld_PROTOBUF_PROTOC_EXECUTABLE=' + protoc + arguments '-Dhelloworld_GRPC_CPP_PLUGIN_EXECUTABLE=' + grpc_cpp_plugin } } ndk.abiFilters 'x86' diff --git a/examples/cpp/helloworld/CMakeLists.txt b/examples/cpp/helloworld/CMakeLists.txt index c3ce4d5ba6..d0f705f6d9 100644 --- a/examples/cpp/helloworld/CMakeLists.txt +++ b/examples/cpp/helloworld/CMakeLists.txt @@ -1,7 +1,24 @@ -# Minimum CMake required
+# Copyright 2018 gRPC authors.
+#
+# 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.
+#
+# cmake build file for C++ helloworld example.
+# Assumes protobuf and gRPC have been installed using cmake.
+# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
+# that automatically builds all the dependencies before building helloworld.
+
cmake_minimum_required(VERSION 2.8)
-# Project
project(HelloWorld C CXX)
if(NOT MSVC)
@@ -10,57 +27,76 @@ else() add_definitions(-D_WIN32_WINNT=0x600)
endif()
-# Protobuf
-# NOTE: we cannot use "CONFIG" mode here because protobuf-config.cmake
-# is broken when used with CMAKE_INSTALL_PREFIX
-find_package(Protobuf REQUIRED)
-message(STATUS "Using protobuf ${protobuf_VERSION}")
-
-# {Protobuf,PROTOBUF}_FOUND is defined based on find_package type ("MODULE" vs "CONFIG").
-# For "MODULE", the case has also changed between cmake 3.5 and 3.6.
-# We use the legacy uppercase version for *_LIBRARIES AND *_INCLUDE_DIRS variables
-# as newer cmake versions provide them too for backward compatibility.
-if(Protobuf_FOUND OR PROTOBUF_FOUND)
- if(TARGET protobuf::libprotobuf)
- set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
- else()
- set(_PROTOBUF_LIBPROTOBUF ${PROTOBUF_LIBRARIES})
- include_directories(${PROTOBUF_INCLUDE_DIRS})
- endif()
- if(TARGET protobuf::protoc)
- set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
- else()
- set(_PROTOBUF_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE})
- endif()
+if(GRPC_AS_SUBMODULE)
+ # One way to build a projects that uses gRPC is to just include the
+ # entire gRPC project tree via "add_subdirectory".
+ # This approach is very simple to use, but the are some potential
+ # disadvantages:
+ # * it includes gRPC's CMakeLists.txt directly into your build script
+ # without and that can make gRPC's internal setting interfere with your
+ # own build.
+ # * depending on what's installed on your system, the contents of submodules
+ # in gRPC's third_party/* might need to be available (and there might be
+ # additional prerequisites required to build them). Consider using
+ # the gRPC_*_PROVIDER options to fine-tune the expected behavior.
+ #
+ # A more robust approach to add dependency on gRPC is using
+ # cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
+
+ # Include the gRPC's cmake build (normally grpc source code would live
+ # in a git submodule called "third_party/grpc", but this example lives in
+ # the same repository as gRPC sources, so we just look a few directories up)
+ add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
+ message(STATUS "Using gRPC via add_subdirectory.")
+
+ # After using add_subdirectory, we can now use the grpc targets directly from
+ # this build.
+ set(_PROTOBUF_LIBPROTOBUF libprotobuf)
+ set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
+ set(_GRPC_GRPCPP_UNSECURE grpc++_unsecure)
+ set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
else()
- message(WARNING "Failed to locate libprotobuf and protoc!")
-endif()
+ # This branch assumes that gRPC and all its dependencies are already installed
+ # on this system, so they can be located by find_package().
-# gRPC
-find_package(gRPC CONFIG REQUIRED)
-message(STATUS "Using gRPC ${gRPC_VERSION}")
+ # Find Protobuf installation
+ # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
+ set(protobuf_MODULE_COMPATIBLE TRUE)
+ find_package(Protobuf CONFIG REQUIRED)
+ message(STATUS "Using protobuf ${protobuf_VERSION}")
-# gRPC C++ plugin
-get_target_property(gRPC_CPP_PLUGIN_EXECUTABLE gRPC::grpc_cpp_plugin
- IMPORTED_LOCATION_RELEASE)
+ set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
+ set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
+
+ # Find gRPC installation
+ # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
+ find_package(gRPC CONFIG REQUIRED)
+ message(STATUS "Using gRPC ${gRPC_VERSION}")
+
+ set(_GRPC_GRPCPP_UNSECURE gRPC::grpc++_unsecure)
+ set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
+endif()
# Proto file
get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)
# Generated sources
-protobuf_generate_cpp(hw_proto_srcs hw_proto_hdrs "${hw_proto}")
+set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
+set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
add_custom_command(
- OUTPUT "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
+ OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
COMMAND ${_PROTOBUF_PROTOC}
- ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" -I "${hw_proto_path}"
- --plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}"
+ ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
+ --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
+ -I "${hw_proto_path}"
+ --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
"${hw_proto}"
DEPENDS "${hw_proto}")
-# Generated include directory
+# Include generated *.pb.h files
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
# Targets greeter_[async_](client|server)
@@ -71,6 +107,6 @@ foreach(_target ${hw_proto_srcs}
${hw_grpc_srcs})
target_link_libraries(${_target}
- ${_PROTOBUF_LIBPROTOBUF}
- gRPC::grpc++_unsecure)
+ ${_GRPC_GRPCPP_UNSECURE}
+ ${_PROTOBUF_LIBPROTOBUF})
endforeach()
diff --git a/examples/cpp/helloworld/cmake_externalproject/CMakeLists.txt b/examples/cpp/helloworld/cmake_externalproject/CMakeLists.txt new file mode 100644 index 0000000000..9fbdf071a8 --- /dev/null +++ b/examples/cpp/helloworld/cmake_externalproject/CMakeLists.txt @@ -0,0 +1,116 @@ +# Copyright 2018 gRPC authors.
+#
+# 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.
+#
+# cmake "superbuild" file for C++ helloworld example.
+# This build file demonstrates how to build the helloworld project
+# and all its dependencies in a single cmake build (hence "superbuild")
+# that is easy to build and maintain.
+# cmake's ExternalProject_Add() is used to import all the sub-projects,
+# including the "helloworld" project itself.
+# See https://blog.kitware.com/cmake-superbuilds-git-submodules/
+
+cmake_minimum_required(VERSION 2.8)
+
+# Project
+project(HelloWorld-SuperBuild C CXX)
+
+include(ExternalProject)
+
+# Builds c-ares project from the git submodule.
+# Note: For all external projects, instead of using checked-out code, one could
+# specify GIT_REPOSITORY and GIT_TAG to have cmake download the dependency directly,
+# without needing to add a submodule to your project.
+ExternalProject_Add(c-ares
+ PREFIX c-ares
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/cares/cares"
+ CMAKE_CACHE_ARGS
+ -DCARES_SHARED:BOOL=OFF
+ -DCARES_STATIC:BOOL=ON
+ -DCARES_STATIC_PIC:BOOL=ON
+ -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares
+)
+
+# Builds protobuf project from the git submodule.
+ExternalProject_Add(protobuf
+ PREFIX protobuf
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/protobuf/cmake"
+ CMAKE_CACHE_ARGS
+ -Dprotobuf_BUILD_TESTS:BOOL=OFF
+ -Dprotobuf_WITH_ZLIB:BOOL=OFF
+ -Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
+ -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/protobuf
+)
+
+# Builds zlib project from the git submodule.
+ExternalProject_Add(zlib
+ PREFIX zlib
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party/zlib"
+ CMAKE_CACHE_ARGS
+ -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/zlib
+)
+
+# the location where protobuf-config.cmake will be installed varies by platform
+if (WIN32)
+ set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
+else()
+ set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/lib/cmake/protobuf")
+endif()
+
+# if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
+set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
+if (OPENSSL_ROOT_DIR)
+ set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
+endif()
+
+# Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
+# are correctly located.
+ExternalProject_Add(grpc
+ PREFIX grpc
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../.."
+ CMAKE_CACHE_ARGS
+ -DgRPC_INSTALL:BOOL=ON
+ -DgRPC_BUILD_TESTS:BOOL=OFF
+ -DgRPC_PROTOBUF_PROVIDER:STRING=package
+ -DgRPC_PROTOBUF_PACKAGE_TYPE:STRING=CONFIG
+ -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
+ -DgRPC_ZLIB_PROVIDER:STRING=package
+ -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
+ -DgRPC_CARES_PROVIDER:STRING=package
+ -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
+ -DgRPC_SSL_PROVIDER:STRING=package
+ ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
+ -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
+ DEPENDS c-ares protobuf zlib
+)
+
+# Build the helloworld projects itself using a CMakeLists.txt that assumes all the dependencies
+# have already been installed.
+# Even though helloworld is not really an "external project" from perspective of this build,
+# we are still importing it using ExternalProject_Add because that allows us to use find_package()
+# to locate all the dependencies (if we were building helloworld directly in this build we,
+# we would have needed to manually import the libraries as opposed to reusing targets exported by
+# gRPC and protobuf).
+ExternalProject_Add(helloworld
+ PREFIX helloworld
+ SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.."
+ BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/helloworld"
+ INSTALL_COMMAND ""
+ CMAKE_CACHE_ARGS
+ -DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
+ -Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
+ -DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
+ ${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
+ -DgRPC_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc/lib/cmake/grpc
+ DEPENDS protobuf grpc
+)
diff --git a/examples/cpp/helloworld/greeter_async_client.cc b/examples/cpp/helloworld/greeter_async_client.cc index ddf6c1aaf3..d7a9d52836 100644 --- a/examples/cpp/helloworld/greeter_async_client.cc +++ b/examples/cpp/helloworld/greeter_async_client.cc @@ -20,7 +20,7 @@ #include <memory> #include <string> -#include <grpc++/grpc++.h> +#include <grpcpp/grpcpp.h> #include <grpc/support/log.h> #include "helloworld.grpc.pb.h" diff --git a/examples/cpp/helloworld/greeter_async_client2.cc b/examples/cpp/helloworld/greeter_async_client2.cc index 3154e84d85..d5098b9fc3 100644 --- a/examples/cpp/helloworld/greeter_async_client2.cc +++ b/examples/cpp/helloworld/greeter_async_client2.cc @@ -20,7 +20,7 @@ #include <memory> #include <string> -#include <grpc++/grpc++.h> +#include <grpcpp/grpcpp.h> #include <grpc/support/log.h> #include <thread> diff --git a/examples/cpp/helloworld/greeter_async_server.cc b/examples/cpp/helloworld/greeter_async_server.cc index e40889a84c..a74673d803 100644 --- a/examples/cpp/helloworld/greeter_async_server.cc +++ b/examples/cpp/helloworld/greeter_async_server.cc @@ -21,7 +21,7 @@ #include <string> #include <thread> -#include <grpc++/grpc++.h> +#include <grpcpp/grpcpp.h> #include <grpc/support/log.h> #include "helloworld.grpc.pb.h" diff --git a/examples/cpp/helloworld/greeter_client.cc b/examples/cpp/helloworld/greeter_client.cc index 555fd8d2cd..932583c84a 100644 --- a/examples/cpp/helloworld/greeter_client.cc +++ b/examples/cpp/helloworld/greeter_client.cc @@ -20,7 +20,7 @@ #include <memory> #include <string> -#include <grpc++/grpc++.h> +#include <grpcpp/grpcpp.h> #ifdef BAZEL_BUILD #include "examples/protos/helloworld.grpc.pb.h" diff --git a/examples/cpp/helloworld/greeter_server.cc b/examples/cpp/helloworld/greeter_server.cc index 832f4400d2..f36ad906a2 100644 --- a/examples/cpp/helloworld/greeter_server.cc +++ b/examples/cpp/helloworld/greeter_server.cc @@ -20,7 +20,7 @@ #include <memory> #include <string> -#include <grpc++/grpc++.h> +#include <grpcpp/grpcpp.h> #ifdef BAZEL_BUILD #include "examples/protos/helloworld.grpc.pb.h" diff --git a/examples/cpp/route_guide/route_guide_client.cc b/examples/cpp/route_guide/route_guide_client.cc index b8298661e5..a89ec164c6 100644 --- a/examples/cpp/route_guide/route_guide_client.cc +++ b/examples/cpp/route_guide/route_guide_client.cc @@ -24,10 +24,10 @@ #include <thread> #include <grpc/grpc.h> -#include <grpc++/channel.h> -#include <grpc++/client_context.h> -#include <grpc++/create_channel.h> -#include <grpc++/security/credentials.h> +#include <grpcpp/channel.h> +#include <grpcpp/client_context.h> +#include <grpcpp/create_channel.h> +#include <grpcpp/security/credentials.h> #include "helper.h" #include "route_guide.grpc.pb.h" diff --git a/examples/cpp/route_guide/route_guide_server.cc b/examples/cpp/route_guide/route_guide_server.cc index b071e355bf..5867c16712 100644 --- a/examples/cpp/route_guide/route_guide_server.cc +++ b/examples/cpp/route_guide/route_guide_server.cc @@ -24,10 +24,10 @@ #include <string> #include <grpc/grpc.h> -#include <grpc++/server.h> -#include <grpc++/server_builder.h> -#include <grpc++/server_context.h> -#include <grpc++/security/server_credentials.h> +#include <grpcpp/server.h> +#include <grpcpp/server_builder.h> +#include <grpcpp/server_context.h> +#include <grpcpp/security/server_credentials.h> #include "helper.h" #include "route_guide.grpc.pb.h" @@ -51,6 +51,7 @@ float ConvertToRadians(float num) { return num * 3.1415926 /180; } +// The formula is based on http://mathforum.org/library/drmath/view/51879.html float GetDistance(const Point& start, const Point& end) { const float kCoordFactor = 10000000.0; float lat_1 = start.latitude() / kCoordFactor; diff --git a/examples/csharp/helloworld-from-cli/global.json b/examples/csharp/helloworld-from-cli/global.json deleted file mode 100644 index e4b797ee8e..0000000000 --- a/examples/csharp/helloworld-from-cli/global.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "sdk": { - "version": "1.0.0" - } -} diff --git a/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs b/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs index 66c4a94573..f9af190888 100644 --- a/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs +++ b/examples/csharp/route_guide/RouteGuide/RouteGuideUtil.cs @@ -52,26 +52,23 @@ namespace Routeguide /// <summary> /// Calculate the distance between two points using the "haversine" formula. - /// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html. + /// The formula is based on http://mathforum.org/library/drmath/view/51879.html /// </summary> /// <param name="start">the starting point</param> /// <param name="end">the end point</param> /// <returns>the distance between the points in meters</returns> public static double GetDistance(this Point start, Point end) { - double lat1 = start.GetLatitude(); - double lat2 = end.GetLatitude(); - double lon1 = start.GetLongitude(); - double lon2 = end.GetLongitude(); - int r = 6371000; // metres - double phi1 = ToRadians(lat1); - double phi2 = ToRadians(lat2); - double deltaPhi = ToRadians(lat2 - lat1); - double deltaLambda = ToRadians(lon2 - lon1); + int r = 6371000; // earth radius in metres + double lat1 = ToRadians(start.GetLatitude()); + double lat2 = ToRadians(end.GetLatitude()); + double lon1 = ToRadians(start.GetLongitude()); + double lon2 = ToRadians(end.GetLongitude()); + double deltalat = lat2 - lat1; + double deltalon = lon2 - lon1; - double a = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) + Math.Cos(phi1) * Math.Cos(phi2) * Math.Sin(deltaLambda / 2) * Math.Sin(deltaLambda / 2); + double a = Math.Sin(deltalat / 2) * Math.Sin(deltalat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(deltalon / 2) * Math.Sin(deltalon / 2); double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); - return r * c; } diff --git a/examples/node/dynamic_codegen/route_guide/route_guide_server.js b/examples/node/dynamic_codegen/route_guide/route_guide_server.js index ab537ff401..3819c092eb 100644 --- a/examples/node/dynamic_codegen/route_guide/route_guide_server.js +++ b/examples/node/dynamic_codegen/route_guide/route_guide_server.js @@ -103,7 +103,7 @@ function listFeatures(call) { /** * Calculate the distance between two points using the "haversine" formula. - * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html. + * The formula is based on http://mathforum.org/library/drmath/view/51879.html. * @param start The starting point * @param end The end point * @return The distance between the points in meters @@ -112,21 +112,18 @@ function getDistance(start, end) { function toRadians(num) { return num * Math.PI / 180; } - var lat1 = start.latitude / COORD_FACTOR; - var lat2 = end.latitude / COORD_FACTOR; - var lon1 = start.longitude / COORD_FACTOR; - var lon2 = end.longitude / COORD_FACTOR; - var R = 6371000; // metres - var φ1 = toRadians(lat1); - var φ2 = toRadians(lat2); - var Δφ = toRadians(lat2-lat1); - var Δλ = toRadians(lon2-lon1); - - var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + - Math.cos(φ1) * Math.cos(φ2) * - Math.sin(Δλ/2) * Math.sin(Δλ/2); + var R = 6371000; // earth radius in metres + var lat1 = toRadians(start.latitude / COORD_FACTOR); + var lat2 = toRadians(end.latitude / COORD_FACTOR); + var lon1 = toRadians(start.longitude / COORD_FACTOR); + var lon2 = toRadians(end.longitude / COORD_FACTOR); + + var deltalat = lat2-lat1; + var deltalon = lon2-lon1; + var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) + + Math.cos(lat1) * Math.cos(lat2) * + Math.sin(deltalon/2) * Math.sin(deltalon/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); - return R * c; } diff --git a/examples/node/static_codegen/route_guide/route_guide_server.js b/examples/node/static_codegen/route_guide/route_guide_server.js index ef00bbbdfb..eecac62354 100644 --- a/examples/node/static_codegen/route_guide/route_guide_server.js +++ b/examples/node/static_codegen/route_guide/route_guide_server.js @@ -102,7 +102,7 @@ function listFeatures(call) { /** * Calculate the distance between two points using the "haversine" formula. - * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html. + * The formula is based on http://mathforum.org/library/drmath/view/51879.html. * @param start The starting point * @param end The end point * @return The distance between the points in meters @@ -111,21 +111,18 @@ function getDistance(start, end) { function toRadians(num) { return num * Math.PI / 180; } - var lat1 = start.getLatitude() / COORD_FACTOR; - var lat2 = end.getLatitude() / COORD_FACTOR; - var lon1 = start.getLongitude() / COORD_FACTOR; - var lon2 = end.getLongitude() / COORD_FACTOR; - var R = 6371000; // metres - var φ1 = toRadians(lat1); - var φ2 = toRadians(lat2); - var Δφ = toRadians(lat2-lat1); - var Δλ = toRadians(lon2-lon1); - - var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + - Math.cos(φ1) * Math.cos(φ2) * - Math.sin(Δλ/2) * Math.sin(Δλ/2); + var R = 6371000; // earth radius in metres + var lat1 = toRadians(start.getLatitude() / COORD_FACTOR); + var lat2 = toRadians(end.getLatitude() / COORD_FACTOR); + var lon1 = toRadians(start.getLongitude() / COORD_FACTOR); + var lon2 = toRadians(end.getLongitude() / COORD_FACTOR); + + var deltalat = lat2-lat1; + var deltalon = lon2-lon1; + var a = Math.sin(deltalat/2) * Math.sin(deltalat/2) + + Math.cos(lat1) * Math.cos(lat2) * + Math.sin(deltalon/2) * Math.sin(deltalon/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); - return R * c; } diff --git a/examples/python/route_guide/route_guide_server.py b/examples/python/route_guide/route_guide_server.py index f10008fdec..1969fdd378 100644 --- a/examples/python/route_guide/route_guide_server.py +++ b/examples/python/route_guide/route_guide_server.py @@ -46,6 +46,7 @@ def get_distance(start, end): delta_lat_rad = math.radians(lat_2 - lat_1) delta_lon_rad = math.radians(lon_2 - lon_1) + # Formula is based on http://mathforum.org/library/drmath/view/51879.html a = (pow(math.sin(delta_lat_rad / 2), 2) + (math.cos(lat_rad_1) * math.cos(lat_rad_2) * pow( math.sin(delta_lon_rad / 2), 2))) diff --git a/examples/ruby/route_guide/route_guide_server.rb b/examples/ruby/route_guide/route_guide_server.rb index 8ea07a21c5..5eb268b533 100755 --- a/examples/ruby/route_guide/route_guide_server.rb +++ b/examples/ruby/route_guide/route_guide_server.rb @@ -32,19 +32,18 @@ COORD_FACTOR = 1e7 RADIUS = 637_100 # Determines the distance between two points. +# The formula is based on http://mathforum.org/library/drmath/view/51879.html. def calculate_distance(point_a, point_b) to_radians = proc { |x| x * Math::PI / 180 } - lat_a = point_a.latitude / COORD_FACTOR - lat_b = point_b.latitude / COORD_FACTOR - long_a = point_a.longitude / COORD_FACTOR - long_b = point_b.longitude / COORD_FACTOR - φ1 = to_radians.call(lat_a) - φ2 = to_radians.call(lat_b) - Δφ = to_radians.call(lat_a - lat_b) - Δλ = to_radians.call(long_a - long_b) - a = Math.sin(Δφ / 2)**2 + - Math.cos(φ1) * Math.cos(φ2) + - Math.sin(Δλ / 2)**2 + lat_a = to_radians.call(point_a.latitude / COORD_FACTOR) + lat_b = to_radians.call(point_b.latitude / COORD_FACTOR) + lon_a = to_radians.call(point_a.longitude / COORD_FACTOR) + lon_b = to_radians.call(point_b.longitude / COORD_FACTOR) + delta_lat = lat_a - lat_b + delta_lon = lon_a - lon_b + a = Math.sin(delta_lat / 2)**2 + + Math.cos(lat_a) * Math.cos(lat_b) + + Math.sin(delta_lon / 2)**2 (2 * RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))).to_i end |