Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | NDN Ping example |
| 3 | |
| 4 | Using ping responder on NDN testbed, which responds to Interests in |
| 5 | /<topo-prefix>/ping/<random-number> |
| 6 | |
| 7 | Jeff Burke |
| 8 | jburke@remap.ucla.edu |
| 9 | |
| 10 | See COPYING for copyright and distribution information. |
| 11 | |
| 12 | */ |
| 13 | |
| 14 | // One of NDN project default hubs |
Jeff Thompson | 8b0e387 | 2013-07-08 18:30:37 -0700 | [diff] [blame] | 15 | var hostip = "B.ws.ndn.ucla.edu"; |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 16 | |
Jeff Thompson | 8b0e387 | 2013-07-08 18:30:37 -0700 | [diff] [blame] | 17 | var AsyncGetClosure = function AsyncGetClosure(T0) { |
| 18 | this.T0 = T0; |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 19 | Closure.call(this); |
| 20 | }; |
| 21 | |
Jeff Thompson | 8b0e387 | 2013-07-08 18:30:37 -0700 | [diff] [blame] | 22 | AsyncGetClosure.gotFirstUpcall = false; |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 23 | AsyncGetClosure.prototype.upcall = function(kind, upcallInfo) { |
Jeff Thompson | 8b0e387 | 2013-07-08 18:30:37 -0700 | [diff] [blame] | 24 | if (!AsyncGetClosure.gotFirstUpcall) { |
| 25 | AsyncGetClosure.gotFirstUpcall = true; |
| 26 | dopings(); |
| 27 | } |
| 28 | |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 29 | if (kind == Closure.UPCALL_FINAL) { |
| 30 | // Do nothing. |
| 31 | } else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 32 | |
| 33 | nameStr = upcallInfo.interest.name.getName().split("/").slice(0,-2).join("/"); |
| 34 | document.getElementById('pingreport').innerHTML += '<tr><td width="65%">' + nameStr + ' </td><td align="right">timeout</td></tr>' ; |
| 35 | } else if (kind == Closure.UPCALL_CONTENT) { |
| 36 | var T1 = new Date(); |
| 37 | var content = upcallInfo.contentObject; |
| 38 | nameStr = content.name.getName().split("/").slice(0,-2).join("/"); |
| 39 | strContent = DataUtils.toString(content.content); |
| 40 | if (strContent=="ping ack") { |
| 41 | document.getElementById('pingreport').innerHTML += '<tr><td width="65%">' + nameStr + ' </td><td align="right">' + (T1-this.T0) + ' ms</td></tr>' ; |
| 42 | } else { |
| 43 | console.log("Unknown content received."); |
| 44 | }; |
| 45 | } |
| 46 | return Closure.RESULT_OK; |
| 47 | }; |
| 48 | |
| 49 | function ping(name) { |
| 50 | pingname = name + "/ping/" + Math.floor(Math.random()*100000); |
| 51 | ndn.expressInterest(new Name(pingname), new AsyncGetClosure( new Date() )); |
| 52 | }; |
| 53 | |
| 54 | function dopings() { |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 55 | ping("/ndn/caida.org"); |
| 56 | ping("/ndn/colostate.edu/netsec") |
| 57 | ping("/ndn/memphis.edu/netlab") |
| 58 | ping("/ndn/neu.edu/northpole") |
| 59 | ping("/ndn/parc.com"); |
| 60 | ping("/ndn/pku.edu"); |
| 61 | ping("/ndn/uci.edu"); |
| 62 | ping("/ndn/ucla.edu"); |
| 63 | ping("/ndn/ucla.edu/apps"); |
| 64 | ping("/ndn/uiuc.edu"); |
| 65 | ping("/ndn/wustl.edu"); |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Jeff Thompson | 8b0e387 | 2013-07-08 18:30:37 -0700 | [diff] [blame] | 68 | var ndn = new NDN({host:hostip}); |
Jeff Burke | 0d10e34 | 2012-12-08 11:47:11 -0800 | [diff] [blame] | 69 | var T0 = 0; |
Jeff Thompson | 8b0e387 | 2013-07-08 18:30:37 -0700 | [diff] [blame] | 70 | // Do the first ping to get started. The remaining pings are requested on the first upcall. |
| 71 | ping("/ndn/arizona.edu"); |