Adrian Nier Code

Back to overview

Lookup bonjour service

Last modification: Thursday, June 11, 2009 10:22 pm

Looks up the IP address and the port for the service of the specified type on the host with the specified name.

Required parameters
Parameter 1 (string)
The service type to lookup (http://www.dns-sd.org/ServiceTypes.html)

Parameter 2 (string)
The computer name to lookup

Parameter 3 (float)
The time in seconds the discovery is allowed to take.

Function Calls

-- Get IP address of the AFP server whose name is "Media Server"
copy lookupBonjourService("afpovertcp", "Media Server", 0.5) to {_address, _port}

-- Get the IP address of the host whose name is "Abulafia" and has Remote Login enabled
copy lookupBonjourService("ssh", "Abulafia", 0.5) to {_address, _port}
 

Implementation

on lookupBonjourService(_serviceType, _computerName, _scantime)
   
   script LookupBonjourService_Extras
      
      on addressAndPortFromLookupResult(_result)
         if _result does not contain "Service can be reached at" then return {false, false}
         
         set _prvDlmt to text item delimiters
         set text item delimiters to "Service can be reached at"
         set _address to text item 2 of _result
         set text item delimiters to ":"
         set _port to text item 2 of _address
         set _address to text item 1 of _address
         set text item delimiters to ASCII character 13
         set _port to text item 1 of _port
         set text item delimiters to _prvDlmt
         
         set _address to stripWhitespace(_address)
         set _port to stripWhitespace(_port)
         
         return {_address, _port}
      end addressAndPortFromLookupResult
      
      on runLookupScript(_serviceType, _computerName, _scantime)
         try
            -- Start the script
            set _script to {"#!/bin/bash"}
            
            -- Use the mDNS tool to discover services
            set end of _script to "/usr/bin/mDNS -L " & quoted form of _computerName & " _" & _serviceType & "._tcp local &"
            
            -- Keep track of the process id for the mDNS tool
            set end of _script to "mDNSpid=$!"
            
            -- Wait a little for mDNS to discover services
            set end of _script to "sleep " & (_scantime as string)
            
            -- Quit the mDNS tool
            set end of _script to "kill -HUP $mDNSpid"
            
            -- Compose the script
            set _script to combineList(_script, ASCII character 10)
            
            -- Run the script
            set _result to do shell script _script
            return result
            -- Get the actual results without the header
            set _results to paragraphs 4 thru -1 of _result
            
            return _results
         on error _eMsg number _eNum
            return {}
         end try
      end runLookupScript
      
      on combineList(_list, _delimiter)
         set _prvDlmt to text item delimiters
         set text item delimiters to _delimiter
         set _list to _list as string
         set text item delimiters to _prvDlmt
         
         return _list
      end combineList
      
      on stripWhitespace(_string)
         
         set _charCount to count of characters of _string
         
         -- Exit early if there's no string to process
         if _charCount is 0 then return ""
         
         -- Determine the first character that is not whitespace
         repeat with _startChar from 1 to _charCount
            if (ASCII number (character _startChar of _string)) > 32 then exit repeat
         end repeat
         
         -- Determine the last characters that is not whitespace
         set _endChar to _charCount
         repeat
            if (ASCII number (character _endChar of _string)) > 32 then exit repeat
            set _endChar to _endChar - 1
            if _endChar is 0 then exit repeat
         end repeat
         
         -- Get the substring
         set _prvDlmt to text item delimiters
         set text item delimiters to ""
         set _string to characters _startChar thru _endChar of _string as string
         set text item delimiters to _prvDlmt
         
         return _string
         
      end stripWhitespace
      
   end script
   
   tell LookupBonjourService_Extras
      set _result to runLookupScript(_serviceType, _computerName, _scantime)
      return addressAndPortFromLookupResult(_result)
   end tell
   
end lookupBonjourService