/
Traffic.js
72 lines (64 loc) · 1.84 KB
/
Traffic.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
define(["lodash","./Flow","../utilities","../flows/flowModels","dojo/Deferred"],function(_,Flow,utilities,flowModels,Deferred){
/**
* A modelFactory factory
* the returned factory returns a new instance of the model that .matches() the given arguments
*/
function modelFactory(models,fallback,aggregator) {
/**
* Create an instance of Cls with the given arguments
*/
function newCall(Cls,args){
//based on http://stackoverflow.com/a/8843181/934719
return new (Function.bind.apply(Cls,
[0].concat(Array.prototype.slice.call(args)))
)();
}
return function factory() {
var data = aggregator.apply(this,arguments); //aggregated data from flows.
var model = _.find(models,function(m){
if("matches" in m) {
var x = m.matches(data);
return x;
}
return false;
});
model = model || fallback;
return newCall(model,arguments);
};
}
/**
* Factory for flows.
* Returns an instance of Flow or an instance of a subclass of Flow
* that .matches() the given flow data.
*/
var flowFactory = modelFactory(
flowModels,
Flow,
function aggregate(data){
return {
contentType : utilities.getContentTypeFromHeaders(data.response.headers),
path : data.request.path,
responseCode: data.response.code
};
});
/**
* HoneyProxy Traffic Model - Stores a list of flows.
* A new flow gets created from data with the flowFactory.
*/
var Traffic = Backbone.Collection.extend({
initialize: function(){
this.firstFlow = new Deferred();
var firstFlowTrigger = function() {
if(this.length > 0) {
this.firstFlow.resolve("firstflow");
this.off("newflow",firstFlowTrigger);
this.off("reset",firstFlowTrigger);
}
};
this.on("add",firstFlowTrigger);
this.on("reset",firstFlowTrigger);
},
model: flowFactory
});
return Traffic;
});