// BroadcastListener *fixed* to support the new type array style.
// With the help of TomToad(again) ;-)
type NetworkNameType
name$ // the name of the network detected (this is used to connect to the network using the JoinNetwork command)
lastBroadcast# // this will store the time that the network was last heard from, allowing us to prune dead networks
endtype
// create a global array of networks detected
// the aim is to display this list to the screen
global networkListSize as integer
networkListSize = 0
//dim networkNames[0] as NetworkName // obsolete.
//dim networkNames[networkListSize] as NetworkName // suggested by TomToad, which is the definition for compatibility.
//networkNames as NetworkName[networkListSize] // declaration array new style<< not working , for arraysize must be constant or literal integer.
//global networkNames as NetworkNameType[0] // declaration array new style set to global as by default arrays are set to local now, hence the previous error.
networkNames as NetworkNameType[0] // declaration array new style,
// create a broadcast listener to listen for agk networks
// agk networks broadcast their existance intermittently on port 45631
global broadcastListener
broadcastListener = CreateBroadcastListener(45631)
// create a string that will contain the list of networks
// as none have been found, the default value is the no networks found message
global networkListText as string
networkListText = "No Networks Found."
do
updateNetworkList(networkNames) // update the list of networks stored in networkNames
Print(networkListText) // print the updated list of networks to the screen
Sync() // refresh the screen
loop
function updateNetworkList(networkNames ref as networkNameType[] ) // passed networkNames by reference to circumvent the array not being global.
updates = 0 // assume that there have been no updates this cycle
time# = Timer() // save the current time
// get a message (if any) received by the broadcast listener
incomingMessage = GetBroadcastMessage(broadcastListener)
// while there are still messages to process (GetBroadcastMessage will return 0 if there are no more messages)
while (incomingMessage > 0)
// read a string from the message - this will be the name of the agk network
networkName$ = GetNetworkMessageString(incomingMessage)
// find out if the network that sent the message is already in our list
alreadyInList = 0
for c = 1 to networkListSize
// if the network already is in our list
if networkNames[c].name$ = networkName$ // main.agc:47: error: "networknames" has not been defined as an array
// update its time of detection so that we know that it is still active
networkNames[c].lastBroadcast# = time#
alreadyInList = 1
exit
endif
next c
// if the network that sent the message is new (because it does not appear in our list)
if alreadyInList = 0
updates = 1 // record that there have been updates this cycle, meaning that the text will need to be updated later
// add the new network to our array and save its time of detection
inc networkListSize
//dim networkNames[networkListSize] // obsolete
networkNames.length = networkListSize // change size of the array new style..
networkNames[networkListSize].name$ = networkName$
networkNames[networkListSize].lastBroadcast# = time#
endif
// delete the message we were processing and move on to the next message
DeleteNetworkMessage(incomingMessage)
incomingMessage = GetBroadcastMessage(broadcastListener)
endwhile
// now that we have processed the broadcast messages, we need to check if any old networks have become inactive
// therefore we cycle through all the network in our list
for c = 1 to networkListSize
// if it has been over 2 seconds since we last heard from a network, we can assume that it has become inactive
if time# - networkNames[c].lastBroadcast# > 2.0
// remove the network from the list
for d = c to networkListSize - 1
networkNames[d].name$ = networkNames[d + 1].name$
networkNames[d].lastBroadcast# = networkNames[d + 1].lastBroadcast#
next d
dec networkListSize
// dim networkNames[networkListSize] // obsolete
networkNames.length = networkListSize // change size of the array new style..
dec c // move the index back so that we don't miss any network this cycle having just deleted one
updates = 1 // record that there have been updates this cycle, meaning that the text will need to be updated later
endif
next c
// if there were any updates this cycle, we need to regenerate the text listing the networks to be displayed to the screen
if updates
// if any networks have been detected
if networkListSize > 0
// add all the networks to the string as a numbered list
networkListText = ""
for c = 1 to networkListSize
networkListText = networkListText + Str(c) + ". " + networkNames[c].name$ + Chr(10)
next c
// if no networks have been detected
else
// reset the message to the default no networks found message
networkListText = "No Networks Found."
endif
endif
endfunction
type NetworkName name$ // the name of the network detected (this is used to connect to the network using the JoinNetwork command) lastBroadcast# // this will store the time that the network was last heard from, allowing us to prune dead networks endtype // create a global array of networks detected // the aim is to display this list to the screen global networkListSize as integer dim networkNames[0] as NetworkName networkListSize = 0 // create a broadcast listener to listen for agk networks // agk networks broadcast their existance intermittently on port 45631 global broadcastListener broadcastListener = CreateBroadcastListener(45631) // create a string that will contain the list of networks // as none have been found, the default value is the no networks found message global networkListText as string networkListText = "No Networks Found." do updateNetworkList() // update the list of networks stored in networkNames Print(networkListText) // print the updated list of networks to the screen Sync() // refresh the screen loop function updateNetworkList() updates = 0 // assume that there have been no updates this cycle time# = Timer() // save the current time // get a message (if any) received by the broadcast listener incomingMessage = GetBroadcastMessage(broadcastListener) // while there are still messages to process (GetBroadcastMessage will return 0 if there are no more messages) while (incomingMessage > 0) // read a string from the message - this will be the name of the agk network networkName$ = GetNetworkMessageString(incomingMessage) // find out if the network that sent the message is already in our list alreadyInList = 0 for c = 1 to networkListSize // if the network already is in our list if networkNames[c].name$ = networkName$ // update its time of detection so that we know that it is still active networkNames[c].lastBroadcast# = time# alreadyInList = 1 exit endif next c // if the network that sent the message is new (because it does not appear in our list) if alreadyInList = 0 updates = 1 // record that there have been updates this cycle, meaning that the text will need to be updated later // add the new network to our array and save its time of detection inc networkListSize dim networkNames[networkListSize] networkNames[networkListSize].name$ = networkName$ networkNames[networkListSize].lastBroadcast# = time# endif // delete the message we were processing and move on to the next message DeleteNetworkMessage(incomingMessage) incomingMessage = GetBroadcastMessage(broadcastListener) endwhile // now that we have processed the broadcast messages, we need to check if any old networks have become inactive // therefore we cycle through all the network in our list for c = 1 to networkListSize // if it has been over 2 seconds since we last heard from a network, we can assume that it has become inactive if time# - networkNames[c].lastBroadcast# > 2.0 // remove the network from the list for d = c to networkListSize - 1 networkNames[d].name$ = networkNames[d + 1].name$ networkNames[d].lastBroadcast# = networkNames[d + 1].lastBroadcast# next d dec networkListSize dim networkNames[networkListSize] dec c // move the index back so that we don't miss any network this cycle having just deleted one updates = 1 // record that there have been updates this cycle, meaning that the text will need to be updated later endif next c // if there were any updates this cycle, we need to regenerate the text listing the networks to be displayed to the screen if updates // if any networks have been detected if networkListSize > 0 // add all the networks to the string as a numbered list networkListText = "" for c = 1 to networkListSize networkListText = networkListText + Str(c) + ". " + networkNames[c].name$ + Chr(10) next c // if no networks have been detected else // reset the message to the default no networks found message networkListText = "No Networks Found." endif endif endfunction