Permalink
Browse files

More work to get users to connect

  • Loading branch information...
1 parent 485807a commit e62d43719e2d63dada95e9b3be144c699002ba39 @aduston aduston committed Mar 12, 2012
View
@@ -1 +1 @@
-web: node web.js
+web: ./node_wrapper.sh
View
@@ -3,8 +3,8 @@ Vagrant::Config.run do |config|
config.vm.box = "lucid64"
config.vm.box_url = "http://files.vagrantup.com/lucid64.box"
+ config.vm.forward_port 80, 8001
config.vm.forward_port 8000, 8000
- config.vm.forward_port 3000, 3000
config.vm.share_folder "project", "/opt/langolab", "."
@@ -1 +1,2 @@
-npm install
+npm install
+npm install -g supervisor
@@ -1,4 +1,6 @@
-var pubsub = require('../singletonpubsub.js');
+var pubsub = require('../singletonpubsub.js'),
+channels = require('./channels.js'),
+_und = require('underscore');
/**
* @constructor
@@ -10,7 +12,7 @@ var InviteeController = function(userID, socket) {
InviteeController.prototype.listenForInvitations = function() {
pubsub.subscribe(
- inviteChannel(this.userID_),
+ channels.inviteChannel(this.userID_),
_und.bind(this.invitationReceived_, this));
};
@@ -21,10 +23,11 @@ InviteeController.prototype.invitationReceived_ = function(token) {
InviteeController.prototype.invitationResponseReceived = function(data) {
var that = this;
pubsub.publish(
- invitationResponseChannel(data.token),
+ channels.invitationResponseChannel(data.token),
data.response);
if (data.response == InviteeResponse.ACCEPTED) {
- var confirmationChannel = invitationConfirmationChannel(data.token);
+ var confirmationChannel =
+ channels.invitationConfirmationChannel(data.token);
var unsubscribe = function() {
pubsub.unsubscribe(confirmationChannel);
};
@@ -1,5 +1,9 @@
var pubsub = require('../singletonpubsub.js'),
-WaitingUser = require('../models/waitinguser.js');
+WaitingUser = require('../models/waitinguser.js'),
+_und = require('underscore'),
+InviterResponse = require('./inviterresponse.js');
+
+var PING_THRESHOLD = 8;
var InviterController = function(userID) {
/**
@@ -15,8 +19,8 @@ var InviterController = function(userID) {
InviterController.prototype.makeInvitations =
function(languagePairs, callback)
{
+ console.log("makeInvitations");
this.callback_ = callback;
-
var userList = [];
var numQueriesExecuted = 0;
var pingTimeout = new Date(new Date().getTime() - PING_THRESHOLD * 1000);
@@ -43,19 +47,26 @@ InviterController.prototype.makeInvitations =
InviterController.prototype.processAndInviteUserList_ =
function(rawUserList)
{
+ if (rawUserList.length == 0) {
+ this.callback_(InviterResponse.ZERO_USERS);
+ return;
+ }
rawUserList = _und.sortBy(
rawUserList,
function(user) { return user.userID; });
rawUserList = _und.uniq(
rawUserList, true,
function(user) { return user.userID; });
+ // see http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Sortbyidtosortbyinsertiontime
rawUserList = _und.sortBy(
rawUserList,
- function(user) { return user.waitingSince; });
+ function(user) { return user._id; });
this.inviteUserList_(rawUserList);
};
InviterController.prototype.inviteUserList_ = function(userIDList) {
+ console.log("inviteUserList_");
+ console.log(userIDList);
var that = this;
var accepted = false;
var numResponses = 0;
@@ -1,13 +1,21 @@
+if (typeof module == 'undefined') {
+ goog.provide('ll.InviterResponse');
+}
+
/**
*
* @enum {string}
*/
var InviterResponse = {
MATCH: 'match',
CONFLICT: 'conflict',
- NONE: 'none'
+ NONE: 'none',
+ ZERO_USERS: 'zerousers'
};
-if (module) {
+if (typeof module != 'undefined') {
module.exports = exports = InviterResponse;
}
+else {
+ ll.InviterResponse = InviterResponse;
+}
View
@@ -1,22 +1,46 @@
-var mongoose = require('mongoose');
+var mongoose = require('mongoose'),
+_und = require('underscore');
var LanguagePairSchema = new mongoose.Schema({
foreignLanguage: String,
nativeLanguage: String
});
var WaitingUserSchema = new mongoose.Schema({
- userID: mongoose.Schema.ObjectId,
- waitingSince: Date,
+ userID: { type: mongoose.Schema.ObjectId, unique: true },
lastPing: Date,
languages: [LanguagePairSchema]
});
WaitingUserSchema.index(
- { waitingSince: 1,
- lastPing: -1,
+ { lastPing: -1,
'languages.foreignLanguage': 1,
'languages.nativeLanguage': 1});
+WaitingUser.statics.ping =
+ function(userID, languagePairs, callback)
+{
+ if (typeof userID == "string") {
+ userID = new mongoose.Schema.ObjectId(userID);
+ }
+ languagePairs = _und.map(
+ languagePairs,
+ function(pair) {
+ return {
+ foreignLanguage: pair[1],
+ nativeLanguage: pair[0]
+ };
+ });
+ console.log(langaugePairs);
+ this.update(
+ { userID: userID },
+ { lastPing: new Date(),
+ langauges: languagePairs },
+ { upsert: true},
+ function(err, numAffected) {
+ callback();
+ });
+};
+
var WaitingUser = exports = module.exports =
mongoose.model("waitinguser", WaitingUserSchema);
View
@@ -0,0 +1,5 @@
+if [ "$NODE_ENV" == "production" ]; then
+ node web.js
+else
+ supervisor web.js
+fi
@@ -0,0 +1,7 @@
+// This file was autogenerated by /opt/google-closure/closure/bin/build/depswriter.py.
+// Please do not edit.
+goog.addDependency('../../js/conversations/invitationlistener.js', ['ll.InvitationListener'], []);
+goog.addDependency('../../js/conversations/invitationmaker.js', ['ll.InvitationMaker'], []);
+goog.addDependency('../../js/conversations/inviterresponse.js', ['ll.InviterResponse'], []);
+goog.addDependency('../../js/conversations/startwaiting.js', ['ll.StartWaiting'], ['ll.WaitingRoutine']);
+goog.addDependency('../../js/conversations/waitingRoutine.js', ['ll.WaitingRoutine'], ['goog.Timer', 'll.InvitationListener', 'll.InvitationMaker', 'll.InviterResponse']);
@@ -1,19 +0,0 @@
-goog.provide('EngagementInvitationListener');
-
-/**
- * @constructor
- */
-EngagementInvitationListener = function(socket) {
-};
-goog.addSingletonGetter(EngagementInvitationListener);
-
-EngagementInvitationListener.prototype.listen = function(socket, listener) {
- if (this.socket_) {
- throw new Exception();
- }
- this.socket_ = socket;
- this.listener_ = listener;
- this.socket_.on(
- 'engagementInvitation',
- goog.bind(this.engagementInvitationReceived_, this));
-};
@@ -0,0 +1,23 @@
+goog.provide('ll.InvitationListener');
+
+/**
+ * @constructor
+ */
+ll.InvitationListener = function() {
+};
+goog.addSingletonGetter(ll.InvitationListener);
+
+ll.InvitationListener.prototype.listen = function(socket, listener) {
+ if (this.socket_) {
+ throw new Exception();
+ }
+ this.socket_ = socket;
+ this.listener_ = listener;
+ this.socket_['on'](
+ 'engagementInvitation',
+ goog.bind(this.invitationReceived_, this));
+};
+
+ll.InvitationListener.prototype.invitationReceived_ = function() {
+ console.log(arguments);
+};
@@ -0,0 +1,20 @@
+goog.provide('ll.InvitationMaker');
+
+/**
+ * @constructor
+ */
+ll.InvitationMaker = function() {
+};
+goog.addSingletonGetter(ll.InvitationMaker);
+
+
+/**
+ * @param {Object} socket
+ * @param {function(ll.WaitingRoutine.InviterResponse)} callback
+ */
+ll.InvitationMaker.prototype.attemptToEngage = function(socket, callback) {
+ socket['emit'](
+ 'makeInvitations',
+ [['en', 'es'], ['en', 'de']],
+ callback);
+};
@@ -0,0 +1,19 @@
+goog.provide('ll.StartWaiting');
+
+goog.require('ll.WaitingRoutine');
+
+var socket = goog.global['socket'];
+
+socket['emit'](
+ 'setUserInfo',
+ goog.global['USER_ID'],
+ goog.global['LANGUAGE_PAIRS'],
+ function() {
+ var waitingRoutine = ll.WaitingRoutine.getInstance();
+ waitingRoutine.start(
+ goog.global['socket'],
+ function(response) {
+ alert(response);
+ });
+ });
+
@@ -1,48 +1,54 @@
-goog.provide("WaitingRoutine");
+goog.provide("ll.WaitingRoutine");
goog.require("goog.Timer");
+goog.require('ll.InvitationListener');
+goog.require('ll.InvitationMaker');
+goog.require('ll.InviterResponse');
/**
* @constructor
*/
-function WaitingRoutine(socket) {
-
+ll.WaitingRoutine = function() {
+
};
-goog.addSingletonGetter(WaitingRoutine);
+goog.addSingletonGetter(ll.WaitingRoutine);
/**
* @param {function(string)} engagementCallback Called with room id
* when connected
*/
-WaitingRoutine.prototype.start = function(socket, engagementCallback) {
+ll.WaitingRoutine.prototype.start = function(socket, engagementCallback) {
if (this.waiting_) {
return;
}
this.waiting_ = true;
this.socket_ = socket;
this.engagementCallback_ = engagementCallback;
- this.invitationListener_ = EngagementInvitationListener.getInstance();
- this.invitationMaker_ = EngagementInvitationMaker.getInstance();
+ this.invitationListener_ = ll.InvitationListener.getInstance();
+ this.invitationMaker_ = ll.InvitationMaker.getInstance();
this.invitationListener_.listen(
socket,
goog.bind(this.invitationReceived_, this));
this.attemptToEngage_();
};
-WaitingRoutine.prototype.attemptToEngage_ = function() {
+ll.WaitingRoutine.prototype.attemptToEngage_ = function() {
this.attemptingToEngage_ = true;
this.invitationMaker_.attemptToEngage(
this.socket_,
goog.bind(this.engagementAttemptReturned_, this));
};
-WaitingRoutine.prototype.engagementAttemptReturned_ = function(result) {
+ll.WaitingRoutine.prototype.engagementAttemptReturned_ = function(result) {
+ console.log("engagementAttemptReturned");
+ console.log(result);
this.attemptingToEngage_ = false;
- if (result['none']) {
+ if (result == ll.InviterResponse.NONE ||
+ result == ll.InviterResponse.ZERO_USERS) {
// no results. we're waiting.
this.startHeartbeat_();
}
- else if (result['match']) {
+ else if (result == ll.InviterResponse.MATCH) {
// we're matched!
this.stopWaiting_();
this.invitationListener_.stop();
@@ -59,17 +65,18 @@ WaitingRoutine.prototype.engagementAttemptReturned_ = function(result) {
}
};
-WaitingRoutine.prototype.startHeartbeat_ = function() {
+ll.WaitingRoutine.prototype.startHeartbeat_ = function() {
// make and start this.heartbeatTimer_
+
};
-WaitingRoutine.prototype.stopWaiting_ = function() {
+ll.WaitingRoutine.prototype.stopWaiting_ = function() {
this.heartbeatTimer_.stop();
this.invitationListener_.stop();
this.waiting_ = false;
};
-WaitingRoutine.prototype.invitationReceived_ = function(invitation) {
+ll.WaitingRoutine.prototype.invitationReceived_ = function(invitation) {
if (!this.waiting_) {
invitation.reject();
}
@@ -0,0 +1,16 @@
+server {
+ listen 80;
+ server_name ll.example.com;
+ client_max_body_size 4G;
+ open_file_cache off;
+ sendfile off;
+
+ keepalive_timeout 5;
+
+ location ~* /closure/(.*) {
+ root /opt/google-closure;
+ }
+ location ~* /js/conversations/(.*) {
+ root /opt/langolab/public;
+ }
+}
@@ -0,0 +1,12 @@
+class langolab::closure {
+ # happens to be the latest revision as of 10/24/2011
+ $revision = 1376
+ $svn_repo = 'http://closure-library.googlecode.com/svn/trunk/'
+ $local_closure_dir = '/opt/google-closure'
+
+ exec { "svn_checkout_closure":
+ require => Package['subversion'],
+ path => "/usr/local/bin:/usr/bin:/bin",
+ command => "svn checkout -r ${revision} ${svn_repo} ${local_closure_dir}";
+ }
+}
Oops, something went wrong.

0 comments on commit e62d437

Please sign in to comment.