Skip to content

Commit

Permalink
Using protobuf in peer library (#27)
Browse files Browse the repository at this point in the history
Also:
* Changed instanceId to be a number
* Optimistic message support
  • Loading branch information
pablitar authored Jan 23, 2020
1 parent bea64b6 commit f9406c9
Show file tree
Hide file tree
Showing 23 changed files with 971 additions and 154 deletions.
35 changes: 33 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "9901bc17138a79135048fb0c107ee7a56e91815ec6594c08cb9a17b80276d62b",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.40.0/rules_nodejs-0.40.0.tar.gz"],
sha256 = "a54b2511d6dae42c1f7cdaeb08144ee2808193a088004fc3b464a04583d5aa2e",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.3/rules_nodejs-0.42.3.tar.gz"],
)

# The yarn_install rule runs yarn anytime the package.json or yarn.lock file changes.
Expand Down Expand Up @@ -83,3 +83,34 @@ load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_depe
go_rules_dependencies()

go_register_toolchains()


#Protobuf

http_archive(
name = "rules_proto",
sha256 = "602e7161d9195e50246177e7c55b2f39950a9cf7366f74ed5f22fd45750cd208",
strip_prefix = "rules_proto-97d8af4dc474595af3900dd85cb3a29ad28cc313",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz",
],
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()
rules_proto_toolchains()

http_archive(
name = "rules_typescript_proto",
sha256 = "0c76ae0d04eaa4d4c5f12556615cb70d294082ee672aee6dd849fea4ec2075ee",
strip_prefix = "rules_typescript_proto-0.0.3",
urls = [
"https://github.com/Dig-Doug/rules_typescript_proto/archive/0.0.3.tar.gz",
],
)

load("@rules_typescript_proto//:index.bzl", "rules_typescript_proto_dependencies")

