2
0
Эх сурвалжийг харах

Merge pull request #23 from murat-dogan/p2p-tests

P2P tests
Paul-Louis Ageneau 5 жил өмнө
parent
commit
a6992c765d

+ 24 - 0
CMakeLists.txt

@@ -25,6 +25,14 @@ set(TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp
 )
 
+set(TESTS_OFFERER_SOURCES
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/p2p/offerer.cpp
+)
+
+set(TESTS_ANSWERER_SOURCES
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/p2p/answerer.cpp
+)
+
 # Hack because usrsctp uses CMAKE_SOURCE_DIR instead of CMAKE_CURRENT_SOURCE_DIR
 set(CMAKE_REQUIRED_FLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/deps/usrsctp/usrsctplib")
 
@@ -112,6 +120,7 @@ endif()
 add_library(LibDataChannel::LibDataChannel ALIAS datachannel)
 add_library(LibDataChannel::LibDataChannelStatic ALIAS datachannel-static)
 
+# Main Test
 add_executable(tests ${TESTS_SOURCES})
 set_target_properties(tests PROPERTIES
 	VERSION ${PROJECT_VERSION}
@@ -119,3 +128,18 @@ set_target_properties(tests PROPERTIES
 
 target_link_libraries(tests datachannel)
 
+# P2P Test: offerer
+add_executable(offerer ${TESTS_OFFERER_SOURCES})
+set_target_properties(offerer PROPERTIES
+	VERSION ${PROJECT_VERSION}
+	CXX_STANDARD 17)
+
+target_link_libraries(offerer datachannel)
+
+# P2P Test: answerer
+add_executable(answerer ${TESTS_ANSWERER_SOURCES})
+set_target_properties(answerer PROPERTIES
+	VERSION ${PROJECT_VERSION}
+	CXX_STANDARD 17)
+
+target_link_libraries(answerer datachannel)

+ 9 - 0
test/p2p/README.md

@@ -0,0 +1,9 @@
+* Execute ```offerer``` app in console
+* Execute ```answerer``` app in another console
+* Copy "Local Description" from ```offerer```
+* Enter 1 to ```answerer```
+* Paste copied description, press enter
+* Redo same procedure for ```answerer```
+* Redo same procedure for candidates
+* Wait for "DataChannel open" message
+* Send message from one peer to another

+ 139 - 0
test/p2p/answerer.cpp

@@ -0,0 +1,139 @@
+/**
+ * Copyright (c) 2019 Paul-Louis Ageneau, Murat Dogan
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "rtc/rtc.hpp"
+
+#include <chrono>
+#include <iostream>
+#include <memory>
+
+using namespace rtc;
+using namespace std;
+
+template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
+
+int main(int argc, char **argv) {
+	rtc::Configuration config;
+	// config.iceServers.emplace_back("stun.l.google.com:19302");
+	// config.enableIceTcp = true;
+
+	// Add TURN Server Example
+	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
+	//							IceServer::RelayType::TurnTls);
+	// config.iceServers.push_back(turnServer);
+
+	auto pc = std::make_shared<PeerConnection>(config);
+
+	pc->onLocalDescription([](const Description &sdp) {
+		std::string s(sdp);
+		std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
+		cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
+	});
+
+	pc->onLocalCandidate([](const Candidate &candidate) {
+		cout << "Local Candidate (Paste this to other peer):" << endl << candidate << endl << endl;
+	});
+
+	pc->onStateChange(
+	    [](PeerConnection::State state) { cout << "[ State: " << state << " ]" << endl; });
+	pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
+		cout << "[ Gathering State: " << state << " ]" << endl;
+	});
+
+	shared_ptr<DataChannel> dc = nullptr;
+	pc->onDataChannel([&dc](shared_ptr<DataChannel> _dc) {
+		cout << "[ Got a DataChannel with label: " << _dc->label() << " ]" << endl;
+		dc = _dc;
+		dc->onMessage([](const variant<binary, string> &message) {
+			if (holds_alternative<string>(message)) {
+				cout << "[ Received: " << get<string>(message) << " ]" << endl;
+			}
+		});
+	});
+
+	bool exit = false;
+	while (!exit) {
+		cout << endl
+		     << endl
+		     << "*************************************************************************" << endl
+		     << "* 0: Exit /"
+		     << " 1: Enter Description /"
+		     << " 2: Enter Candidate /"
+		     << " 3: Send Message *" << endl
+		     << " [Command]: ";
+
+		int command;
+		std::string sdp, candidate, message;
+		const char *a;
+		std::unique_ptr<Candidate> candidatePtr;
+		std::unique_ptr<Description> descPtr;
+		cin >> command;
+
+		switch (command) {
+		case 0:
+			exit = true;
+			break;
+
+		case 1:
+			// Parse Description
+			cout << "[SDP]: ";
+			sdp = "";
+			while (sdp.length() == 0)
+				getline(cin, sdp);
+
+			std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
+			descPtr = std::make_unique<Description>(sdp, Description::Type::Offer,
+			                                        Description::Role::Passive);
+			pc->setRemoteDescription(*descPtr);
+			break;
+
+		case 2:
+			// Parse Candidate
+			cout << "[Candidate]: ";
+			candidate = "";
+			while (candidate.length() == 0)
+				getline(cin, candidate);
+
+			candidatePtr = std::make_unique<Candidate>(candidate);
+			pc->addRemoteCandidate(*candidatePtr);
+			break;
+
+		case 3:
+			// Send Message
+			if (!dc || !dc->isOpen()) {
+				cout << "** Channel is not Open ** ";
+				break;
+			}
+			cout << "[Message]: ";
+			message = "";
+			while (message.length() == 0)
+				getline(cin, message);
+			dc->send(message);
+			break;
+
+		default:
+			cout << "** Invalid Command ** ";
+			break;
+		}
+	}
+
+	if (dc)
+		dc->close();
+	if (pc)
+		pc->close();
+}

+ 141 - 0
test/p2p/offerer.cpp

@@ -0,0 +1,141 @@
+/**
+ * Copyright (c) 2019 Paul-Louis Ageneau, Murat Dogan
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "rtc/rtc.hpp"
+
+#include <chrono>
+#include <iostream>
+#include <memory>
+
+using namespace rtc;
+using namespace std;
+
+template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
+
+int main(int argc, char **argv) {
+	rtc::Configuration config;
+	// config.iceServers.emplace_back("stun.l.google.com:19302");
+	// config.enableIceTcp = true;
+
+	// Add TURN Server Example
+	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
+	//							IceServer::RelayType::TurnTls);
+	// config.iceServers.push_back(turnServer);
+
+	auto pc = std::make_shared<PeerConnection>(config);
+
+	pc->onLocalDescription([](const Description &sdp) {
+		std::string s(sdp);
+		std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
+		cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
+	});
+
+	pc->onLocalCandidate([](const Candidate &candidate) {
+		cout << "Local Candidate (Paste this to other peer):" << endl << candidate << endl << endl;
+	});
+
+	pc->onStateChange(
+	    [](PeerConnection::State state) { cout << "[ State: " << state << " ]" << endl; });
+
+	pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
+		cout << "[ Gathering State: " << state << " ]" << endl;
+	});
+
+	auto dc = pc->createDataChannel("test");
+	dc->onOpen([&]() {
+		if (!dc)
+			return;
+		cout << "[ DataChannel open: " << dc->label() << " ]" << endl;
+	});
+
+	dc->onMessage([](const variant<binary, string> &message) {
+		if (holds_alternative<string>(message)) {
+			cout << "[ Received: " << get<string>(message) << " ]" << endl;
+		}
+	});
+
+	bool exit = false;
+	while (!exit) {
+		cout << endl
+		     << endl
+		     << "*************************************************************************" << endl
+		     << "* 0: Exit /"
+		     << " 1: Enter Description /"
+		     << " 2: Enter Candidate /"
+		     << " 3: Send Message *" << endl
+		     << " [Command]: ";
+
+		int command;
+		std::string sdp, candidate, message;
+		const char *a;
+		std::unique_ptr<Candidate> candidatePtr;
+		std::unique_ptr<Description> descPtr;
+		cin >> command;
+
+		switch (command) {
+		case 0:
+			exit = true;
+			break;
+
+		case 1:
+			// Parse Description
+			cout << "[SDP]: ";
+			sdp = "";
+			while (sdp.length() == 0)
+				getline(cin, sdp);
+
+			std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
+			descPtr = std::make_unique<Description>(sdp);
+			pc->setRemoteDescription(*descPtr);
+			break;
+
+		case 2:
+			// Parse Candidate
+			cout << "[Candidate]: ";
+			candidate = "";
+			while (candidate.length() == 0)
+				getline(cin, candidate);
+
+			candidatePtr = std::make_unique<Candidate>(candidate);
+			pc->addRemoteCandidate(*candidatePtr);
+			break;
+
+		case 3:
+			// Send Message
+			if (!dc->isOpen()) {
+				cout << "** Channel is not Open ** ";
+				break;
+			}
+			cout << "[Message]: ";
+			message = "";
+			while (message.length() == 0)
+				getline(cin, message);
+			dc->send(message);
+			break;
+
+		default:
+			cout << "** Invalid Command ** ";
+			break;
+		}
+	}
+
+	if (dc)
+		dc->close();
+	if (pc)
+		pc->close();
+}