1'
2' Network.vb
3'
4' Authors:
5'   Rolf Bjarne Kvinge (RKvinge@novell.com>
6'
7' Copyright (C) 2007 Novell (http://www.novell.com)
8'
9' Permission is hereby granted, free of charge, to any person obtaining
10' a copy of this software and associated documentation files (the
11' "Software"), to deal in the Software without restriction, including
12' without limitation the rights to use, copy, modify, merge, publish,
13' distribute, sublicense, and/or sell copies of the Software, and to
14' permit persons to whom the Software is furnished to do so, subject to
15' the following conditions:
16'
17' The above copyright notice and this permission notice shall be
18' included in all copies or substantial portions of the Software.
19'
20' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24' LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25' OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27'
28
29Imports System.Net
30Imports Microsoft.VisualBasic.FileIO
31Imports System.Net.NetworkInformation
32
33Namespace Microsoft.VisualBasic.Devices
34    Public Class Network
35
36        'MonoTODO
37        Public Event NetworkAvailabilityChanged As NetworkAvailableEventHandler
38
39        Private Const DEFAULTTIMEOUT As Integer = 100000
40
41        Public Sub New()
42            'Empty
43        End Sub
44
45        Public Sub DownloadFile(ByVal address As String, ByVal destinationFileName As String)
46            DownloadFile(address, destinationFileName, String.Empty, String.Empty, False, DEFAULTTIMEOUT, False, UICancelOption.ThrowException)
47        End Sub
48
49        Public Sub DownloadFile(ByVal address As Uri, ByVal destinationFileName As String)
50            DownloadFile(address, destinationFileName, String.Empty, String.Empty, False, DEFAULTTIMEOUT, False, UICancelOption.ThrowException)
51        End Sub
52
53        Public Sub DownloadFile(ByVal address As String, ByVal destinationFileName As String, ByVal userName As String, ByVal password As String)
54            DownloadFile(address, destinationFileName, userName, password, False, DEFAULTTIMEOUT, False, UICancelOption.ThrowException)
55        End Sub
56
57        Public Sub DownloadFile(ByVal address As Uri, ByVal destinationFileName As String, ByVal userName As String, ByVal password As String)
58            DownloadFile(address, destinationFileName, userName, password, False, DEFAULTTIMEOUT, False, UICancelOption.ThrowException)
59        End Sub
60
61        Public Sub DownloadFile(ByVal address As Uri, ByVal destinationFileName As String, ByVal networkCredentials As ICredentials, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal overwrite As Boolean)
62            DownloadFile(address, destinationFileName, networkCredentials, showUI, connectionTimeout, overwrite, UICancelOption.ThrowException)
63        End Sub
64
65        Public Sub DownloadFile(ByVal address As String, ByVal destinationFileName As String, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal overwrite As Boolean)
66            DownloadFile(address, destinationFileName, userName, password, showUI, connectionTimeout, overwrite, UICancelOption.ThrowException)
67        End Sub
68
69        Public Sub DownloadFile(ByVal address As Uri, ByVal destinationFileName As String, ByVal networkCredentials As ICredentials, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal overwrite As Boolean, ByVal onUserCancel As UICancelOption)
70            Dim client As WebClient
71
72            If connectionTimeout < 0 Then
73                Throw New ArgumentOutOfRangeException("connectionTimeout")
74            End If
75
76            If IO.File.Exists(destinationFileName) Then
77                If overwrite Then
78                    IO.File.Delete(destinationFileName)
79                Else
80                    Throw New System.IO.IOException(String.Format("The file '{0}' already exists.", destinationFileName))
81                End If
82            End If
83
84            client = New MyWebClient(connectionTimeout)
85
86            If networkCredentials IsNot Nothing Then
87                client.Credentials = networkCredentials
88            End If
89
90            If showUI Then
91#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper
92                Dim progress As MyProgressDialog
93                progress = New MyProgressDialog(client, String.Format("Downloading '{0}'...", address.ToString))
94
95                client.DownloadFileAsync(address, destinationFileName)
96
97                If progress.ShowDialog() = Windows.Forms.DialogResult.Cancel AndAlso onUserCancel = UICancelOption.ThrowException Then
98                    Throw New OperationCanceledException("The operation was canceled.")
99                End If
100#End If
101            Else
102                client.DownloadFile(address, destinationFileName)
103            End If
104        End Sub
105
106        Public Sub DownloadFile(ByVal address As Uri, ByVal destinationFileName As String, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal overwrite As Boolean)
107            DownloadFile(address, destinationFileName, userName, password, showUI, connectionTimeout, overwrite, UICancelOption.ThrowException)
108        End Sub
109
110        Public Sub DownloadFile(ByVal address As String, ByVal destinationFileName As String, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal overwrite As Boolean, ByVal onUserCancel As UICancelOption)
111            Dim credentials As NetworkCredential
112            Dim uri As Uri
113
114            credentials = New NetworkCredential(userName, password)
115            uri = New Uri(address)
116
117            DownloadFile(uri, destinationFileName, credentials, showUI, connectionTimeout, overwrite, onUserCancel)
118        End Sub
119
120        Public Sub DownloadFile(ByVal address As Uri, ByVal destinationFileName As String, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal overwrite As Boolean, ByVal onUserCancel As UICancelOption)
121            Dim credentials As NetworkCredential
122            credentials = New NetworkCredential(userName, password)
123            DownloadFile(address, destinationFileName, credentials, showUI, connectionTimeout, overwrite, onUserCancel)
124        End Sub
125
126        <MonoNotSupported("")> _
127        Public Function Ping(ByVal hostNameOrAddress As String) As Boolean
128            Return Ping(hostNameOrAddress, 500)
129        End Function
130
131        <MonoNotSupported("")> _
132        Public Function Ping(ByVal address As Uri) As Boolean
133            Return Ping(address.Host, 500)
134        End Function
135
136        ' Not supported, since uses unsupported System.Net.NetworkInformation.Ping.Send()
137        <MonoNotSupported("")> _
138        Public Function Ping(ByVal hostNameOrAddress As String, ByVal timeout As Integer) As Boolean
139#If TARGET_JVM = True Then
140			Throw New NotSupportedException()
141#Else
142            If IsAvailable = False Then
143                Throw New InvalidOperationException("Network not available.")
144            End If
145
146            Dim worker As Ping
147            Dim result As PingReply
148
149            worker = New Ping()
150            result = worker.Send(hostNameOrAddress, timeout)
151
152            Return result.Status = IPStatus.Success
153#End If
154        End Function
155
156        <MonoNotSupported("")> _
157        Public Function Ping(ByVal address As Uri, ByVal timeout As Integer) As Boolean
158            Return Ping(address.Host, timeout)
159        End Function
160
161        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As String)
162            UploadFile(sourceFileName, address, String.Empty, String.Empty, False, DEFAULTTIMEOUT, UICancelOption.ThrowException)
163        End Sub
164
165        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As Uri)
166            UploadFile(sourceFileName, address, String.Empty, String.Empty, False, DEFAULTTIMEOUT, UICancelOption.ThrowException)
167        End Sub
168
169        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As String, ByVal userName As String, ByVal password As String)
170            UploadFile(sourceFileName, address, userName, password, False, DEFAULTTIMEOUT, UICancelOption.ThrowException)
171        End Sub
172
173        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As Uri, ByVal userName As String, ByVal password As String)
174            UploadFile(sourceFileName, address, userName, password)
175        End Sub
176
177        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As Uri, ByVal networkCredentials As ICredentials, ByVal showUI As Boolean, ByVal connectionTimeout As Integer)
178            UploadFile(sourceFileName, address, networkCredentials, showUI, connectionTimeout, UICancelOption.ThrowException)
179        End Sub
180
181        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As String, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer)
182            UploadFile(sourceFileName, address, userName, password, showUI, connectionTimeout, UICancelOption.ThrowException)
183        End Sub
184
185        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As Uri, ByVal networkCredentials As ICredentials, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal onUserCancel As UICancelOption)
186            Dim client As WebClient
187
188            If connectionTimeout < 0 Then
189                Throw New ArgumentOutOfRangeException("connectionTimeout")
190            End If
191
192            client = New MyWebClient(connectionTimeout)
193
194            If networkCredentials IsNot Nothing Then
195                client.Credentials = networkCredentials
196            End If
197
198            If showUI Then
199#If TARGET_JVM = False Then 'Windows.Forms Not Supported by Grasshopper
200                Dim progress As MyProgressDialog
201                progress = New MyProgressDialog(client, String.Format("Uploading '{0}'...", address.ToString))
202
203                client.UploadFileAsync(address, sourceFileName)
204
205                If progress.ShowDialog() = Windows.Forms.DialogResult.Cancel AndAlso onUserCancel = UICancelOption.ThrowException Then
206                    Throw New OperationCanceledException("The operation was canceled.")
207                End If
208#End If
209            Else
210                client.UploadFile(address, sourceFileName)
211            End If
212        End Sub
213
214        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As Uri, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer)
215            UploadFile(sourceFileName, address, userName, password, showUI, connectionTimeout, UICancelOption.ThrowException)
216        End Sub
217
218        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As String, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal onUserCancel As UICancelOption)
219            Dim credentials As NetworkCredential
220            Dim uri As Uri
221
222            credentials = New NetworkCredential(userName, password)
223            uri = New Uri(address)
224
225            UploadFile(sourceFileName, uri, credentials, showUI, connectionTimeout, onUserCancel)
226        End Sub
227
228        Public Sub UploadFile(ByVal sourceFileName As String, ByVal address As Uri, ByVal userName As String, ByVal password As String, ByVal showUI As Boolean, ByVal connectionTimeout As Integer, ByVal onUserCancel As UICancelOption)
229            Dim credentials As NetworkCredential
230
231            credentials = New NetworkCredential(userName, password)
232
233            UploadFile(sourceFileName, address, credentials, showUI, connectionTimeout, onUserCancel)
234        End Sub
235
236        Public ReadOnly Property IsAvailable() As Boolean
237            Get
238#If TARGET_JVM = True Then
239				Throw New NotSupportedException()
240#Else
241                Return NetworkInterface.GetIsNetworkAvailable()
242#End If
243            End Get
244        End Property
245    End Class
246End Namespace
247