-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
158 lines (155 loc) · 5 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var mysql = require('mysql');
var async = require('async') ;
//This function adds an experiment into database
module.exports.addExperiment = function addExperiment(connection,caller,callee,streamType,taskSetup)
{
var peer1Id, peer2Id,experimentId ;
//Async execution - waterfall pattern
async.waterfall([
// Locate first peer in database if exists
function(callback){
var query = "SELECT * FROM peer WHERE device= ? AND os = ? AND browser = ? AND network = ? " ;
var inserts = [caller.device,caller.os,caller.browser,caller.network];
query = mysql.format(query, inserts);
connection.query(query, function(err, rows, fields) {
if (err) throw err;
if( rows.length > 0 )
{
peer1Id = rows[0].peer_id;
}
else
{
peer1Id = -1;
}
callback(null, peer1Id );
});
},
// Insert first peer in database if does not exist
function(arg1, callback){
console.log("2");
if( arg1 == -1 )
{
// If peer1 was not found in DB then add it
query = "INSERT INTO peer (device, os, browser, network) VALUES (?, ?, ?,?)" ;
inserts = [caller.device,caller.os,caller.browser,caller.network];
query = mysql.format(query, inserts);
connection.query(query, function (err, results) {
if (err) throw err;
else {
peer1Id = results.insertId;
callback(null, peer1Id);
}
});
}
else
{
// Otherwise proceed to next step
callback(null, arg1 );
}
},
// Find second peer in the database
function(arg1, callback){
var query = "SELECT * FROM peer WHERE device= ? AND os = ? AND browser = ? AND network = ? " ;
var inserts = [callee.device,callee.os,callee.browser,callee.network];
query = mysql.format(query, inserts);
connection.query(query, function(err, rows, fields) {
if (err) throw err;
if( rows.length > 0 )
{
peer2Id = rows[0].peer_id;
}
else
{
peer2Id = -1;
}
callback(null, arg1 , peer2Id );
});
},
// Insert second peer in database if does not exist
function(arg1,arg2, callback){
if( arg2 == -1 )
{
// If second peer wasnot found then add it
query = "INSERT INTO peer (device, os, browser, network) VALUES (?, ?, ?,?)" ;
inserts = [callee.device,callee.os,callee.browser,callee.network];
query = mysql.format(query, inserts);
connection.query(query, function (err, results) {
if (err) throw err;
else {
peer2Id = results.insertId;
}
});
}
else
{
// otherwise proceed to next step
callback(null, arg1, arg2 );
}
},
// Find the same experiment in the database
function(peer1Id,peer2Id, callback){
var query = "SELECT * FROM experiment WHERE sender_id = ? AND receiver_id = ? AND stream_type = ? AND task_setup = ? " ;
var inserts = [peer1Id,peer2Id,streamType,taskSetup];query = mysql.format(query, inserts);
query = mysql.format(query, inserts);
connection.query(query, function(err, rows, fields) {
if (err) throw err;
if( rows.length > 0 )
{
expId = rows[0].experiment_id;
}
else
{
expId = -1;
}
callback(null, expId , peer1Id , peer2Id );
});
},
// Create and add to database if no such experiment exists
function(expId, peer1Id,peer2Id, callback){
console.log( 'exp id = ' + expId + ' peer1Id ' + peer1Id + 'peer2Id ' + peer2Id) ;
if( expId == -1 )
{
var query = "INSERT INTO experiment (sender_id, receiver_id, stream_type,task_setup) VALUES (?,?,?,?)" ;
var inserts = [peer1Id,peer2Id,streamType,taskSetup];
query = mysql.format(query, inserts);
connection.query(query,function (err, results) {
if (err) throw err;
else
{
experimentId = results.insertId;
callback(null, experimentId );
}
});
}
else
{
callback(null, expId );
}
},
// Add sender(caller) timings into sender_timings table
function(experimentId, callback){
var query = "INSERT INTO sender_timings (experiment_id, exp_timestamp, init_peer_connection,get_stream_from_device, open_data_channel) VALUES (?,?,?,?,?)";
var inserts = [experimentId,caller.timestamp, caller.timings['init_peer_connection'] ,caller.timings['get_stream_from_device'],caller.timings['open_data_channel'] ] ;
query = mysql.format(query, inserts);
//console.log(query);
connection.query(query,function(err, results) {
if (err) throw err;
callback(null, experimentId );
});
},
// Add receiver(callee) timings into receiver_timings table
function(experimentId, callback){
var query = "INSERT INTO receiver_timings (experiment_id, exp_timestamp, init_peer_connection, get_stream_from_device, open_data_channel) VALUES (?,?,?,?,?)";
var inserts = [experimentId,callee.timestamp, callee.timings['init_peer_connection'] , callee.timings['get_stream_from_device'],callee.timings['open_data_channel']] ;
query = mysql.format(query, inserts);
connection.query(query,function(err, results) {
if (err) throw err;
});
}], function (err, result) {
// result now equals 'done'
if (err)
console.log(err);
else
console.log(result);
});
}