Advertisement
omegastripes

test_mproclite_top_50_ping.vbs

Aug 11th, 2013
2,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Launch "Main"
  3.  
  4. ' main programm section
  5.  
  6. Sub Main()
  7.    
  8.     Dim arrUrls, objWnd, lWidth, lHeight, i, objNode, j
  9.    
  10.     ' create window for output
  11.     Set objWnd = CreateWindow()
  12.     ' set up window
  13.     With objWnd
  14.         With .Document
  15.             .GetElementsByTagName("head")(0).AppendChild .CreateElement("style")
  16.             .stylesheets(0).cssText = "body, #output {font-family: consolas, courier new; font-size: 9pt;} body {background-color: buttonface;} #output {height: 100%; width: 100%; overflow: scroll; background: #FFF;}"
  17.             .Title = "similarweb.com Top 50 Pings"
  18.             .Body.InnerHtml = "<div id='output'></div>"
  19.         End With
  20.         lWidth = CInt(.Screen.AvailWidth * 0.75)
  21.         lHeight = CInt(.Screen.AvailHeight * 0.75)
  22.         .ResizeTo .Screen.AvailWidth, .Screen.AvailHeight
  23.         .ResizeTo lWidth + .Screen.AvailWidth - .Document.Body.OffsetWidth, lHeight + .Screen.AvailHeight - .Document.Body.OffsetHeight
  24.         .MoveTo CInt((.Screen.AvailWidth - lWidth) / 2), CInt((.Screen.AvailHeight - lHeight) / 2)
  25.     End With
  26.     ' get top 50 websites urls array from similarweb.com
  27.     arrUrls = GetTopSites()
  28.     ' create divs in window document, put urls into divs, add to array divs object where to output results
  29.     For i = 1 To UBound(arrUrls)
  30.         Set objNode = objWnd.Document.CreateElement("div")
  31.         objNode.innerHTML = "<span style='width: 200px;'>" & arrUrls(i) & "</span>"
  32.         objWnd.output.AppendChild objNode
  33.         arrUrls(i) = Array(objNode, arrUrls(i))
  34.     Next
  35.     ' launch separate procs to ping urls
  36.     For i = 1 To 15
  37.         CreateProc "CheckPing"
  38.     Next
  39.     ' wait procs until initialized
  40.     Joint "CheckPing", 4, 0
  41.     ' ping each url
  42.     For i = 1 To UBound(arrUrls)
  43.     ' For i = UBound(arrUrls) To 0 Step -1
  44.         Do
  45.             If TypeName(objWnd) = "Object" Then Exit For ' window closed
  46.             ' find not busy proc
  47.             For j = 1 To Jobs.Count - 1
  48.                 Select Case True
  49.                     Case Jobs(j).SubName <> "CheckPing"
  50.                     Case Jobs(j).Busy
  51.                     Case Else
  52.                         ' push task to proc
  53.                         Set Jobs(j).Node = arrUrls(i)(0)
  54.                         Jobs(j).Url = arrUrls(i)(1)
  55.                         Jobs(j).Busy = True
  56.                         Exit Do
  57.                 End Select
  58.             Next
  59.             WScript.Sleep 1
  60.         Loop
  61.     Next
  62.     ' release all ping procs
  63.     Free "CheckPing"
  64.     ' wait procs until terminated
  65.     Joint "CheckPing", 64, 0
  66.     ' release main sub procs
  67.     Free Id
  68. End sub
  69.  
  70. Sub CheckPing()
  71.     Dim Ip, Rt
  72.     NewVar "Url"
  73.     NewVar "Node"
  74.     NewVar "Busy"
  75.     Busy = False
  76.     On Error Resume Next
  77.     Do
  78.         ' wait until task is received
  79.         Do Until Busy Or Release
  80.             WScript.Sleep 1
  81.         Loop
  82.         If Release Then Exit Do ' all urls pings are completed
  83.         ' make ping
  84.         Ping Url, Ip, Rt
  85.         ' output result
  86.         If Rt = "" Then
  87.             Node.innerHTML = Node.innerHTML & "<span style='color: red;'>n/a</span>"
  88.         Else
  89.             Node.innerHTML = Node.innerHTML & "IP " & Ip & ", Ping " & Rt & " msec"
  90.         End If
  91.         If Err.Number <> 0 Then ' window closed
  92.             Release = True
  93.             Exit Do
  94.         End If
  95.         Busy = False
  96.     Loop
  97. End Sub
  98.  
  99. Function GetTopSites()
  100.     Dim arrUrls, i
  101.     ' request rating from similarweb
  102.     With CreateObject("Msxml2.ServerXMLHTTP.6.0")
  103.         .SetOption 2, 13056 ' SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
  104.         .Open "GET", "https://www.similarweb.com/top-websites", False
  105.         .Send
  106.         ' parse response to array
  107.         arrUrls = Split(.ResponseText, "data-shorturl=""")
  108.     End With
  109.     For i = 1 To UBound(arrUrls)
  110.         arrUrls(i) = Split(arrUrls(i), """", 2)(0)
  111.     Next
  112.     GetTopSites = arrUrls
  113. End Function
  114.  
  115. Function CreateWindow()
  116.     ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
  117.     Dim sSignature, oShellWnd, oProc
  118.     On Error Resume Next
  119.     sSignature = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
  120.     Do
  121.         Set oProc = CreateObject("WScript.Shell").Exec("mshta about:""about:<head><script>moveTo(-32000,-32000);document.title=' '</script><hta:application id=app border=thick minimizebutton=no maximizebutton=no scroll=no showintaskbar=yes contextmenu=no selection=yes innerborder=no icon=""""/><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('" & sSignature & "',document.parentWindow);</script></head>""")
  122.         Do
  123.             If oProc.Status > 0 Then Exit Do
  124.             For Each oShellWnd In CreateObject("Shell.Application").Windows
  125.                 Set CreateWindow = oShellWnd.GetProperty(sSignature)
  126.                 If Err.Number = 0 Then Exit Function
  127.                 Err.Clear
  128.             Next
  129.         Loop
  130.     Loop
  131. End Function
  132.  
  133. Sub Ping(strUrl, strProtocolAddress, lngResponseTime)
  134.     Dim objStatus
  135.     For Each objStatus In GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_PingStatus Where Address = '" & strUrl & "'")
  136.         Select Case True
  137.             Case IsNull(objStatus.StatusCode)
  138.             Case objStatus.StatusCode <> 0
  139.             Case Else
  140.                 strProtocolAddress = objStatus.ProtocolAddress
  141.                 lngResponseTime = objStatus.ResponseTime
  142.         End Select
  143.     Next
  144. End Sub
  145.  
  146. ' do not modify service section
  147.  
  148. sub launch(byval destination)
  149.     dim job
  150.     executeglobal "dim scene, container, signature, subname, jobs, id, state, release"
  151.     release = false
  152.     if not wscript.arguments.named.exists("task") then
  153.         dim elt
  154.         executeglobal "dim found, lost"
  155.         id = 0
  156.         found = 0
  157.         lost = 0
  158.         signature = ""
  159.         randomize
  160.         do
  161.             signature = signature & hex(rnd * 16)
  162.         loop while len(signature) < 16
  163.         set scene = me
  164.         set jobs = createobject("Scripting.Dictionary")
  165.         set jobs(0) = scene
  166.         set container = getobject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
  167.         container.putproperty signature, scene
  168.         startproc destination
  169.         on error resume next
  170.         do until (lost >= found) or release
  171.             for elt = found to 1 step -1
  172.                 if typename(jobs(elt)) = "Object" then
  173.                     lost = lost + 1
  174.                     jobs(elt) = empty
  175.                 end if
  176.                 err.clear
  177.                 wscript.sleep 1
  178.             next
  179.         loop
  180.         release = true
  181.         executeglobal "scene_beforeterminate"
  182.         for elt = found to 1 step -1
  183.             if typename(jobs(elt)) = "VBScriptTypeInfo" then
  184.                 jobs(elt).wscript.timeout = 1
  185.                 jobs(elt).wscript.quit
  186.                 err.clear
  187.                 nojobs = false
  188.             end if
  189.             wscript.sleep 1
  190.         next
  191.         container.quit
  192.         createobject("wscript.shell").popup "scenequit", 1, "primary", 64
  193.     else
  194.         job = split(wscript.arguments.named("task"), ";")
  195.         signature = cstr(job(0))
  196.         id = clng(job(1))
  197.         subname = cstr(job(2))
  198.         do
  199.             for each container in createobject("Shell.Application").windows
  200.                 if isobject(container.getproperty(signature)) then
  201.                     exit do
  202.                 end if
  203.             next
  204.             wscript.sleep 1
  205.         loop
  206.         set scene = container.getproperty(signature)
  207.         set jobs = scene.jobs
  208.         set jobs(id) = me
  209.         state = 4
  210.         executeglobal subname
  211.         state = 24
  212.         do until release
  213.             wscript.sleep 10
  214.         loop
  215.         state = 28
  216.     end if
  217. end sub
  218.  
  219. function startproc(subname)
  220.     startproc = createproc(subname)
  221.     joint startproc, 4, 0
  222.     REM do while getstate(startproc) < 4
  223.         REM wscript.sleep 10
  224.     REM loop
  225. end function
  226.  
  227. function createproc(subname)
  228.     if me is scene then
  229.         if not release then
  230.             found = found + 1
  231.             createproc = found
  232.             set jobs(createproc) = nothing
  233.             createobject("WScript.Shell").exec("""" & wscript.fullname & """ """ & wscript.scriptfullname & """ ""/task:" & join(array(signature, createproc, subname), ";") & """")
  234.         end if
  235.     else
  236.         createproc = scene.createproc(subname)
  237.     end if
  238. end function
  239.  
  240. function getjob(target)
  241.     on error resume next
  242.     if jobs.exists(target) then
  243.         set getjob = jobs(target)
  244.         if err.number = 0 then exit function
  245.         err.clear
  246.     end if
  247.     set getjob = nothing
  248. end function
  249.  
  250. sub share(varname, value)
  251.     scene.newvar varname
  252.     if isobject(value) then
  253.         execute "set scene." & varname & " = value"
  254.     else
  255.         execute "scene." & varname & " = value"
  256.     end if
  257. end sub
  258.  
  259. sub newvar(varname)
  260.     executecommand "dim " & varname
  261. end sub
  262.  
  263. sub executecommand(command)
  264.     executeglobal command
  265. end sub
  266.  
  267. function getstate(target)
  268.     dim elt
  269.     if jobs.exists(target) then
  270.         on error resume next
  271.         set elt = jobs(target)
  272.         getstate = elt.state
  273.         if err.number <> 0 then
  274.             if not(elt is nothing) then
  275.                 getstate = 64
  276.             else
  277.                 getstate = 1
  278.             end if
  279.         end if
  280.         set elt = nothing
  281.     else
  282.         getstate = 64
  283.     end if
  284. end function
  285.  
  286. function isresponsive(target)
  287.     isresponsive = cbool(getstate(target) and 28)
  288. end function
  289.  
  290. sub free(target)
  291.     if jobs.exists(target) then
  292.         on error resume next
  293.         jobs(target).release = true
  294.     else
  295.         dim elt
  296.         for elt = scene.found to 1 step -1
  297.             on error resume next
  298.             if jobs(elt).subname = target then
  299.                 free jobs(elt).id
  300.             end if
  301.             err.clear
  302.         next
  303.     end if
  304. end sub
  305.  
  306. function joint(target, state, timeout)
  307.     dim reftime
  308.     reftime = timer
  309.     on error resume next
  310.     if isnumeric(target) then
  311.         if jobs.exists(target) then
  312.             do while getstate(target) < state
  313.                 if timeisout(timeout, reftime) then
  314.                     joint = false
  315.                     exit function
  316.                 end if
  317.                 wscript.sleep 10
  318.             loop
  319.         end if
  320.     else
  321.         dim elt
  322.         for elt = scene.found to 1 step -1
  323.             err.clear
  324.             if jobs(elt).subname = target then
  325.                 do while getstate(elt) < state
  326.                     if timeisout(timeout, reftime) then
  327.                         joint = false
  328.                         exit function
  329.                     end if
  330.                     wscript.sleep 10
  331.                 loop
  332.             end if
  333.             err.clear
  334.         next
  335.     end if
  336.     joint = true
  337. end function
  338.  
  339. function timeisout(timeout, reftime)
  340.     if timeout > 0 then
  341.         dim delta
  342.         delta = timer - reftime
  343.         if delta < 0 then delta = delta + 86400
  344.         if delta > timeout then
  345.             timeisout = true
  346.         end if
  347.     else
  348.         timeisout = false
  349.     end if
  350. end function
  351.  
  352. sub interrupt(target, timeout)
  353.     if jobs.exists(target) then
  354.         on error resume next
  355.         jobs(target).wscript.timeout = timeout
  356.         jobs(target).wscript.quit
  357.     else
  358.         dim elt, subname
  359.         for elt = scene.found to 1 step -1
  360.             on error resume next
  361.             subname = jobs(elt).subname
  362.             if subname = target then
  363.                 interrupt jobs(elt).id
  364.             end if
  365.             err.clear
  366.         next
  367.     end if
  368. end sub
  369.  
  370. sub push(name, value)
  371.     container.putproperty name, value
  372. end sub
  373.  
  374. function pop(name)
  375.     on error resume next
  376.     if isobject(container.getproperty(name)) then
  377.         set pop = container.getproperty(name)
  378.     else
  379.         pop = container.getproperty(name)
  380.     end if
  381. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement