Skip to content

Commit

Permalink
Added in browser example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccotter committed Jul 30, 2012
1 parent 62db44e commit 946c857
Show file tree
Hide file tree
Showing 11 changed files with 721 additions and 40 deletions.
6 changes: 3 additions & 3 deletions docs/api.rst
@@ -1,7 +1,7 @@

==============================
OpenCoweb OT API Documentation
==============================
=================
API Documentation
=================

Overview
========
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.rst
@@ -1,6 +1,6 @@

======================
OpenCoweb OT API Usage
Usage and Requirements
======================

Overview
Expand Down
34 changes: 3 additions & 31 deletions examples/README.markdown
@@ -1,34 +1,6 @@

# OpenCoweb OT API Examples
#OpenCoweb OT API Examples

## Node.js

First of all, in order to use the OT API with Node.js, you will need to install
the requirejs module. See the README in the root directory of this project for
instructions.

### The example

In this example, clients running on the same machine will collaboratively edit
an array of strings (a list). The command line interface is simple (and a little
lacking), but the code itself should serve a useful purpose.

### Usage

### The code

Two files are provided: `NodeClient.js` and `NodeOTServer.js`. The NodeOTServer
module contains a single class LocalServerConnection that provides communication
for each client. All message passing is through the file system. There are two
files that all peers use: `order` and `lock`. The order file keeps track of a
global total order incrementing integer, and the lock file is used as a mutex
lock for executing critical sections of code (reading/writing to the total order
file, for example).

Each peer is assigned a single file in the specified directory. Peers append
messages to each other's files and each their own files periodically to receive
messages. See `NodeOTServer.js` for more.

The `NodeClient.js` uses the exposed OT API to keep the shared list
synchronized. Notice the timers that purge and send out engine state syncs.
Examples are split up into subdirectories. Each example has a README describing
usage.

73 changes: 73 additions & 0 deletions examples/inbrowser/README
@@ -0,0 +1,73 @@

#OpenCoweb OT Engine API Example - In browser client

##About

This demonstrates how to use the OCW OT engine as a standalone API.

The code in this directory is a Node.js server and simple cooperative list web
application client (logically similar to the OCW CoList demo).

In this scenario, the client is the only entity that uses the coweb-jsoe. The
server exists to facilitate communication among remote clients and fulfill some
requirements of the OT engine (eg: a total order for operations).

##Running and usage

To run the Node.js server, run `node server.js`. To specify a port to listen on,
use `node server.js <port>`.

Navigate to http://localhost:8889 (the default port the server listens on) in
two or three (or more) windows. Note at the top of each webpage there should be
text saying "Site ID: <number>". Use the interface to insert/update/delete the
list. Note that all clients should remain synchronized.

##Server requirements

As demonstrated by the Node.js server, the server must at a minimum provide
certain functionality as described below.

* Assign unique IDs to clients: each participating client needs a unique ID
to distinguish it from others. The server should provide this.
* Ferry operations: remote clients must know when some client performs an
operation (eg: insert). Clients can send oprations to the server, and the
server will send these to all other remote clients.
* Assign a total order to operations: all operations should be totally ordered,
and the server must provide this. When remote syncs are sent to other
clients, the server should send this total order information for each sync.
* Ferry engine state syncs: clients periodically must send their OT engine's
context vector to other clients, and the server must fulfill this.

##Server-Client communication

This section describes how the Node.js server and browser JS client communicate.
Note this protocol in unrelated to coweb-jsoe.

HTTP requests with path ./admin, ./ot, or ./engineSync are how the client talks
to the server. The server expects an HTTP POST JSON message with a `command`
attribute, among other attributes. The server will send back a response with a
`success` attribute, among others.

Each request (except ./admin connect) should include a `site` JSON attribute
with the unique site ID of the requesting client.

Note that while the typical OCW coweb-java communication is cometd, I chose a
simpler approach where the client frequently polls the server for updates
(instead of the cometd approach of the server sending updates to clients as
soon as they are available).

* ./admin
* command = connect
The server allocates a unique token for the requester and sends it back.
This unnique token should be considered the site ID of the client.
* command = fetch
Periodically, the client will ask for an update (engine syncs and engine
ops). All queued engine syncs and ops are send back to the client.
* ./engineSync
* No command attribute here...the client sends its engine's context vector so
that other clients can sync their engine state.
* ./ot
* No command attribute here either, the client just sends an operation to be
queued up and sent to other clients. The server applies the total order
here.

35 changes: 35 additions & 0 deletions examples/inbrowser/config.js
@@ -0,0 +1,35 @@
//
// Config file. Split out from the app for ease of overlaying a new config
// without affecting the app controller.
//
// Copyright (c) The Dojo Foundation 2011. All Rights Reserved.
//
var dojoConfig = {
baseUrl: '/',
async:true,

paths : {
},

packages: [{
name: "coweb",
location: "./coweb",
main: "main"
},
{
name: 'dojo',
location:'https://ajax.googleapis.com/ajax/libs/dojo/1.7.3/dojo',
main:'main'
},
{
name: 'dijit',
location:'https://ajax.googleapis.com/ajax/libs/dojo/1.7.3/dijit',
main:'main'
},
{
name: 'dojox',
location:'https://ajax.googleapis.com/ajax/libs/dojo/1.7.3/dojox',
main:'main'
}]
};

20 changes: 20 additions & 0 deletions examples/inbrowser/index.html
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test bed for exposed OCW OT</title>
<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="http://vhost1629.developer.ihost.com/dojotoolkit/1.7.0/dojo/dojo.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="siteid" style="font-size: 24px;"></div>
<br />
<input type="button" id="insBtn" value="Insert" />
<input type="button" id="updBtn" value="Update" />
<input type="button" id="delBtn" value="Delete" />
Pos:<input type="text" id="pos" value="0" />
Value:<input type="text" id="val" value="item" />
<div id="list"></div>
</body>
</html>

0 comments on commit 946c857

Please sign in to comment.