File size: 15,464 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
'    DWSIM Flowsheet Solver Server for Microsoft Azure (TM) Service Bus
'    Copyright 2015 Daniel Wagner O. de Medeiros
'
'    This file is part of DWSIM.
'
'    DWSIM is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    DWSIM is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with DWSIM.  If not, see <http://www.gnu.org/licenses/>.

Imports DWSIM
Imports System.IO
Imports System.Threading
Imports System.Threading.Tasks
Imports Microsoft.ServiceBus
Imports Microsoft.ServiceBus.Messaging
Imports System.Linq
Imports System.Reflection
Imports System.Runtime.Serialization.Formatters.Binary

Module AzureServer

    Private nm As NamespaceManager
    Private qcc, qcs As QueueClient
    Private queueNameS As String = "DWSIMserver"
    Private queueNameC As String = "DWSIMclient"

    Private solutions As Dictionary(Of String, Byte())

    Dim ts As CancellationTokenSource

    Sub Main()

        solutions = New Dictionary(Of String, Byte())

        Console.WriteLine()
        Console.WriteLine("DWSIM - Open Source Process Simulator")
        Console.WriteLine("Microsoft Azure (TM) Service Bus Solver Server")
        Console.WriteLine(My.Application.Info.Copyright)
        Dim dt As DateTime = CType("01/01/2000", DateTime).AddDays(My.Application.Info.Version.Build).AddSeconds(My.Application.Info.Version.Revision * 2)
        Console.WriteLine("Version " & My.Application.Info.Version.Major & "." & My.Application.Info.Version.Minor & _
        ", Build " & My.Application.Info.Version.Build & " (" & Format(dt, "dd/MM/yyyy HH:mm") & ")")
        If Type.GetType("Mono.Runtime") Is Nothing Then
            Console.WriteLine("Microsoft .NET Framework Runtime Version " & System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion.ToString())
        Else
            Dim displayName As MethodInfo = Type.GetType("Mono.Runtime").GetMethod("GetDisplayName", BindingFlags.NonPublic Or BindingFlags.[Static])
            If displayName IsNot Nothing Then
                Console.WriteLine("Mono " + displayName.Invoke(Nothing, Nothing) + " / " + System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion.ToString())
            Else
                Console.WriteLine(System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion.ToString())
            End If
        End If
        Console.WriteLine()

        Dim connectionString As String

        If My.Application.CommandLineArgs.Count > 0 Then
            connectionString = My.Application.CommandLineArgs(0)
        Else
            Console.Write("Please enter the Service Bus connection string: ")
            connectionString = Console.ReadLine()
        End If

        Try

            nm = NamespaceManager.CreateFromConnectionString(connectionString)
            If Not nm.QueueExists(queueNameC) Then nm.CreateQueue(queueNameC)
            If Not nm.QueueExists(queueNameS) Then nm.CreateQueue(queueNameS)

            qcc = QueueClient.CreateFromConnectionString(connectionString, queueNameC)
            qcs = QueueClient.CreateFromConnectionString(connectionString, queueNameS)

            Console.WriteLine("[" & Date.Now.ToString & "] " & "Server is clearing messages on queues...")

            While (qcc.Peek() IsNot Nothing)
                Dim brokeredMessage = qcc.Receive(New TimeSpan(0, 0, 0))
                If Not brokeredMessage Is Nothing Then brokeredMessage.Complete()
            End While
            While (qcs.Peek() IsNot Nothing)
                Dim brokeredMessage = qcs.Receive(New TimeSpan(0, 0, 0))
                If Not brokeredMessage Is Nothing Then brokeredMessage.Complete()
            End While

            Console.WriteLine("[" & Date.Now.ToString & "] " & "Cleared ok")

            Console.WriteLine("[" & Date.Now.ToString & "] " & "Server is running and listening to incoming data on queue '" & queueNameC & "'...")

            Dim bytearr As New List(Of Byte())

            Dim icounter As Integer = 100

            While True

                If ts Is Nothing Then ts = New CancellationTokenSource
                Dim ct As CancellationToken = ts.Token

                Thread.Sleep(1000)

                Dim message As BrokeredMessage

                message = qcc.Receive(New TimeSpan(0, 0, 0))

                If Not message Is Nothing Then

                    If message.Properties("origin") = "client" Then

                        Dim requestID As String = message.Properties("requestID")

                        Try

                            If message.Properties("type") = "abort" Then

                                ts.Cancel()

                            ElseIf message.Properties("type") = "data" Then

                                Dim i, n As Integer

                                Dim bytes As Byte() = message.GetBody(Of Byte())()

                                message.Complete()

                                If message.Properties("multipart") = False Then

                                    i = 0
                                    n = 0

                                Else

                                    i = message.Properties("partnumber")
                                    n = message.Properties("totalparts")

                                    If i = 1 Then
                                        bytearr.Clear()
                                        bytearr.Add(bytes)
                                    ElseIf i > 1 And i < n Then
                                        bytearr.Add(bytes)
                                    Else
                                        bytearr.Add(bytes)
                                        bytes = Combine(bytearr.ToArray)
                                    End If

                                End If

                                If i = n Then

                                    Console.WriteLine("[" & Date.Now.ToString & "] Data received with Request ID = " & requestID & ", flowsheet solving started!")
                                    Dim msg As New BrokeredMessage("Data received, flowsheet solving started!")
                                    msg.Properties.Add("requestID", requestID)
                                    msg.Properties.Add("type", "text")
                                    msg.Properties.Add("origin", "server")
                                    qcs.Send(msg)
                                    Task.Factory.StartNew(Sub()
                                                              ProcessData(bytes, requestID)
                                                          End Sub,
                                                          TaskCreationOptions.LongRunning,
                                                          ct).ContinueWith(Sub(t)
                                                                               If Not t Is Nothing Then
                                                                                   If Not t.Exception Is Nothing Then
                                                                                       Console.WriteLine("[" & Date.Now.ToString & "] " & t.Exception.Flatten().ToString)
                                                                                       msg = New BrokeredMessage(t.Exception.Flatten().ToString)
                                                                                       msg.Properties.Add("requestID", requestID)
                                                                                       msg.Properties.Add("type", "exception")
                                                                                       msg.Properties.Add("origin", "server")
                                                                                       qcs.Send(msg)
                                                                                   ElseIf t.IsCanceled Then
                                                                                       Console.WriteLine("[" & Date.Now.ToString & "] " & "Calculation aborted.")
                                                                                       msg = New BrokeredMessage("Calculation aborted.")
                                                                                       msg.Properties.Add("requestID", requestID)
                                                                                       msg.Properties.Add("type", "text")
                                                                                       msg.Properties.Add("origin", "server")
                                                                                       qcs.Send(msg)
                                                                                   End If
                                                                               End If
                                                                           End Sub,
                                                                           TaskContinuationOptions.OnlyOnFaulted).ContinueWith(Sub()
                                                                                                                                   ts.Dispose()
                                                                                                                                   ts = Nothing
                                                                                                                               End Sub)

                                End If

                            ElseIf message.Properties("type") = "connectioncheck" Then

                                message.Complete()

                                Dim msg As New BrokeredMessage("Server endpoint '" & My.User.Name & "@" & My.Computer.Name & "' is listening to requests")
                                msg.Properties.Add("requestID", requestID)
                                msg.Properties.Add("type", "connectioncheck")
                                msg.Properties.Add("origin", "server")
                                qcs.Send(msg)

                            End If

                        Catch ex As Exception

                            Dim msg As New BrokeredMessage(ex.ToString)
                            msg.Properties.Add("requestID", requestID)
                            msg.Properties.Add("type", "exception")
                            msg.Properties.Add("origin", "server")
                            qcs.Send(msg)

                        End Try

                    End If

                End If

                icounter += 1

                If Math.IEEERemainder(icounter, 100) = 0 Then

                    Dim binFormatter = New BinaryFormatter()
                    Dim mStream = New MemoryStream()
                    binFormatter.Serialize(mStream, solutions)

                    If mStream.Length > 100 * 1024 * 1024 Then
                        solutions.Clear()
                    End If

                End If

            End While

            qcc.Close()
            qcs.Close()

        Catch ex As Exception

            Console.WriteLine("[" & Date.Now.ToString & "] " & ex.ToString)

        End Try

    End Sub

    Sub ProcessData(bytes As Byte(), requestID As String)
        Using bytestream As New MemoryStream(bytes)
            Using form As FormFlowsheet = DWSIM.UnitOperations.UnitOperations.Flowsheet.InitializeFlowsheet(bytestream, New FormFlowsheet)
                If Not solutions.ContainsKey(form.Options.Key) Then
                    FlowsheetSolver.FlowsheetSolver.SolveFlowsheet(form, 1, ts)
                    Using retbytes As MemoryStream = DWSIM.UnitOperations.UnitOperations.Flowsheet.ReturnProcessData(form)
                        Dim uncompressedbytes As Byte() = retbytes.ToArray
                        Using compressedstream As New MemoryStream()
                            Using gzs As New BufferedStream(New Compression.GZipStream(compressedstream, Compression.CompressionMode.Compress, True), 64 * 1024)
                                gzs.Write(uncompressedbytes, 0, uncompressedbytes.Length)
                                gzs.Close()
                                solutions.Add(form.Options.Key, compressedstream.ToArray)
                            End Using
                        End Using
                    End Using
                End If
                If solutions(form.Options.Key).Length < 220 * 1024 Then
                    Dim msg As New BrokeredMessage(solutions(form.Options.Key))
                    msg.Properties.Add("multipart", False)
                    msg.Properties.Add("requestID", requestID)
                    msg.Properties.Add("origin", "server")
                    msg.Properties.Add("type", "data")
                    qcs.Send(msg)
                    Console.WriteLine("[" & Date.Now.ToString & "] " & "Sent data to the queue: " & solutions(form.Options.Key).Length & " B, Request ID = " & requestID)
                Else
                    Dim i, n As Integer
                    Dim bytearray As ArrayList = Split(solutions(form.Options.Key), 220)
                    n = bytearray.Count
                    i = 1
                    For Each b As Byte() In bytearray
                        Dim msg As New BrokeredMessage(b)
                        msg.Properties.Add("multipart", True)
                        msg.Properties.Add("partnumber", i)
                        msg.Properties.Add("totalparts", n)
                        msg.Properties.Add("type", "data")
                        msg.Properties.Add("requestID", requestID)
                        msg.Properties.Add("origin", "server")
                        qcs.Send(msg)
                        i += 1
                        Console.WriteLine("[" & Date.Now.ToString & "] " & "Sent data to the queue: " & b.Length & " B, Request ID = " & requestID)
                    Next
                End If
            End Using
        End Using
    End Sub

    Private Function Split(filebytes As Byte(), partsizeKB As Integer) As ArrayList

        partsizeKB *= 1000

        Dim pos As Integer = 0
        Dim remaining As Integer

        Dim result As New ArrayList()

        remaining = filebytes.Length - pos

        While remaining > 0

            Dim block As Byte() = New Byte(Math.Min(remaining, partsizeKB) - 1) {}

            Array.Copy(filebytes, pos, block, 0, block.Length)
            result.Add(block)

            pos += block.Length
            remaining = filebytes.Length - pos

        End While

        Return result

    End Function

    Private Function Combine(ParamArray arrays As Byte()()) As Byte()

        Dim rv As Byte() = New Byte(arrays.Sum(Function(a) a.Length) - 1) {}
        Dim offset As Integer = 0
        For Each array As Byte() In arrays
            System.Buffer.BlockCopy(array, 0, rv, offset, array.Length)
            offset += array.Length
        Next

        Return rv

    End Function

End Module