#Region Project Attributes
#ApplicationLabel: Server
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private server As ServerSocket
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim connections As Map
Dim Imageinfo1, Imageinfo2, Imageinfo3, Imageinfo4, Imageinfo5, Imageinfo6 As String
Dim computer1, computer2, computer3, computer4, computer5, computer6 As String
Dim slots As List
Dim Panel1 As Panel
Dim Canvas1 As Canvas
Dim DestRect As Rect
Dim n As Int
Dim nz As Int
Dim v_nbr As Int
Dim nString As String
Dim sf As StringFunctions
Type LabelAndImageView (lbl As String, iv As String, connected As Boolean)
End Sub
Sub Activity_Create(FirstTime As Boolean)
n=0
nz=0
nString=" "
slots.Initialize
connections.Initialize
computer1 = "0"
computer2 = "0"
computer3 = "0"
computer4 = "0"
computer5 = "0"
computer6 = "0"
slots.Add(CreateSlot(computer1, Imageinfo1))
slots.Add(CreateSlot(computer2, Imageinfo2))
slots.Add(CreateSlot(computer3, Imageinfo3))
slots.Add(CreateSlot(computer4, Imageinfo4))
slots.Add(CreateSlot(computer5, Imageinfo5))
slots.Add(CreateSlot(computer6, Imageinfo6))
server.Initialize(17178, "server")
server.Listen
DestRect.Initialize(109, 0, 1279, 517)
Panel1.Initialize("Panel1")
Activity.AddView(Panel1,0,0, Activity.Width, Activity.Height)
Canvas1.Initialize(Panel1)
Canvas1.DrawText("My IP: " & server.GetMyIP,30dip,30dip, Typeface.DEFAULT,20,Colors.white,"LEFT")
Panel1.Invalidate
Msgbox("ready","Alert")
End Sub
Sub Activity_Resume
server.Listen
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Log("paused")
End Sub
Private Sub CreateSlot(lbl As String, iv As String) As LabelAndImageView
Dim liiv As LabelAndImageView
liiv.Initialize
liiv.iv = iv
liiv.lbl = lbl
liiv.connected = False
Return liiv
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
nz=nz+1
nString = NumberFormat(nz, 0, 0)&" Connection"
If Successful Then
Dim Astream As AsyncStreams
Astream.InitializePrefix(NewSocket.InputStream, False, NewSocket.OutputStream, "Astream")
For Each li As LabelAndImageView In slots 'There are now 6 slots
If li.connected = False Then
li.connected = True
connections.Put(Astream, li) 'put info in map
'Log(li)
Canvas1.DrawText("██████████████████████████████████████████████████████████████████",20dip,50dip, Typeface.DEFAULT,20,Colors.black,"LEFT")
Canvas1.DrawText(Astream,20dip,50dip, Typeface.DEFAULT,20,Colors.white,"LEFT")
Panel1.Invalidate
Exit
End If
Next
Else
Log(LastException)
Canvas1.DrawText("████████████████████████",20dip,50dip, Typeface.DEFAULT,20,Colors.black,"LEFT")
Canvas1.DrawText("Status: Connect Failed",20dip,50dip, Typeface.DEFAULT,20,Colors.white,"LEFT")
Panel1.Invalidate
End If
server.Listen
End Sub
Sub Astream_NewData (Buffer() As Byte)
n=n+1
nString=sf.Right(sf.left(connections.Get(Sender),6),1)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
If nString<> sf.Right(sf.Left(msg,10),1) Then
n=1
For Each li As LabelAndImageView In slots
If li.lbl=nString Then
li.lbl=sf.Right(sf.Left(msg,10),1)
Select True
Case n=1
computer1=sf.Right(sf.Left(msg,10),1)
Case n=2
computer2=sf.Right(sf.Left(msg,10),1)
Case n=3
computer3=sf.Right(sf.Left(msg,10),1)
Case n=4
computer4=sf.Right(sf.Left(msg,10),1)
Case n=5
computer5=sf.Right(sf.Left(msg,10),1)
Case n=6
computer6=sf.Right(sf.Left(msg,10),1)
End Select
Exit
End If
n=n+1
Next
End If
Canvas1.DrawText("██████████████████████████████████████████████████████████████████",20dip,70dip, Typeface.DEFAULT,20,Colors.black,"LEFT")
Canvas1.DrawText(nString&" "&msg,20dip,70dip, Typeface.DEFAULT,20,Colors.white,"LEFT")
Panel1.Invalidate
End Sub
Sub Send1(OutStr As String)
Dim buffer() As Byte
buffer = OutStr.GetBytes("UTF8")
Dim Astream As AsyncStreams = connections.GetKeyAt(sf.Val(computer2)-1) 'subtract 1 because it starts with 0
Astream.Write(buffer)
End Sub
Sub Send2(OutStr As String)
Dim buffer() As Byte
buffer = OutStr.GetBytes("UTF8")
Dim Astream As AsyncStreams = connections.GetKeyAt(sf.Val(computer1)-1) 'subtract 1 because it starts with 0
Astream.Write(buffer)
End Sub
Sub Send3(OutStr As String)
Dim buffer() As Byte
buffer = OutStr.GetBytes("UTF8")
Dim Astream As AsyncStreams = connections.GetKeyAt(sf.Val(computer3)-1) 'subtract 1 because it starts with 0
Astream.Write(buffer)
End Sub
' Add additional subs for up to 6 computers
Sub Astream_Error
Log("Error: " & LastException)
Dim Astream As AsyncStreams = Sender
ReleaseSlot(Astream)
Astream.Close
End Sub
Private Sub ReleaseSlot(Astream As AsyncStreams)
If connections.ContainsKey(Astream) Then
Dim liiv As LabelAndImageView = connections.Get(Astream)
liiv.connected = False
Canvas1.DrawText("████████████████████████",20dip,50dip, Typeface.DEFAULT,20,Colors.black,"LEFT")
Canvas1.DrawText("Status: Disconnected",20dip,50dip, Typeface.DEFAULT,20,Colors.white,"LEFT")
Panel1.Invalidate
connections.Remove(Astream)
End If
End Sub
Sub Astream_Terminated
Dim Astream As AsyncStreams = Sender
ReleaseSlot(Astream)
Astream.Close
End Sub
Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
Select True
Case Action = 0
v_nbr = X
Case Action = 2
Case Action = 1
Select True
Case v_nbr< (X-60)
Send1("Right")
Case v_nbr> (X+60)
Send2("Left")
End Select
End Select
End Sub