Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; NetTest2
- ; Globals
- CompilerIf #PB_Compiler_OS = #PB_OS_Linux
- Global TempDir$ = "/tmp/"
- CompilerElse
- Global Sys32Path$ = GetEnvironmentVariable("WINDIR") + "\system32\"
- Global TempDir$ = GetEnvironmentVariable("TEMP") + "\"
- CompilerEndIf
- Global BgColor = $eeeeee
- Global CurrentStep = 0
- Global PingRunning,DNSRunning,RXRunning,TXRunning,StatsRunning=#False
- Global PingThread,DNSThread,RXThread,TXThread,StatsThread,SizeOfUpload,SizeOfDownload,UploadFileName$
- Global PingResult,DNSResult,RXResult,TXResult,FinRx,RXSpeed.d,FinTX,TXSpeed.d,StatsResult
- Global OSVersionFull$,PingLoss,PingLat.d
- Global Version.s = "103"
- ; Procedures
- Structure EchoResult
- Reply.ICMP_ECHO_REPLY
- Buffer.l[20000]
- EndStructure
- Procedure Ping(Address$,Timeout,StringToSend$)
- hPort = IcmpCreateFile_()
- Result = IcmpSendEcho_(hPort,inet_addr_(@Address$),@StringToSend$,Len(StringToSend$),0,@ECHO.EchoResult,SizeOf(EchoResult),Timeout)
- IcmpCloseHandle_(hPort)
- If Result = 0
- ProcedureReturn -1
- Else
- ProcedureReturn ECHO\Reply\RoundTripTime
- EndIf
- EndProcedure
- Procedure DoPing(Dummy)
- PingRunning=#True
- PingLoss = 0
- PingLat.d = 0
- STS$ = ""
- STSb$ = "EchoThisMessageAsFastAsYouCan..." ;32 bytes
- For b=1 To 400 ; 12800 bytes
- STS$ = STS$ + STSb$
- Next
- For a=1 To 100
- CurrentPing = Ping("195.191.221.2",1000,STS$)
- Delay(100)
- ;Debug CurrentPing
- SetGadgetState(20,GetGadgetState(20)+1)
- If CurrentPing >= 0
- PingLat.d = PingLat.d + CurrentPing
- Else
- PingLoss = PingLoss + 1
- EndIf
- Next
- If PingLoss = 100
- PingResult = 0
- Else
- PingLat.d = PingLat.d/(100-PingLoss)
- PingResult = 1
- EndIf
- CurrentStep = 1
- ;Debug PingLoss
- EndProcedure
- Procedure DoDNS(Dummy)
- DNSRunning=#True
- SetGadgetState(21,1)
- If ReceiveHTTPFile("http://home-nadym.ru/",TempDir$+"ntdns.tmp") = 0
- DNSResult = 0
- SetGadgetState(21,100)
- Else
- DeleteFile(TempDir$+"ntdns.tmp")
- DNSResult = 1
- SetGadgetState(21,100)
- EndIf
- CurrentStep = 2
- EndProcedure
- Procedure DoRX(Dummy)
- RXRunning=#True
- OpenFTP(0,"services.home-nadym.ru","anon","anony")
- If IsFTP(0)
- SetFTPDirectory(0,"pub")
- ExamineFTPDirectory(0)
- While NextFTPDirectoryEntry(0)
- If FTPDirectoryEntryName(0) = "big.file"
- SizeOfDownload = FTPDirectoryEntrySize(0)
- EndIf
- Wend
- StartRX = ElapsedMilliseconds()
- If ReceiveFTPFile(0,"big.file",TempDir$+"ntrx.tmp")
- FinRX = ElapsedMilliseconds()-StartRX
- RXSpeed.d = ((SizeOfDownload/(FinRX/1000))/1024/1024)*8
- If FileSize(TempDir$+"ntrx.tmp") = SizeOfDownload
- RXResult = 1
- Else
- RXResult = 0
- EndIf
- Else
- RXResult = 0
- EndIf
- CloseFTP(0)
- Else
- RXResult = 0
- ;MessageRequester("Error!","Failed to open FTP... [Code: 4]")
- EndIf
- DeleteFile(TempDir$+"ntrx.tmp")
- CurrentStep = 3
- EndProcedure
- Procedure DoTX(Dummy)
- TXRunning=#True
- UploadFileName$ = Chr(Random(25)+97)+Chr(Random(25)+97)+Chr(Random(25)+97)+Chr(Random(25)+97)+Chr(Random(25)+97)+"."+Chr(Random(25)+97)+Chr(Random(25)+97)+Chr(Random(25)+97)
- CreateFile(1,TempDir$+UploadFileName$)
- For a=1 To 400000
- WriteStringN(1,"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
- Next
- CloseFile(1)
- Global SizeOfUpload = FileSize(TempDir$+UploadFileName$)
- OpenFTP(0,"services.home-nadym.ru","anon","anony")
- If IsFTP(0)
- SetFTPDirectory(0,"pub")
- StartTX = ElapsedMilliseconds()
- SendFTPFile(0,TempDir$+UploadFileName$,UploadFileName$)
- FinTX = ElapsedMilliseconds()-StartTX
- TXSpeed.d = ((SizeOfUpload/(FinTX/1000))/1024/1024)*8
- CloseFTP(0)
- OpenFTP(0,"services.home-nadym.ru","anon","anony")
- SetFTPDirectory(0,"pub")
- ExamineFTPDirectory(0)
- While NextFTPDirectoryEntry(0)
- If FTPDirectoryEntryName(0) = UploadFileName$
- If FTPDirectoryEntrySize(0) = SizeOfUpload
- TXResult = 1
- Else
- TXResult = 0
- EndIf
- DeleteFTPFile(0,UploadFileName$)
- EndIf
- Wend
- CloseFTP(0)
- Else
- Global TXResult = 0
- EndIf
- DeleteFile(TempDir$+UploadFileName$)
- CurrentStep = 4
- EndProcedure
- Procedure DetectOS()
- CompilerIf #PB_Compiler_OS = #PB_OS_Linux
- OSVersionFull$ = "Linux"
- CompilerElse
- Select OSVersion()
- Case #PB_OS_Windows_NT3_51
- OSVersionFull$ = "NT3"
- Case #PB_OS_Windows_NT_4
- OSVersionFull$ = "NT4"
- Case #PB_OS_Windows_ME
- OSVersionFull$ = "ME"
- Case #PB_OS_Windows_95
- OSVersionFull$ = "95"
- Case #PB_OS_Windows_98
- OSVersionFull$ = "98"
- Case #PB_OS_Windows_2000
- OSVersionFull$ = "2k"
- Case #PB_OS_Windows_XP
- OSVersionFull$ = "XP"
- Case #PB_OS_Windows_Server_2003
- OSVersionFull$ = "2k3"
- Case #PB_OS_Windows_Vista
- OSVersionFull$ = "Vista"
- Case #PB_OS_Windows_Server_2008
- OSVersionFull$ = "2k8"
- Case #PB_OS_Windows_7
- OSVersionFull$ = "7"
- Default
- OSVersionFull$ = "fail"
- EndSelect
- CompilerEndIf
- EndProcedure
- Procedure DoStats(Dummy)
- StatsRunning=#True
- SetGadgetState(24,1)
- SendPP$ = Str(PingLoss)
- SendPT$ = Str(Round(PingLat.d,#PB_Round_Nearest))
- DetectOS()
- UploadFileName$ = ""
- For i = 1 To 24
- UploadFileName$ = UploadFileName$ + Chr(65+Random(1)*32+Random(25))
- Next
- SendString$ = "http://home-nadym.ru/nettest-stats-collector.php?m=1&v="+Version+"&pt="+SendPT$+"ms"+"&pp="+SendPP$+"&rx="+Str(RXSpeed.d)+"Mbits&tx="+Str(TXSpeed.d)+"Mbits&w="+OSVersionFull$+"&a="+UploadFileName$
- ReceiveHTTPFile(SendString$,TempDir$+"ntstat.tmp")
- DeleteFile(TempDir$+"ntstat.tmp")
- UploadFileName$ = UploadFileName$+".txt"
- CreateFile(12,TempDir$+UploadFileName$)
- CompilerIf #PB_Compiler_OS = #PB_OS_Linux
- Pid = RunProgram("/sbin/ifconfig","-a",TempDir$,#PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
- CompilerElse
- Pid = RunProgram(Sys32Path$+"ipconfig.exe","/all",TempDir$,#PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
- CompilerEndIf
- If ProgramRunning(Pid)
- While ProgramRunning(Pid)
- If AvailableProgramOutput(Pid) > 0
- WriteStringN(12,ReadProgramString(Pid))
- EndIf
- Wend
- Else
- WriteStringN(12,"`ipconfig /all` failed")
- EndIf
- CloseFile(12)
- OpenFTP(0,"services.home-nadym.ru","anon","anony")
- SetFTPDirectory(0,"pub")
- SendFTPFile(0,TempDir$+UploadFileName$,UploadFileName$)
- CloseFTP(0)
- DeleteFile(TempDir$+UploadFileName$)
- ; don't need to check the result, for now
- StatsResult = 1
- SetGadgetState(24,100)
- CurrentStep = 5
- EndProcedure
- Procedure SetTransferStatus(Mode)
- If Mode = 1
- While IsThread(RXThread)
- If IsFTP(0)
- If FTPProgress(0) > 0
- CurPercent.d = FTPProgress(0)/(SizeOfDownload/100)
- Delay(500)
- SetGadgetState(22,Round(CurPercent.d,#PB_Round_Nearest))
- EndIf
- EndIf
- Wend
- SetGadgetState(22,100)
- Else
- While IsThread(TXThread)
- If IsFTP(0)
- If FTPProgress(0) > 0
- CurPercent.d = FTPProgress(0)/(SizeOfUpload/100)
- Delay(500)
- SetGadgetState(23,Round(CurPercent.d,#PB_Round_Nearest))
- EndIf
- EndIf
- Wend
- SetGadgetState(23,100)
- EndIf
- EndProcedure
- ; UI
- ; logo & icons
- ImgLogo = CatchImage(0,?ImgLogo)
- ImgOk = CatchImage(1,?ImgOk)
- ImgFail = CatchImage(2,?ImgFail)
- ImgWork = CatchImage(3,?ImgWork)
- ImgTodo = CatchImage(4,?ImgTodo)
- DataSection
- ImgLogo: IncludeBinary "S:\misc\work\inter\NetTest2-win\logo.bmp"
- ImgOk: IncludeBinary "S:\misc\work\inter\NetTest2-win\icons\ok.bmp"
- ImgFail: IncludeBinary "S:\misc\work\inter\NetTest2-win\icons\fail.bmp"
- ImgWork: IncludeBinary "S:\misc\work\inter\NetTest2-win\icons\work.bmp"
- ImgTodo: IncludeBinary "S:\misc\work\inter\NetTest2-win\icons\todo.bmp"
- EndDataSection
- ; Window
- If OpenWindow(0,#PB_Ignore,#PB_Ignore,300,200,"NetTest 3.0.3 b"+Version,#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
- SetWindowColor(0,BgColor)
- StickyWindow(0,1)
- SetActiveWindow(0)
- BringWindowToTop_(WindowID(0))
- ImageGadget(0,10,10,60,60,ImgLogo)
- ButtonGadget(1,100,150,100,40,"Начать проверку",#PB_Button_Default)
- Else
- MessageRequester("Error!","Can't create window... [Code: 11]")
- EndIf
- ; Events
- Repeat
- ev = WaitWindowEvent(100)
- If #PB_Event_Gadget And EventGadget()=1 And EventType() = #PB_EventType_LeftClick
- If IsGadget(50)
- HideGadget(50,1)
- EndIf
- HideGadget(0,1)
- HideGadget(1,1)
- TextGadget(10,10,10,60,20,"Ping: ")
- SetGadgetColor(10,#PB_Gadget_BackColor,BgColor)
- TextGadget(11,10,40,60,20,"DNS: ")
- SetGadgetColor(11,#PB_Gadget_BackColor,BgColor)
- TextGadget(12,10,70,60,20,"RX Trans: ")
- SetGadgetColor(12,#PB_Gadget_BackColor,BgColor)
- TextGadget(13,10,100,60,20,"TX Trans: ")
- SetGadgetColor(13,#PB_Gadget_BackColor,BgColor)
- TextGadget(14,10,130,60,20,"Send stats: ")
- SetGadgetColor(14,#PB_Gadget_BackColor,BgColor)
- ProgressBarGadget(20,70,10,190,15,0,100,#PB_ProgressBar_Smooth)
- ImageGadget(30,270,10,16,16,ImgWork)
- ProgressBarGadget(21,70,40,190,15,0,100,#PB_ProgressBar_Smooth)
- ImageGadget(31,270,40,16,16,ImgTodo)
- ProgressBarGadget(22,70,70,190,15,0,100,#PB_ProgressBar_Smooth)
- ImageGadget(32,270,70,16,16,ImgTodo)
- ProgressBarGadget(23,70,100,190,15,0,100,#PB_ProgressBar_Smooth)
- ImageGadget(33,270,100,16,16,ImgTodo)
- ProgressBarGadget(24,70,130,190,15,0,100,#PB_ProgressBar_Smooth)
- ImageGadget(34,270,130,16,16,ImgTodo)
- TextGadget(15,10,165,280,20,"Пожалуйста, дождитесь выполнения тестов...",#PB_Text_Center)
- StartTests=1
- EndIf
- If StartTests = 1
- StartTests = 0
- ;Debug CurrentStep
- ;Debug PingThread
- If CurrentStep = 0
- If InitNetwork()
- ; Step 1 - ping
- PingThread = CreateThread(@DoPing(),Dummy)
- Else
- TextGadget(15,10,165,280,20,"Не найден TCP/IP стэк... [Code: 12]",#PB_Text_Center)
- EndIf
- EndIf
- If CurrentStep = 1 And PingResult = 1
- ; Step 2 - DNS
- DNSThread = CreateThread(@DoDNS(),Dummy)
- EndIf
- If CurrentStep = 2
- ; Step 3 - RX
- RXThread = CreateThread(@DoRX(),Dummy)
- CreateThread(@SetTransferStatus(),1)
- EndIf
- If CurrentStep = 3
- ; Step 4 - TX
- TXThread = CreateThread(@DoTX(),Dummy)
- CreateThread(@SetTransferStatus(),2)
- EndIf
- If CurrentStep = 4
- ; Step 5 - Stats
- StatsThread = CreateThread(@DoStats(),Dummy)
- EndIf
- If CurrentStep = 5
- ; Start Over
- ;CurrentStep = 0
- PingThread = CreateThread(@DoPing(),Dummy)
- EndIf
- EndIf
- If PingRunning=#True
- If IsThread(PingThread) = 0
- PingRunning=#False
- If PingResult=0
- ImageGadget(30,270,10,16,16,ImgFail)
- ImageGadget(31,270,40,16,16,ImgFail)
- ImageGadget(32,270,70,16,16,ImgFail)
- ImageGadget(33,270,100,16,16,ImgFail)
- ImageGadget(34,270,130,16,16,ImgFail)
- TextGadget(15,10,165,280,20,"Ping: 100% потерь... [Code: 13]",#PB_Text_Center)
- Else
- ImageGadget(30,270,10,16,16,ImgOk)
- ImageGadget(31,270,40,16,16,ImgWork)
- StartTests = 1
- EndIf
- EndIf
- EndIf
- If DNSRunning=#True
- If IsThread(DNSThread) = 0
- DNSRunning=#False
- If DNSResult = 0
- ImageGadget(31,270,40,16,16,ImgFail)
- ImageGadget(32,270,70,16,16,ImgWork)
- TextGadget(15,10,165,280,20,"DNS не доступен... [Code: 14]",#PB_Text_Center)
- Else
- ImageGadget(31,270,40,16,16,ImgOk)
- ImageGadget(32,270,70,16,16,ImgWork)
- StartTests = 1
- EndIf
- EndIf
- EndIf
- If RXRunning=#True
- If IsThread(RXThread) = 0
- RXRunning=#False
- If RXResult = 0
- ImageGadget(32,270,70,16,16,ImgFail)
- ImageGadget(33,270,100,16,16,ImgFail)
- ImageGadget(34,270,130,16,16,ImgFail)
- TextGadget(15,10,165,280,20,"Не удалось скачать файл... [Code: 15]",#PB_Text_Center)
- Else
- ImageGadget(32,270,70,16,16,ImgOk)
- ImageGadget(33,270,100,16,16,ImgWork)
- StartTests = 1
- EndIf
- EndIf
- EndIf
- If TXRunning=#True
- If IsThread(TXThread) = 0
- TXRunning=#False
- If TXResult=0
- ImageGadget(33,270,100,16,16,ImgFail)
- ImageGadget(34,270,130,16,16,ImgFail)
- TextGadget(15,10,165,280,20,"Не удалось закачать файл... [Code: 16]",#PB_Text_Center)
- Else
- ImageGadget(33,270,100,16,16,ImgOk)
- ImageGadget(34,270,130,16,16,ImgWork)
- StartTests = 1
- EndIf
- EndIf
- EndIf
- If StatsRunning=#True
- If IsThread(StatsThread) = 0
- StatsRunning=#False
- ImageGadget(34,270,130,16,16,ImgOk)
- HideGadget(10,1)
- HideGadget(11,1)
- HideGadget(12,1)
- HideGadget(13,1)
- HideGadget(14,1)
- HideGadget(20,1)
- HideGadget(21,1)
- HideGadget(22,1)
- HideGadget(23,1)
- HideGadget(24,1)
- HideGadget(30,1)
- HideGadget(31,1)
- HideGadget(32,1)
- HideGadget(33,1)
- HideGadget(34,1)
- HideGadget(15,1)
- EditorGadget(50,10,10,280,130,#PB_Editor_ReadOnly)
- AddGadgetItem(50,1,"Пинг:")
- AddGadgetItem(50,2,"Потери пакетов - "+Str(PingLoss)+"%")
- AddGadgetItem(50,3,"Средняя задержка - "+Str(Round(PingLat.d,#PB_Round_Nearest))+"ms")
- AddGadgetItem(50,4,"")
- AddGadgetItem(50,5,"Входящая скорость:")
- AddGadgetItem(50,6,"~"+Str(RXSpeed.d)+"Мбит")
- AddGadgetItem(50,7,"")
- AddGadgetItem(50,8,"Исходящая скорость:")
- AddGadgetItem(50,9,"~"+Str(TXSpeed.d)+"Мбит")
- Global PingResult,DNSResult,RXResult,TXResult,FinRx,RXSpeed.d,FinTX,TXSpeed.d = 0
- Global PingRunning,DNSRunning,RXRunning,TXRunning,StatsRunning=#False
- ;Global PingThread,DNSThread,RXThread,TXThread,StatsThread = 0
- HideGadget(1,0)
- EndIf
- EndIf
- Until ev=#PB_Event_CloseWindow
- ; IDE Options = PureBasic 4.60 (Windows - x86)
- ; CursorPosition = 282
- ; FirstLine = 259
- ; Folding = --
- ; EnableXP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement