Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private Server As ServerSocket
Type LabelAndString (lbl As Label, val As String, connected As Boolean)
Dim connections As Map
Dim slots As List
Dim lblIP As Label
Dim lblStatus1, lblStatus2 As Label
Dim Valore1, Valore2 As String
Dim ListView1 As ListView
Private lblVersione As Label
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
slots.Initialize
connections.Initialize
slots.Add(CreateLstSlot(lblStatus1, Valore1))
slots.Add(CreateLstSlot(lblStatus2, Valore2))
Server.Initialize(51042, "Server")
Server.Listen
lblIP.Text = Server.GetMyIP
End Sub
'***************************************************************************
Private Sub CreateLstSlot(lbl As Label, val As String) As LabelAndString
Log(GetType(lbl))
Dim tLS As LabelAndString
tLS.Initialize
tLS.lbl = lbl
tLS.val = val
tLS.connected = False
Return tLS
End Sub
Private Sub ReleaseLstSlot(astream As AsyncStreams)
If connections.ContainsKey(astream) Then
Dim tLS As LabelAndString = connections.Get(astream)
tLS.connected = False
tLS.lbl.Text = "Status: Disconnected"
connections.Remove(astream)
End If
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim astream As AsyncStreams
astream.InitializePrefix(NewSocket.InputStream, False, NewSocket.OutputStream, "astream")
For Each LS As LabelAndString In slots
If LS.connected = False Then
LS.connected = True
LS.lbl.Text = "Status: Connected"
connections.Put(astream, LS)
Exit
End If
Next
Else
Log(LastException)
End If
Server.Listen
End Sub
'***************************************************************************
Sub astream_NewData (Buffer() As Byte)
Log("received: " & DateTime.GetSecond(DateTime.Now))
Dim tLS As LabelAndString = connections.Get(Sender)
Log(tLS.lbl)
Log(tLS.val)
ListView1.Items.Add(tLS.val)
End Sub
Sub astream_Error
Log("Error: " & LastException)
Dim astream As AsyncStreams = Sender
ReleaseLstSlot(astream)
astream.Close
End Sub
Sub astream_Terminated
Log(Sender)
Dim astream As AsyncStreams = Sender
ReleaseLstSlot(astream)
astream.Close
End Sub