API Docs for:
Show:

File: model\Source.js

  1. /*
  2. * BGPlay.js #9660
  3. * A web-based service for the visualization of the Internet routing
  4. *
  5. * Copyright (c) 2012 Roma Tre University and RIPE NCC
  6. *
  7. * See the file LICENSE.txt for copying permission.
  8. */
  9.  
  10. /**
  11. * @class Source
  12. * @module model
  13. */
  14. var Source=Backbone.Model.extend({
  15. defaults:{
  16. type:"source"
  17. },
  18.  
  19. /**
  20. * The initialization method of this object.
  21. * @method initialize
  22. * @param {Map} A map of parameters
  23. */
  24. initialize:function(){
  25. this.attributes.events={};
  26. this.attributes.cur_events={};
  27. this.environment=this.attributes.environment;
  28. this.bgplay=this.environment.bgplay;
  29. this.bgplay.on("change:cur_instant",function(){
  30. this.updateState();
  31. },this);
  32. },
  33.  
  34. /**
  35. * The validation method of this object.
  36. * This method is used to check the initialization parameters.
  37. * @method validate
  38. * @param {Map} A map of parameters
  39. * @return {Array} An array of {String} errors
  40. */
  41. validate:function(attrs){
  42. var err=new Array();
  43. if(attrs.id==null)
  44. err.push("An id is required!");
  45.  
  46. if (err.length>0)
  47. return err;
  48. },
  49.  
  50. /**
  51. * This method updates the state of this source, it is triggered at each change of the current instant defined by Bgplay.
  52. * The objective of this method is to find the current event in which the source is involved.
  53. * If the current event changes then this method triggers a "curEventChange" event (pub/sub) containing the current event.
  54. * If the current event is null then this method triggers a "curEventNull" event (pub/sub) containing the previous event.
  55. * @method updateState
  56. */
  57. updateState:function(){
  58. var oldEvent;
  59. var instant=this.bgplay.get("cur_instant");
  60.  
  61. for (var cur_target in this.get("events")){
  62.  
  63. var tree=this.get("events")[cur_target]; //TreeMap for the current target
  64. var cur_event_for_this_targets=tree.nearest(instant,false,true);
  65.  
  66. oldEvent=this.get("cur_events")[cur_target];
  67. if (cur_event_for_this_targets!=null){
  68. if (oldEvent!=cur_event_for_this_targets || tree.compare(cur_event_for_this_targets.get("instant"), instant)==0){
  69. this.trigger('curEventChange',cur_event_for_this_targets);
  70. }
  71. }else{
  72. this.trigger('curEventNull',oldEvent);
  73. }
  74. this.get("cur_events")[cur_target]=cur_event_for_this_targets;
  75. }
  76.  
  77. },
  78.  
  79. /**
  80. * Returns a string representing this object.
  81. * @method toString
  82. * @return {String} A string representing this object
  83. */
  84. toString:function(){
  85. var out="";
  86. switch (this.bgplay.get("type")){
  87. default:
  88. out=this.get("id")||"";
  89. break;
  90. }
  91. return out;
  92. },
  93.  
  94. /**
  95. * Adds an event involving this source.
  96. * @method addEvent
  97. * @param {Object} An instance of Event
  98. */
  99. addEvent:function(event){
  100. var target=event.get("target");
  101. if (!this.get("events")[target.get("id")]){
  102. this.get("events")[target.get("id")]= new net.webrobotics.TreeMap(
  103. function(obj1,obj2){
  104. if (obj1.getTimestamp()<obj2.getTimestamp() || (obj1.getTimestamp()==obj2.getTimestamp() && obj1.getId()<obj2.getId()))
  105. return -1;
  106. if (obj1.getTimestamp()>obj2.getTimestamp() || (obj1.getTimestamp()==obj2.getTimestamp() && obj1.getId()>obj2.getId()))
  107. return 1;
  108. if (obj1.getTimestamp()==obj2.getTimestamp() && obj1.getId()==obj2.getId())
  109. return 0;
  110. },
  111. {allowDuplicateKeys:false,suppressDuplicateKeyAlerts:true}
  112. );
  113. }
  114. this.get("events")[target.get("id")].put(event.get("instant"),event);
  115. }
  116. });
  117.  
  118. var Sources=Backbone.Collection.extend({
  119. model:Source
  120. });
  121.