rules_typescript_proto_dependencies()
5 changes: 3 additions & 2 deletions comms/lighthouse/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ export function configureRoutes(app: express.Express, services: Services, option
const topologyInfo = layersService.getLayerTopology(layerId);
if (req.query.format === "graphviz") {
res.send(`
strict graph graphName {
strict digraph graphName {
concentrate=true
${topologyInfo.map(it => (it.connectedPeerIds?.length ? it.connectedPeerIds.map(connected => `"${it.peerId}"--"${connected}"`).join("\n") : `"${it.peerId}"`)).join("\n")}
${topologyInfo.map(it => `"${it.peerId}"[label="${it.peerId}\\nconns:${it.connectedPeerIds?.length ?? 0}"];`).join("\n")}
${topologyInfo.map(it => (it.connectedPeerIds?.length ? it.connectedPeerIds.map(connected => `"${it.peerId}"->"${connected}";`).join("\n") : `"${it.peerId}";`)).join("\n")}
}`);
} else {
res.send(topologyInfo);
Expand Down
2 changes: 1 addition & 1 deletion comms/lighthouse/test/roomsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Rooms service", () => {

beforeEach(() => {
peerLibrary = {
nickname: lighthouseId,
peerId: lighthouseId,
joinRoom(roomId: string) {
roomsService.addUserToRoom(roomId, lighthousePeerData);
},
Expand Down
2 changes: 1 addition & 1 deletion comms/peer-react-app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ ts_devserver(
"npm/node_modules/react-dom/umd",
"react_samples/src/styles",
],
data = ["//:node_modules"],
entry_module = "katalyst/comms/peer-react-app/index",
port = 3001,
# This is the path we'll request from the browser, see index.html
Expand All @@ -51,6 +50,7 @@ ts_devserver(
":peer-react-app",
"@npm//decentraland-ui",
"@npm//fp-future:fp-future__umd",
"@npm//protobufjs:protobufjs__umd",
"@npm//react",
"@npm//react-dom",
],
Expand Down
6 changes: 3 additions & 3 deletions comms/peer-react-app/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function Chat(props: { peer: IPeer; layer: string; room: string; url: str
}

function sendMessage() {
appendMessage(currentRoom, props.peer.nickname, message);
appendMessage(currentRoom, props.peer.peerId, message);
props.peer.sendMessage(currentRoom, { type: "chat", message }, PeerMessageTypes.reliable);
setMessage("");
}
Expand Down Expand Up @@ -157,7 +157,7 @@ export function Chat(props: { peer: IPeer; layer: string; room: string; url: str

return (
<div className="chat">
<h2 className="welcome-message">Welcome to the Chat {props.peer.nickname}</h2>
<h2 className="welcome-message">Welcome to the Chat {props.peer.peerId}</h2>
<div className="side">
<h3>Available rooms</h3>
<ul className="available-rooms">
Expand Down Expand Up @@ -237,7 +237,7 @@ export function Chat(props: { peer: IPeer; layer: string; room: string; url: str
</div>
<div className="messages-container">
{messages[currentRoom]?.map((it, i) => (
<MessageBubble message={it} key={i} own={it.sender === props.peer.nickname} />
<MessageBubble message={it} key={i} own={it.sender === props.peer.peerId} />
))}
<div style={{ float: "left", clear: "both" }} ref={messagesEndRef}></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion comms/peer-react-app/components/ConnectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare const window: Window & { peer: Peer };
export function ConnectForm(props: {
onConnected: (peer: IPeer, layer: string, room: string, url: string) => any;
peerClass: {
new (url: string, nickname: string, callback: any, config: any): IPeer;
new (url: string, peerId: string, callback: any, config: any): IPeer;
};
}) {
const [url, setUrl] = useState("http://localhost:9000");
Expand Down
4 changes: 2 additions & 2 deletions comms/peer-react-app/components/PeerToken.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { util } from "../../peer/src/peerjs-server-connector/util";

export const PeerToken = {
getToken(nickname: string): string {
const key = `${nickname}_token`;
getToken(peerId: string): string {
const key = `${peerId}_token`;
let token = localStorage.getItem(key);
if (!token) {
token = util.generateToken(64);
Expand Down
2 changes: 2 additions & 0 deletions comms/peer-react-app/static/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require.config({
'fp-future': 'npm/node_modules/fp-future/index',
'simple-peer': 'npm/node_modules/simple-peer/simplepeer.min',
'eventemitter3': 'npm/node_modules/eventemitter3/umd/eventemitter3',
'protobufjs/minimal': 'npm/node_modules/protobufjs/dist/minimal/protobuf',
'long': 'npm/node_modules/long/dist/long'
//redux: '/npm/node_modules/redux/dist/redux',
//d3: '/npm/node_modules/d3/dist/d3',
//jsonwebtoken: 'static/jsonwebtoken.umd',
Expand Down
12 changes: 8 additions & 4 deletions comms/peer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("//tools/npm:package.bzl", "dataform_npm_package")
load("@npm_bazel_karma//:index.bzl", "karma_web_test_suite")
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")

package(default_visibility = ["//visibility:public"])

Expand All @@ -12,6 +14,9 @@ ts_library(
deps = [
"@npm//eventemitter3",
"@npm//fp-future",
"@npm//long",
"@npm//@types/long",
"@npm//protobufjs",
"@npm//simple-peer",
],
)
Expand All @@ -25,8 +30,6 @@ dataform_npm_package(
deps = [":peer"],
)

load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")

rollup_bundle(
name = "bundle",
config_file = ":rollup.config.js",
Expand All @@ -40,8 +43,6 @@ rollup_bundle(
],
)

load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")

npm_package(
name = "package",
deps = [
Expand Down Expand Up @@ -74,6 +75,8 @@ karma_web_test_suite(
static_files = [
"test/config.js",
"@npm//simple-peer:simple-peer__files",
"@npm//protobufjs:protobufjs__files",
"@npm//long:long__files",
],
tags = [],
runtime_deps = [
Expand All @@ -83,6 +86,7 @@ karma_web_test_suite(
":tests",
"@npm//eventemitter3:eventemitter3__umd",
"@npm//fp-future:fp-future__umd",
"@npm//protobufjs:protobufjs__umd",
"@npm//karma-mocha-reporter",
"@npm//karma-spec-reporter",
],
Expand Down
4 changes: 3 additions & 1 deletion comms/peer/peer.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"dependencies": {
"eventemitter3": "^4.0.0",
"fp-future": "^1.0.1",
"simple-peer": "^9.6.2"
"simple-peer": "^9.6.2",
"protobufjs": "^6.8.8",
"long"
}
}
2 changes: 1 addition & 1 deletion comms/peer/scripts/stress-test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const sessionId = urlParams.get("sessionId") ?? util.randomToken();
if (peerContainer.messagesSent < messageCount) {
setTimeout(send, timeBetweenMessages);
} else {
console.log("Peer finished: " + peerContainer.peer!.nickname);
console.log("Peer finished: " + peerContainer.peer!.peerId);
finishedPeers.push(peerContainer);

if (finishedPeers.length === peers.length) {
Expand Down
Loading

0 comments on commit f9406c9

Please sign in to comment.