Georouting Webrtc
Georouting Webrtc
Georouting Webrtc
WebRTC
Using WebRTC to build geographically aware
Distributed Hash Table between browsers.
Author: Supervisor:
Rolf Erik H. Lekang Svein Erik Bratsberg
Peer-to-peer systems have earlier made it possible to do more with the net-
works available than the typical server-client architecture allows. In the last
few years, websites have developed from very static pages to complex dynamic
applications. The web continues to evolve and WebRTC introduces peer-to-
peer communication to the browser and the web applications we run in these
browsers. The web applications can utilize this technology in different ways.
This project moves a known peer-to-peer algorithm, Chord, to the browser as a
proof-of-concept. Moreover, it looks into using geographical location data to en-
hance the routing in a Distributed Hash Table (DHT). This project investigates
the how to build a peer-to-peer system between browsers and how to test the
system during development and after. The project proposes to use simulation
and a scaled test with real browsers to test WebRTC based peer-to-peer applica-
tions. In this project, the browsers ran within Docker containers in data centers
around the world. Both simulations and tests with real browsers can be used in
development as well as evaluation testing. The Docker based real browser tests
were helpful in indicating bugs, however, the environment were hard to debug.
The results from the evaluation tests indicate that the routing enhanced with
knowledge of geographical locations gives a boost in routing performance.
iii
iv
Sammendrag
Peer-to-peer systemer har tidligere gjort det mulig å oppnå mer med nettverkene
som er tilgjengelig enn det man får til med en klient-tjener arkitektur. De siste
årene har man sett nettsider utvikle seg fra statisk innhold til avanserte dy-
namiske web-applikasjoner. Utviklingen fortsetter, og WebRTC introduserer
peer-to-peer kommunikasjon mellom nettlesere og web applikasjonen vi kjører i
disse nettleserne. Web applikasjonene kan utnytte denne teknologien på forskjel-
lige måter. Dette flytter en kjent peer-to-peer algoritme, Chord, til nettleseren
som et proof-of-concept. I tillegg ser prosjektet på bruk av lokasjonsdata til
forbedring av rutevalg i en distribuert hash-tabell. Videre, undersøker pros-
jektet hvordan man kan utvikle et peer-to-peer system mellom nettlesere og
hvordan man kan teste et slikt system. Prosjektet foreslår å bruke simulasjoner
og skalerte tester med ekte nettlesere til testing av peer-to-peer systemer. I dette
prosjektet kjørte de ekte nettleserne i Docker kontainere i datasentre omkring
i verden. Både simulasjonene og testene mellom flere nettlesere kan bli brukt
under utvikling og til evalueringstesting. Testene med ekte nettlesere i Docker
var nyttig til å indikere feil eller dårlig feilhåndtering, men det var utfordrende
å feilsøke i det miljøet. Resultatene fra evalueringstestene indikerte at ytelsen
av systemet øker ved bruk av geografiske lokasjonsdata til å forbedre rutevalg.
v
vi
Acknowledgement
I would like to thank Professor Svein Erik Bratsberg at the Department of Com-
puter and Information Science at Norwegian University of Science and Technol-
ogy. He has guided the project and given valuable feedback along the way.
vii
viii
Contents
1 Introduction 1
1.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Problem definition . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Readers manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Background 3
2.1 Peer-to-peer systems . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1.1 Structured vs Unstructured peer-to-peer systems . . . . . 4
2.2 Distributed Hash Tables . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.1 Consistent Hashing . . . . . . . . . . . . . . . . . . . . . . 5
2.2.2 Churn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.3 Chord . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Web technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.3.1 Geographical location . . . . . . . . . . . . . . . . . . . . 10
2.3.2 WebSocket: real-time communication between client and
server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.3 Web Storage . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.4 Offline mode . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.4 JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.1 Asynchronous code in JavaScript . . . . . . . . . . . . . . 13
2.4.2 Promise pattern . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.3 Prototype object . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.4 Node.js and io.js . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.5 Support in browsers . . . . . . . . . . . . . . . . . . . . . 16
2.4.6 Browserify and CommonJS . . . . . . . . . . . . . . . . . 17
2.5 Docker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
ix
x CONTENTS
3 Design 27
3.1 Levels of abstractions . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2 Chord on WebRTC . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.2.1 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.2 Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3 Changes in Chord . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4 Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.5 Testing and simulation . . . . . . . . . . . . . . . . . . . . . . . . 33
3.5.1 Logging and testing utilities . . . . . . . . . . . . . . . . . 35
3.5.2 Dashboard . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4 Experiments 39
4.1 Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.1.1 Dataset generation . . . . . . . . . . . . . . . . . . . . . . 40
4.1.2 Experiment setup . . . . . . . . . . . . . . . . . . . . . . . 40
4.1.3 Retrieval simulation . . . . . . . . . . . . . . . . . . . . . 40
4.2 Real browser experiments . . . . . . . . . . . . . . . . . . . . . . 44
4.2.1 Experiment setup . . . . . . . . . . . . . . . . . . . . . . . 44
4.2.2 Retrieval experiment . . . . . . . . . . . . . . . . . . . . . 44
5 Discussion 47
5.1 Geographical enhanced routing . . . . . . . . . . . . . . . . . . . 47
5.2 Building distributed systems with WebRTC . . . . . . . . . . . . 48
CONTENTS xi
6 Conclusion 51
6.1 Further work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
A Software packages 59
A.1 peerjs-rpc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
A.2 peerjs-mock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
A.3 peerjs-rpc-mock . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
A.4 rpc-dashboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
B List of technologies 65
xii CONTENTS
List of Figures
2.1 The highlighted part of the circle shows the key-space assigned
to peer N2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 The two figures shows the lookups necessary to find an item stored
on N8 from N1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 A permission prompt shown to ask for the users approval of ge-
ographical location access. . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Example code showing the difference of promises and callbacks . 14
2.5 Sequence diagram for a connection process in WebRTC . . . . . 22
xiii
xiv LIST OF FIGURES
List of Tables
xv
xvi LIST OF TABLES
List of Acronyms
CI Continuous Integration.
OS Operating System.
xvii
xviii List of Acronyms
Peer-to-Peer systems have been used for multiple purposes for several years.
However, most peer-to-peer systems to this date are developed to run directly on
an Operating System. WebRTC introduces a way to build peer-to-peer systems
within the browser. Thus, the browser is a sandboxed environment suitable
for peer-to-peer applications. WebRTC is divided in two separate use cases,
streaming media or sending data. To this day, WebRTC implementations mostly
utilize the media-channel. There are existing solutions for video-conference calls
and streaming of live video. However, the data-channel of WebRTC has had a
lack of attention. The attention is picking up as the WebRTC specification and
libraries around it is further developed. Thus, more proofs-of-concepts are built
to show the benefit from using WebRTC to transfer data.
Most of the proof-of-concepts that have surfaced based on the WebRTC data
channel focuses on privacy, e.g. file-sharing applications that allow users to send
files directly to each other without storing the files on a server. Furthermore,
there is new text chat application, called friends1 , which communicate between
the people using it with WebRTC.
Web Real-Time Communication (WebRTC) will probably chance the way users
of applications on the web interacts with each other. It is a young technology
with a lot of promise. This research project investigates the possibilities of
the technology with a proof-of-concept implementation of a known peer-to-peer
application, the Chord Distributed Hash Table (DHT). Moreover, the project
proposes to use the geographical location API in modern browsers to enhance
routing performance. The research has a focus on testing and evaluation of
WebRTC based peer-to-peer applications.
1.1 Motivation
There are two main motivations for this research project. The first is to create
a proof of concept of a peer-to-peer application with WebRTC between web-
browsers, from which requires research on different implementations of WebRTC
1 http://moose-team.github.io/friends/
1
2 CHAPTER 1. INTRODUCTION
and frameworks created for WebRTC and research on how to test the peer-
to-peer application. The second motivation is to look into how geographical
location might be used to enhance routing in peer-to-peer applications. Both of
these points are related to how the web is evolving with client side applications
that in some use cases could benefit from using peer-to-peer communication,
and furthermore, could benefit from higher performance based on geographical
location.
Peer-to-peer systems evolved from the rapid growth of the internet and com-
puter networks. A peer-to-peer system embraces the use of a large quantity of
computing nodes requiring the same information to share that information[2].
In a peer-to-peer system, each peer that needs to send or receive information
communicates directly, or indirectly through a similar node, with the node it
sends to or receives from.
One of the best-known types of peer-to-peer applications is Voice over IP (VoIP),
which makes it possible to call directly peer-to-peer. This kind of services
are very popular for video calls, and Skype[3] one of the best-known peer-to-
peer calling services as well as one of the best-known peer-to-peer applications.
Nevertheless, the peer-to-peer architecture has been utilized in different ways.
The applications includes file-sharing[4], web-caching[5][6], distributing sharded
information[7][8][9] and games[10].
Peer-to-peer communication has been utilized when there is no need for a central
server or a central server is not wanted in the architecture. There are several
reasons why a central server architecture is unwanted. One of those reasons
is avoiding a single point of failure, which could be achieved by having several
sites with central servers. However, there are situations when that is not feasible
because the resources might not be available. Moreover, peer-to-peer communi-
cation can be a viable option if there is critical that computing nodes continue
to work and coordinate when the central server goes down.
Furthermore, there are certain applications that would benefit from not having
a central location, which handle communication and coordination. One use-
case that has had problems because of the central architecture is free speech
movements. In addition, platforms that enable free speech movements to do
important work are target of DDoS attacks and blockage in government con-
trolled networks. Events in recent years have shown that there are those who
will go to those lengths in order to shut down and suppress free speech move-
3
4 CHAPTER 2. BACKGROUND
ments1 . Furthermore, it has been seen that services like Twitter has played an
important role in work against oppression. However, they can be easily blocked
by a governmental firewall, because of its architecture based on central servers.
On the other hand, creating tools to avoid governmental firewalls has its negative
factors. In many countries, the governmental firewall is used for good purposes.
They stop criminality and other bad things from happening. However, peer-
to-peer systems exist and most of those who want to avoid governmental fire-
walls with peer-to-peer technology can already do that. Furthermore, creating
peer-to-peer technologies that work inside web pages makes it more accessible
especially for people with lower resources which free speech movements.
Transferring substantial amounts of data is another situation where peer-to-
peer communications are more applicable than a server-client architecture. In a
server-client architecture file transfer between two clients can be slow, because
the files need to be uploaded to the central server from the first client before the
second client can download it from the server. Thus, it takes more time and uses
more of the network resources than it would if the file were transferred directly.
This is especially true if the clients are sharing files are in closer proximity
together, in terms of network latency, than the clients are with the server.
Furthermore, if they are on the same local network transferring files peer-to-
peer would be faster than transferring through a server. Companies who deliver
content with a large footprint in file size(e.g. operating systems, games and game
updates) often utilizes peer-to-peer technologies to deliver their content. The
cost of delivering downloads of updates to a game with high-quality graphics
can be significantly lower when using peer-to-peer communication to use shared
bandwidth and computing power.
Nonetheless, peer-to-peer technologies have been fundamental to online piracy
and other criminal activities. Especially peer-to-peer file sharing is widely used
to share files illegally without regards for ownership rights and licensing of ma-
terials. However, developing technologies like peer-to-peer file-sharing enables
the society to use the existing network to deliver services and content that would
not be possible at an affordable level without it. There is little indicating that
not having peer-to-peer technologies would stop online piracy. Thus, it is pos-
sible to argue that the technology enables more good than harm. Furthermore,
the legal services that provide content that compete with the piracy channels
in the digital world depend on the same technology.
There are multiple strategies that would result in a good distribution in most
cases. However, there will always be some unique cases that will result, in the
worst case, in a skewed distribution. In such a situation, the statement above
will not be valid.
2.2.2 Churn
In peer-to-peer systems, Distributed Hash Table especially, Churn is a concern
for the stability of the system and the network created by the system. Churn
refers to Churn rate, which is the rate a measurement for the amount of peers
a system [12]. There are different reasons in play when peers leave a peer-
to-peer network. In terms of a system designed for maximal uptime like a
peer-to-peer based database(e.g. Cassandra), the prominent reasons are peer
failure, network failure or network isolation. On the other hand, systems like
file-sharing networks are prone to peers leaving the network because their users
decide to leave or choose to turn off the host machine. Hence, the probability of
a high Churn rate differ significantly from system to system. Moreover, different
systems adopt different strategies for recovering after nodes leave the network.
There are two primary strategies for detecting nodes that have left the network
for some reason. The first is by using a heartbeat signal, where the each peer
sends a message to the other peers that are interested to know about the given
peer. The other is by pinging, where the interested peers ping the given peer to
check if the peer is accessible.
2.2.3 Chord
Chord is a Distributed Hash Table built on the concept of consistent hashing. [7]
It uses a circular key-space of m-bit size. The keyspace starts at the lowest sha1
hash and ends on with a hash of the start hash plus m bits. It uses the circular
key-space and the features of consistent hashing to guarantee correctness in
routing of the peer network. The Chord algorithm works in two different modes,
simple key location and scalable key location. The difference of the two nodes
affects lookup time. The simple key location has N messages worst case lookup
effort while scalable key location uses log N messages in the worst case. In both
cases, N is the number of nodes in the network. The differences between those
two approaches are described in more detail in sections later on.
Each peer has the responsibility of a range of that key-space, that range stretches
from the peers predecessors key to the peers key. Figure 2.1 shows the key-
space of peer N2 highlighted. The key-space ring is self-preserved in the way
that there is no central organization of the ring. Each node has a hash value
and by asking known nodes for its successor the node finds the correct place
in the ring. Chord runs maintenance tasks at regular intervals to maintain the
successor and predecessor connections when new nodes join the ring. Thus,
there is no need for a joining node to announce itself directly after joining as
2.2. DISTRIBUTED HASH TABLES 7
the stabilize task will announce it to the correct node eventually. The stabilize
task runs at regular intervals on each node and makes sure that each node that
should know of the new node knows.
N1
N5
N2
N4
N3
Figure 2.1: The highlighted part of the circle shows the key-space assigned to
peer N2
N1 N1
N 10 N 10
N2 N2
N9 N9
N3 N3
N8 N8
N4 N4
N7 N7
N5 N5
N6 N6
(a) Simple key location (b) Scalable key location
Figure 2.2: The two figures shows the lookups necessary to find an item stored
on N8 from N1
8 CHAPTER 2. BACKGROUND
This is the first of two key location lookup algorithms. It is slower than the
Scalable key location lookup in terms of number of nodes lookup necessary to
get from a key location to another. It has a worst case scenario of O(N ) lookups.
On the other hand, it requires very little maintenance since each node only needs
to know its successor because a correct lookup is guaranteed if each node knows
its successor.
Simple key location can result in N number of lookups with N nodes, which
might result in a low performance. In order to work around this the number of
lookups needs to be significantly less with high numbers of nodes. The scalable
key location is a strategy to lower the nodes that need to be asked in a location
lookup.
The scalable key location maintains a finger table at each node with information
about knowledge of certain nodes in the ring. It has an entry for 2n bit increase
in the key, which means that entry n in the table refers to the key of the node,
holding the finger table plus 2n bits. The finger table makes it possible to
lookup farther away in the keyspace without asking each node in between. This
will significantly lower the number of nodes a node need to ask to lookup a key
location. It will lower the worst case from O(N ) to O(logN ) in a network of N
nodes.
2.2. DISTRIBUTED HASH TABLES 9
function n.findSuccessor(hash)
if f inger[i] ∈ (n, successor) then
return successor
else
n0 = n.closestP recedingN ode(hash)
return n.f indSuccessor(hash)
end if
end function
The jump increases the farther along the ring the node is placed, thus for each
entry in the finger table the keyspan between the referring and the referred node
increases. Since the difference is in a power of two, the range will double for each
entry in the finger table. This have several benefits: Looking up closer node has
a higher probability of reaching the correct node with one lookup, which is good
because then it is possible to lookup approximately correct node and get from
that node to the correct with a few more lookups. The benefit of increasing
the range for every step farther away is lowered maintenance. Thus, it is a
trade-off between detailed knowledge and maintenance, and because a node is
more likely to look up a closer node more often it is acceptable with a higher
level of maintenance closer to the node. This strategy inherits the guarantees of
simple key location because the first row in the finger table will always contain
the successor current node.
The finger table is continuously updated and maintained. A task, which runs
periodically, updates an entry in the finger table by trying to lookup the key of
that place in the finger table.
The client side of the web technology stack consists of HTML and ECMAScript,
in addition, there is the APIs defined by browsers that are an extension of
the HTML specification and the ECMAScript specification. ECMAScript is
a scripting language defined by the ECMA-262 specification[13]. There are
different implementations of ECMAscript, the most popular and the one with
widest support in browsers is JavaScript, see section 2.4 for more background
on JavaScript. The HTML 5 specification ads numerous features to the web
technology stack. A few of those features are relevant and can be useful for
distributed systems in the browser. The sections below introduces the most
relevant ones.
Figure 2.3: A permission prompt shown to ask for the users approval of geo-
graphical location access.
It is possible to retrieve longitude, latitude and accuracy from the API. Those
data make it possible to pinpoint the location of the machine the browser is
running on. Furthermore, with two locations it is possible to calculate the dis-
tance in between the two with Lambert’s formula. It calculates the spherical
distance based on the radius of the earth. Based on the formula and the data
types available in JavaScript the precision of the distance will be on the me-
ter scale, thus comparing in kilometer scale should be accurate given that the
geographical location can be detected with acceptable accuracy.
2.3. WEB TECHNOLOGY 11
Communication between client and server through HTTP has a significant over-
head, by which real-time communication will suffer. There is three different ways
of handling real-time communication between client and server: HTTP polling,
HTTP long-polling and WebSocket[15]. The two first works by sending HTTP
requests to the server when the client application assumes that there is some
new information. The difference between the two is the frequency of the polling.
The polling of information that might be ready requires HTTP requests on reg-
ular intervals, which can be effective and a good solution if the data requested
is accessible on regular intervals. In the case when the data becomes available
sporadically and in an unpredictable manner, polling will waste resources in
the form of network traffic and and increased latency. WebSocket on the other
hand, is designed for the latter use case. WebSocket is a full-duplex communi-
cation between client and server. Thus, when the connection is created, both
parties can send information to the other party. This can be useful of notifying
the client of new data or regularly sending data one or both ways or stream-
ing data, e.g. logs, from the server to the client. A W3C specification[16] the
WebSocket API and communication.
The Web Storage API specification defines an interface for accessing and storing
data in web clients outside cookies[17]. There are two types of storage defined in
the API, which is SessionStorage and LocalStorage. Both are created to address
needs that cannot be addressed by using cookies.
In addition to the Web Storage API, there is another way to store information in
web clients: cookies[18]. Cookies have been a way to store state in web browsers
for more than two decades. Cookies are accessible both to the client and server
side as they are attached to all HTTP requests. The original use case for cookies
was to create a shopping cart for e-commerce websites. The requirements for
storing of state in web browsers has since grown into many different use cases.
There are several use cases where cookies will affect the web application in a
negative manner. There is also a security concern when using cookies, especially
over an unencrypted connection. Since the cookie is sent with every request it
is possible to listen to network communication, from which it is possible to get
cookies of other web users on the network. The Web Storage API, makes it
possible to avoid this in the cases where the state is only useful in the client and
there is no need to send it to the server with each request or to send it to the
server at all.
The first storage in the Web Storage API, SessionStorage, fills the need for
storage in the client that is accessible just to the current session. Isolating a
storage to the current session can be important in applications that a user would,
12 CHAPTER 2. BACKGROUND
2.4 JavaScript
JavaScript is an implementation of the ECMAScript specification. The language
was initially developed by Brendan Eich. It is a dynamically typed programming
language and also defined as a prototype-based scripting language. In today’s
browsers, it is the de-facto standard for client side programming on the web
stack[19] as it has been for several years. Moreover, JavaScript can run both in
a web browser and directly in an OS. Not only can the language run in both
situations, but in various cases the same code base can both run in a browser
and on the directly in the Operating System.
1 // callback version
2 function l o a d W i t h C a l l b a c k ( arg1 , callback ) {
3 a sy nc O pe ra t io n 1 ( arg1 , function ( error , result ) {
4 if ( error ) return callback ( error , null ) ;
5 a sy nc O pe r at io n 2 ( result , function ( error , result2 ) {
6 if ( error ) return callback ( error , null ) ;
7 asy ncOper ation ( result , function ( error , result3 ) {
8 if ( error ) return callback ( error , null ) ;
9 callback ( result3 ) ;
10 }) ;
11 }) ;
12 }) ;
13 }
14
15 load ( function ( error , result ) {
16 if ( error ) {
17 // handle error
18 } else {
19 // handle success
20 }
21 }) ;
22
23 // promise version
24 function lo ad W it hP r om is e ( arg1 ) {
25 return a s yn cO p er at i on 1 ( arg1 )
26 . then ( function ( result ) {
27 return a s yn cO p er at i on 2 ( result ) ;
28 })
29 . then ( function ( result ) {
30 return a s yn cO p er at i on 3 ( result ) ;
31 }) ;
32 }
33
34 load
35 . then ( function ( result ) {
36 // handle success
37 })
38 . catch ( function ( result ) {
39 // handle error
40 }) ;
Figure 2.4: Example code showing the difference of promises and callbacks
2.4. JAVASCRIPT 15
In JavaScript, there is only one construct, Object. All types derive from an
object. Thus, implementing inheritance and class like data structures must be
based on objects. The closest concept to a class that is available in JavaScript
is prototype objects. Each object has an internal link to a prototype. The
prototype object also has an internal link to a prototype object, unless it is
null. Thus, it is possible to create inheritance with prototype objects.
The prototype chain is a chain of linked prototype objects which ends with null.
All prototype objects is a set of properties as with regular objects. Lookups
of properties in an object first checks if the object has an own property with
the given name before checking the prototype chain closest to farthest away.
Moreover, since objects store functions as properties, this makes it possible to
inherit and override functions by extending the prototype chain.
ES6, the next version of ECMAscript, introduces a new keyword to the language
specification: class. It is syntactical sugar for the process of extending objects
and prototype chains to achieve inheritance.
bluebird/blob/f114841282193642484ddb8dc315fc45355bbdb0/benchmark/README.md (re-
trieved 26.04.2015)
16 CHAPTER 2. BACKGROUND
2.5 Docker
Docker is a platform for virtualization. Its goal is to be an easy way to build,
deploy and move instances of distributed applications. The key idea is that
processes are run within a container that isolates the process from the host op-
erating system, by which makes the host not as vulnerable from the application
running inside the container[24]. Moreover, it gives the possibility to run sev-
eral similar applications on the same host without the risk of the applications
interfering with each other. On of the key features of docker is its portability,
it is possible to move a container from one host to another without much delay.
it still needs to reserve 1GB of RAM from the host system. Therefore, running
the same application under docker one would be able to run more instances on
the same hardware compared to regular virtual machines.
The time necessary to start a virtual machine and install the application could
take a while. Docker tries to avoid that by creating the image used for a con-
tainer one time and running it several times. Building the image takes approx-
imately the same time as installing the application on a regular machine, then
starting the container takes an insignificant amount of time. This makes docker
more suitable than regular virtual machines in the cases where there is a need to
start several instances simultaneously or to start an instance in a short amount
of time.
Docker can affect the host system more than a regular virtual machine because
it does not have the same restraints on resources. There are restraints in place
when using Docker, but they are more liberal than and not as strictly defined
as they are in regular virtual machines. However, it is possible to limit the
resources that a given Docker container can use, e.g. set the limit of use of
RAM to 1GB.
still factors that change between different runs, but several of them are control-
lable(e.g. the amount of resources in terms of CPU, RAM, etc. available to the
test).
2.6 WebRTC
WebRTC, or Web Real-Time Communication is an addition to the ECMAScript
API specification[26]. The goal of WebRTC is to enable peer-to-peer commu-
nication directly between browsers and other internet devices through a well-
defined API[27]. Earlier one had to use plugins to enable communication be-
tween browsers, however as a result of the HTML 5 API enhancement. It nat-
urally followed to add an API for peer-to-peer communication. The WebRTC
specification and API has two separate types of communication.
There are other implementations of WebRTC than browsers, even though this
is meant for browsers. To mention a few, there are reusable frameworks for iOS
and Android. Those exist for different reasons. Firstly, the mobile browsers have
not adopted WebRTC fully, and a few have not adopted it at all. Furthermore,
there are applications that benefit from having access to native APIs on mobile
devices. In addition to the mobile platform implementations, there are multiple
open-source unofficial implementations of WebRTC in different programming
languages.
2.6.3 Signaling
WebRTC is based on peer-to-peer communication, but since it is designed to be
used by consumers without configuring networks beforehand there is a need for
connection broker[29]. The modern networks that are combined as the internet
are built of multiple small networks. The small networks can have firewalls
blocking certain traffic or have one IP-address. In cases where a whole network
has one public IP-address, the router controlling the network needs to translate
7 Services like appear.in and talky demonstrate this.
2.6. WEBRTC 21
packages sent to the network for it to reach the correct destination. In most
cases, this is handled by a technology called Network address translation. To be
able to connect directly between peers from different networks behind NAT and
firewalls, it is necessary to have a common broker to negotiate the connection.
In other words, WebRTC is peer-to-peer but still needs a server to connect in
some cases. However, if the peer is publicly available and it knows to listen for
incoming messages, there is no need for signaling through a server.[30] Thus,
the two peers can negotiate the creation of a data transfer channel directly.
The signaling specification is defined by JavaScript Session Establishment Pro-
tocol[31] rather than the WebRTC specification because it is intended to be
more general and make it possible for the implementation to make the signaling
decisions. Thus, the requirements of the protocol are specified in its document.
Figure 2.5 shows a sequence diagram of the process of signaling between two
peers. They both have a connection to the signaling server that is used to
communicate offers and answers. Peer1 tries to connect to Peer2. The process
starts with the creation of an offer on Peer1 and attaches a local descriptor.
The descriptor contains meta-information about Peer1 and is set as the local
descriptor on Peer1 ’s data-channel object as well. Moreover, Peer1 sends the
offer to the signaling server through the connection already established. Then
the signaling server forwards the offer to Peer2 if it has a connection to Peer2.
Otherwise, the signaling server reports back to Peer1 with a message that Peer2
is not available. If Peer2 is available and receives the offer and wants to connect,
it creates an answer set local descriptor on it and itself, as Peer1 did with the
offer. Furthermore, it must set the local descriptor of the offer as the remote
descriptor on its data-channel instance. Then Peer2 sends the answer to the
signaling server, by which sends it to Peer1. After receiving the answer Peer1
sets the descriptor attached to the answer as the remote descriptor on its data-
channel instance and the WebRTC data-channel connection is open and can be
used to send data between the two peers. Also, the answer and offer payload
might contain interesting metadata like browser type and version, which can
be useful when sending data since different browsers support different ways of
serializing payloads.
createOffer()
setLocalDescription()
sends offer
sends offer
createAnswer()
setLocalDescription()
setRemoteDescription()
sends answer
sends answer
setRemoteDescription()
sections below.
Peer.js
Peer.js is a framework with assets that are useful on the client side and the server
side. On the server side, they have a package for Node.js which can be used as
a signaling server. The team behind Peer.js also offers this as a service from
peerjs.com, which is a hosted version of the node-package. On the client side,
2.6. WEBRTC 23
SimpleWebRTC
nium script will move on. Otherwise, the script will raise a condition. Thus,
it is feasible to run a browser with selenium until a condition in the JavaScript
application is fulfilled and then quit the browser.
Headless browsers
The headless browser makes it possible to run tests that need a browser on a
server without a graphical interface. The browser software needs a graphical
interface to render web pages. They are created to be used with graphical
interfaces not to run on servers. However, it is possible to run a normal browser
as a headless browser by attaching its graphical interface to a virtual X-screen.
X is a window system used on Unix-like systems and there exists a virtual
version of it called Xvfb. Thus, it is possible to run a browser, e.g. Firefox
or Chrome, on a Linux server, without attaching it to a screen. Furthermore,
running browsers with Xvfb is helpful when running several browsers inside
different Docker containers as described in section 2.5.4.
Testing of correctness can be divided into two parts. The first is testing of cor-
rectness in the code of the project and the second is testing for correctness with
the whole stack. The latter would be an integration test. If one assume that the
underlying abstractions work as expected testing for correctness in the project
code should be enough, however it is always better to test with integrations
in place because software might not always work as expected. The first part
is achievable by mocking the WebRTC API or the API of the abstraction of
WebRTC that is used. Mocking WebRTC will also make the tests run outside
a browser with Node.js testing tools like mocha. Thus, the tests run faster and
can run on continuous integration without a headless browser.
Testing in at scale
27
28 CHAPTER 3. DESIGN
nodes as promise chains, and it can also be a part of a bigger promise chain.
Using promise chains for all asynchronous task makes sure that all asynchronous
events happen in the correct order. Furthermore, since the RPC module pop-
ulates the error between nodes and reject promises as if the error happened
locally, the correct node handles errors in the correct place. Thus, the error is
handled on the closest place to its origin where it is understood. In most cases
in Chord, that place is on the node that initiated the original sequence of events
like a get-call or a call to findSuccessor.
Chord
peer.js-rpc
Peer.js
WebRTC API
Web Browser
This section will describe the implementation of the regular Chord algorithm,
see section 2.2.3, while section 3.3 describes the changes needed to be able to
enhance the routing based on geographical location.
The implementation of Chord on WebRTC needs to be designed to work in a
modern browser with support for WebRTC. It is important to have a dependency
management system, by which can solve dependency injections of JavaScript
libraries, as well as local files. The choice for this project was Browserify, see
section 2.4.6. It makes it possible to run the code in Node.js or io.js in addition
to browsers as it leverages CommonJS dependency injection. The ability to run
the code in Node.js or io.js as well as browsers makes it possible to run tests
and simulations without using browsers. Thus, the feedback loop from unit
tests becomes smaller since unit tests can be run without a browser or browser
emulator. Most of the unit test needs to mock WebRTC/Peer.js to restrict the
scope of a given test. Thus, mocking Peer.js and running unit-tests in Node.js
with Mocha results in a better development process. Furthermore, the ability
to run simulations in Node.js makes it possible to test the performance of the
routing in given environments and situations, in addition to, the ability to create
more sophisticated tests with a whole chord ring running inside a Node.js script.
3.2. CHORD ON WEBRTC 29
3.2.1 Structure
The implementation is divided several prototype objects. There is four main
classes Chord, Node, Task and Storage. The sections below describes them in
detail. Figure 3.2 illustrates the relations between prototype objects. Each line
represents references between instances, the object above has a reference to the
object below. Also, the RPC prototype object in the figure is from peerjs-rpc.
Chord
Storage
ID This object stores the hash of the node as hex-string and byte arrays
and has the functionality to compare with other ID-objects. Furthermore, this
30 CHAPTER 3. DESIGN
object also can check whether an ID is within in the range between the two
other ID objects. It is in its object in order to use the functionality without
using separate this functionality from the Node object, described above, was
Task The task object maintains the running of scheduled tasks, e.g. fixFingers.
It encapsulates the built-in setInterval in the JavaScript language. It handles
starting and stopping of tasks and error handling within tasks. In addition, it
stores an counter of how many times the task have run for debug purposes.
3.2.2 Routing
Chord has two different types of key location lookup, from which there are
two different implementations of findSuccessor, as described in section 2.2.3
and section 2.2.3. The implementation in this project solves this by adding
an argument on calls to findSuccessor that defines the key location lookup
strategy. Thus, the result is a merged version of the two shown in algorithm 1
and algorithm 2 in section 2.2.3 The updated findSuccessor function is shown in
algorithm 4. Using an argument on the lookup functions instead of a separate
prototype object for each of strategies makes it possible to change on the fly.
Thus, it is possible to do a key location lookup with different strategies without
creating a new ring, which necessary to test comparatively between the different
strategies.
5: function STRATEGIES.scalable(hash)
6: n0 = n.closestP recedingN ode(hash)
7: return n.f indSuccessor(hash, ”scalable”)
8: end function
finger table and predecessor- and successor-fields have to store the geographical
location information. Secondly, the routing must be changed to use the location
information to route more efficiently. In order to use the location of a node to
route better, the algorithm has changes in the closest preceding node lookup in
the finger table.
Algorithm 5 shows the new closest preceding node lookup. The difference from
the original lookup is the when the loop finds the closest node in the finger table,
see line 3. The new lookup will check if the node that is one step farther away, in
the keyspace, in the finger table is geographically closer. If it is, the next closest
node in the finger table will be chosen instead of the closest. Figure 3.3 shows
the lookup routing from N 1 to a key that belongs to N 8, in which figure 3.3(b)
is the lookup with geographical enhancement while figure 3.3(a) is the same as
figure 2.2(b) from section 2.2 shown for comparison.
Furthermore, the findSuccessor needs changes in order to call the correct closest
preceding node function. Algorithm 6 contains changes necessary, in which
line 9 to 12 contains a new function that calls the geographical aware closest
preceding node lookup. Since line 16 calls the correct strategy function based
on the strategy argument it is possible to decide whether to use simple(see
section 2.2.3), scalable(see section 2.2.3) or geographical enhanced lookup on
each separate call to a lookup function.
The approach described above preserves the correct lookup guarantee of the
original Chord implementation. The guarantee states that if all nodes have the
correct successor the lookup will always return the correct node. The nodes
returned by the closest preceding node lookup in the finger table always will
return a node that is between the node asking and the node containing the
32 CHAPTER 3. DESIGN
requested key location. Thus, it will at some point reach the predecessor of
the node containing the requested key location. Therefore, the findSuccessor
function will return the successor of the node asked, as shown on line 14 in
algorithm 6.
The error handling strategy for used in this project is based on the promise pat-
tern, from which custom placing of error handling is achievable. In section 2.4.2,
it is mentioned that promise chains will break on error, and the error populates
to the first catching element in the chain. Thus, in asynchronous distributed
chains of events the error will still be handled at a place where the error the
application has a large enough context to understand how to handle the error.
Furthermore, the intention is to handle the error as close to the origin of the
error as possible. However, if one invoke of a function on another peer throws
an error the peer that throws the error might not know what the error affects.
Thus, sending it back to the invoking peer is the wanted result, because that
peer has the context to handle the error. The promise chain from the RPC
module handles this so that the invoking node gets the error.
In some cases, the error needs to be handled locally. It is possible to handle
errors locally on the invoked peer by adding a catch on the local promise chain.
The catch block handles the error and then rethrow the error, or it throws a
new error after handling the original error. Thus, both the invoking peer and
the invoked peer is aware of the error and can handle it appropriately.
3.5. TESTING AND SIMULATION 33
N1
N 10
N2
N9
N3
N8
N4
N7
N5
N6
(a) Scalable key location
N 1Oslo
N 10
N 2Oslo
N9
N 3T okyo
N 8T okyo
N 4Oslo
N 7Oslo
N 5T okyo
N 6Berlin
(b) Geographical key location
Figure 3.3: The two figures shows the lookups necessary to find an item stored
on N8 from N1
5: function STRATEGIES.scalable(hash)
6: n0 = n.closestP recedingN ode(hash)
7: return n.f indSuccessor(hash, ”scalable”)
8: end function
9: function STRATEGIES.geographical(hash)
10: n0 = n.geoClosestP recedingN ode(hash)
11: return n.f indSuccessor(hash, ”geographical”)
12: end function
2 http://karma-runner.github.io/
3.5. TESTING AND SIMULATION 35
The last type, the real browser network tests, are designed to evaluate how the
system performs in as real as possible situation. Section 4.2 in the experiments
chapter describes the experiment and its results in detail. The tests run in
a real browser. Furthermore, to achieve an as real situation as possible there
must be several psychical nodes included in the network and several browser
instances. In addition to those requirements, since one version of the algorithm
uses geographical location there must be nodes running in different locations.
The tests must run from data centers on different continents. Browsers are, as
mentioned in section 2.5, intended to render web pages. Thus, they do not work
on servers without a window management system. These tests, therefore, run
in browsers attached to a virtual window management system, by which makes
the browser believe they render the content on a screen.
In this project, two docker images was used one with Firefox and one with
Chrome. Detailed descriptions of the Docker images is listed in appendix B.
The Chrome docker images was unsuitable to use with geographical location
because it was not feasible to avoid the prompt to allow access to the browsers
location(see section 2.3.1). Thus, the experiments was performed using the
firefox image as it was possible to load a settings profile with geographical
location access set to always accept.
Node.js server works as a central logging service for the test environment.
The Node.js server can also transmit messages through WebSocket channels to
all browsers currently participating in a test running on the given server. Thus,
it is possible to send commands. Such commands are helpful in controlling and
coordinating the test nodes since there is no direct control over the web-browsers.
Examples of implemented commands are refresh, by which all browsers would
refresh the page, and quit, by which all browsers would result in the quitting
all the browsers.
3.5.2 Dashboard
The dashboard is a simple one-page JavaScript app. The dashboard does two
things. Firstly, it connects to all nodes in a list of peer IDs and invokes the same
function on all of them and stores the result in a list. The dashboard passes
the result list to a template that render the information as HTML, before the
rendered template is shown to the user. Secondly, it is possible to invoke a
function on a given node.
Furthermore, the dashboard is built generically, by which makes it usable for
other applications that use the peerjs-rpc module. The dashboard designed to
accept custom templates and JavaScript code that creates listeners for forms in
the template. Also, the name of the function that the dashboard invokes on all
nodes can be set as an option.
38 CHAPTER 3. DESIGN
4 | Experiments
This project had two types of experiments. A simulation ran with io.js on
one server, and an experiment ran in real browsers. It is just the latter that
had access to WebRTC. Thus, the simulation uses mocked communication with
predefined delays. Setup and results of both experiments are described in the
following sections:
4.1 Simulations
The simulation ran in io.js, in other words it ran directly on a machine, not
within a browser. As described in section 2.4.4, io.js makes it possible to run
JavaScript on a machine outside the browser. This makes it possible to evaluate
the performance of the routing in predictable conditions. Even thought the con-
ditions are predictable it does not mean they are necessarily optimal conditions.
It makes it possible to define the conditions to fit the test in question. Thus, it
is possible to add latency, delayed and faulty messaging.
In a test between browsers in a real-life situation there are multiple changing
factors that affects the performance of routing and messaging in general. Those
factors are beyond the control of the application and the WebRTC framework.
The abstraction level in which this application is operating has no control over
network or the browser. Thus, when the message is handed to the browser to
be sent to another peer there are many factors that weigh in on the correctness
and speed of the delivery.
This experiment is designed to evaluate the changes to the Chord implemen-
tation in known conditions. The geographical aware routing will be tested by
adding a fictional delay to the RPC-modul. Thus, all messages sent with the
RPC module will be delayed by a given time based on the given distance between
the sending peer and the receiving peer. Each node will have a preset location,
with latitude and longitude as the Geolocation API would have returned.
The delays are based on data from publications of statistical data of delays on
geographical distances. [1] shows that the round-trip time for a given distance is
as shown in table 4.1. The RTT data is from statistics from the Akamai network,
39
40 CHAPTER 4. EXPERIMENTS
which handles delivery of streaming of live video feeds from one location to the
whole world. Thus, the data is trustworthy as it is taken from statistics of
real use-cases where RTT is a key performance metric. Hence, using that data
to configure latency in the simulations should generate a realistic view of the
latency impact on the RTT in the Chord network. However, this will not account
for the delays in browsers and computation, which will be present in the real
browser test in the next section.
The locations chosen for the experiment are Berlin, Paris, Honolulu, Oslo, Tokyo
and Washington. The distances between those location ranges from 500 km to
10 000 km. All those distances yield additional noticeable latency as they are
longer than the local distance approximation in [1].
loads from JSON-files. The files contains information about each node as well
as their predecessor, successor and finger table. Thus, the test can run directly
without waiting for the network to stabilize. Furthermore, it makes the test
reproducible because it possible to load the same ring several times. Thus with
the code checkout from version control and the correct data file, the simulation
should give the same result when run several times on the same hardware.
In one simulation 1000 queries was performed on two different 1000 node Chord
networks. The first had an even distribution on geographical location, each node
had location based on n mod LOCAT ION S.length, where n is the nodes place-
ment in the Chord ring. The second had randomly generated geographical loca-
tions. They where assigned based on M ath.random(0, LOCAT ION S.length)2 .
Figure 4.1 shows the cumulative timings of the queries in the first Chord net-
work and figure 4.2 shows the cumulative timings of the queries in the second
network.
Figure 4.1: Cumulative timings of 1000 different key location lookups with
evenly distributed geographical location
A lookup of the farthest away from the peer doing the query takes at average
6-7 lookups in a network with 1000 nodes. Hence, if all lookups have the longest
distance the lookup will have 1344 ms in added latency. The simulations had
2 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_
Objects/Math/random
42 CHAPTER 4. EXPERIMENTS
Figure 4.2: Cumulative timings of 1000 different key location lookups with
random geographical location
an average query time below that, as expected since not all connections are over
the longest geographical distances. Table 4.2 lists the average and standard
deviation of the simulations of both the scalable lookup and the geographical
lookup.
Figure 4.3: Cumulative timings of key location lookups with 40 nodes random
geographical location
code/bfmny6kvdzfdbntyx6jktdb/
4.2. REAL BROWSER EXPERIMENTS 45
successor and their finger table was populated. Then the dashboard was used
to query several lookups from each node to different key locations.
Figure 4.5 shows the cumulative time of queries ran on a Chord ring with 40
nodes. It shows that the geolocation enhanced routing also has a benefit when
running in a network of real browsers.
46 CHAPTER 4. EXPERIMENTS
5 | Discussion
47
48 CHAPTER 5. DISCUSSION
noticeable impact in a typical setting with the user running a single browser
regularly. On the other hand, it has a increased load compared to other web
applications and is more unstable. In the development and early experiment,
running several instances of the Chord algorithm in different tabs within the
same browser often resulted in the browser quitting or crashing.
Even if the load is not noticeable in regular use cases, it can easily affect the
test environment and the test result in an experiment as the one described in
section 4.2. Even if in most cases it is not noticeable to a regular users. There are
known issues with browser implementations of WebRTC using a high amount of
CPU power. Those issues lead to poor performance of everything running inside
the browser in general. The general experience is that running several browsers
on one node works until a certain threshold affected by the CPU power. Both
the clock rate and number of cores impacts the number of nodes one can run on
a given machine. Since each instance of the Chord peer runs inside its browser,
it utilizes multicore and several CPUs in a good way.
While the CPU load was high, the Docker experiments showed that the usage
of memory was not any higher than expected from running a web browser. The
Chord implementation itself is not memory intensive, and the expected load on
the experiments running in real browsers indicates that the handling of WebRTC
messages is not memory intensive either.
Nonetheless, WebRTC is a new technology, and it has not been supported by
browsers for long. The specification is not finished; it is still a draft at World
Wide Web Consortium (W3C). Therefore, it is expected that the technology
will evolve in the near future. As more and more take use of it the browser
implementations will become more battle proven and, thus, have a higher chance
of a better performance.
The performance is also affected by the fact that JavaScript is single-threaded
and asynchronous operations are handled after the current running block has
yielded. Hence, blocking operations affects the asynchronous operation in the
way that they can not complete until the blocking operation does. The Chord
implementation does not have any blocking operations since everything is wrapped
in promises. However the stack below might have blocking operations that even
if they are called by asynchronous code it will result in the blocking of the
event-loop, which handles event-callbacks. Hence, it can affect the performance
of sending and receiving messages, e.g. a background task like fixFingers can
slow down the query of a key location.
50 CHAPTER 5. DISCUSSION
6 | Conclusion
This research project has shown that the concept of distributed systems built on
top of Web Real-Time Communication (WebRTC) is feasible, but the technology
is still young. WebRTC has the potential to be a platform for a new set of peer-
to-peer systems in the future. Furthermore the results from the project indicate
that using geographical knowledge can boost performance in such peer-to-peer
systems. The sections below evaluates the results in the light of the research
questions presented in chapter 1.
51
52 CHAPTER 6. CONCLUSION
version control with a single script. Furthermore, this could be integrated with
a Continuous Integration system, by which would make it possible to evaluate
changes to such an application easier. The Continuous Integration system could
set up peers in browsers on different nodes and then ran the tests before pre-
senting them to the developer evaluating the change. An automated process like
this would be a good tool when developing peer-to-peer WebRTC applications.
[1] Erik Nygren, RK Sitaraman, and Jennifer Sun. The akamai network: a
platform for high-performance internet applications. ACM SIGOPS Oper-
ating Systems Review, 2010.
[2] George Coulouris, Jean Dollimore, and Tim Kindberg. Distributed Sys-
tems: Concepts and Design. International computer science series. Addison-
Wesley, 2012.
[3] Salman a Baset and Henning G Schulzrinne. An Analysis of the Skype Peer
to Peer Internet Telephony Protocol. pages 1–11, 2006.
[4] Johan Pouwelse, Pawel Garbacki, Dick Epema, and Henk Sips. The Bittor-
rent P2P file-sharing system: Measurements and analysis. Lecture Notes
in Computer Science (including subseries Lecture Notes in Artificial Intel-
ligence and Lecture Notes in Bioinformatics), 3640 LNCS:205–216, 2005.
[5] Sitaram Iyer, Antony Rowstron, and Peter Druschel. Squirrel: A decentral-
ized peer-to-peer web cache. Proceedings of the twenty-first annual sym-
posium on Principles of distributed computing - PODC ’02, (Podc):213,
2002.
55
56 BIBLIOGRAPHY
[18] Joon S. Park and Ravi Sandhu. Secure cookies on the web. IEEE Internet
Computing, 4(4):36–44, 2000.
[19] D Crockford. JavaScript: The Good Parts. O’Reilly Media, 2008.
[20] Stefan Tilkov and Steve Vinoski. Node.js: Using JavaScript to build high-
performance network programs. IEEE Internet Computing, 14(6):80–83,
2010.
[21] io.js - JavaScript I/O. https://iojs.org/en/faq.html. Retrieved: 2015-
05-03.
[22] Browserify. http://browserify.org/. Retrieved: 2015-04-04.
The packages and framework described below was developed for this project.
They fill a requirement of the project that was not related directly to the research
and, therefore, is excluded from the codebase.
A.1 peerjs-rpc
Design
This module is designed to work with browserify and exposes its functionality
as defined by browserify. It also requires Peer.js with browserify. Thus, Peer.js
must be installed via npm. npm will handle this when installing this module.
All methods exposed by this module supports both Promises and callbacks, as
described in section 2.4.2. The methods will call the callback with error as
the first argument and then the result as the second argument. This is called
Error-First callbacks.
ping(nodeId, callback) This method will ping the node with a given nodeId
and resolve the returned promise and the callback with either true or false
depending on whether the node responds.
1 https://github.com/relekang/peerjs-rpc
2 https://www.npmjs.com/package/peerjs-rpc
59
60 APPENDIX A. SOFTWARE PACKAGES
A.2 peerjs-mock
Design
3 https://github.com/relekang/peerjs-mock
4 https://www.npmjs.com/package/peerjs-mock
A.3. PEERJS-RPC-MOCK 61
A.3 peerjs-rpc-mock
Design
This module has the same API as peerjs-rpc described in section A.1. It is
designed to mock everything beneath peerjs-rpc. Thus, it isolates the algorithm
from WebRTC and Peer.js.
The package overrides the sending and receiving mechanism of peerjs-rpc and
uses a single object containing references to all nodes. The sending mechanism
calls the receiving mechanism on the peerjs-rpc instance that is supposed to
receive the message. In other words, the sending mechanism finds the given
node in the object containing all instances of peerjs-rpc objects and calls the
mechanism in that instance that handles incoming messages directly. The mod-
ule is wrapped in a function, which takes an object as an argument. The object
is used to store the references to all the nodes. This gives the initiator of the
mock the ability to control the node references, e.g. remove an reference from
the object to simulate peer-failure.
This module gives full control of the communication between peers. Thus, it
is possible to create predictable situations by adding delays. The control over
delays between different nodes is important when creating a simulation that is
supposed to test the performance of changes in an algorithm in environments
with different delays between different nodes.
Furthermore, mocking on a higher level than peerjs-mock can be good to avoid
unnecessary allocation of resources, which might result in the ability to run
simulations with a larger number of nodes.
Usage
The module is installed from NPM with npm install peerjs-rpc-mock. Then
it can either be used directly as shown in figure A.2 or put in as mock in require
as shown in figure A.3.
5 https://github.com/relekang/peerjs-rpc-mock
6 https://www.npmjs.com/package/peerjs-rpc-mock
62 APPENDIX A. SOFTWARE PACKAGES
A.4 rpc-dashboard
Design
The dashboard is designed to be a reusable dashboard renderer, by which can
be used to interact with several peerjs-rpc peers. On instance of the dashboard
takes a given set of options, as listed below in the usage section. The dashboard
will connect to all peers from a list of identification strings and then invoke a
function on each peer and store the result in an object. Furthermore, it will
render a template passed as an option and call a function onRendered passed as
an option. After rendering the newly rendered template will replace the content
of an element in the DOM. The template will get a list of the outputs of the
invoked function from the peers in addition to extra context variables passed as
an option.
The dashboard will create an RPC instance, from peerjs-rpc, for the dashboard
with a unique ID prefixed with dashboard. Thus, if the network it is connecting
to initiate actions on connect, the application should filter peers prefixed with
dashboard.
Usage
Figure A.4 shows a code example of a general usage of the rpc-dashboard mod-
ule.
Options
container CSS selector of the element which should contain the dashboard.
rpcOptions Options-object passed to the peerjs-rpc module.
fetchClients A function that returns a list of RPC-peer identification string
or a promise that will resolve such a list.
template A compiled Handlebar-template.
7 https://github.com/relekang/rpc-dashboard
A.4. RPC-DASHBOARD 63
onRendered A callback function that will be called when the template is ren-
dered.
This chapters lists short descriptions for every framework and technology used
in this project that are relevant to this report.
Docker A platform for deploying and running isolated processes. Section 2.5
describes the platform and how it compares to reguar virtual machines.
JavaScript frameworks
65
66 APPENDIX B. LIST OF TECHNOLOGIES
Third-party
io.js A fork of Node.js. It is for the same purpose as Node.js, but is more up
to date. See section 2.4.4 for more detailed description.
Peer.js A wrapper around the WebRTC API that creates a unified interface
for all supported browsers and handles signaling of connections. More detailed
description is in section 2.6
Karma Karma is a test runner that runs JavaScript unit tests in a browser.
It can run tests written with several test frameworks in most browsers.
Docker images
This project used two Docker images for browser testing. Both images takes
two arguments: an url and a timeout. The browser will visit the url and stay
open for the amount of time specified in the timeout.
3 https://github.com/relekang/docker-webrtc-test
4 https://registry.hub.docker.com/u/relekang/chrome-webrtc/