MIKhanik

SHCS.lua

Oct 28th, 2015
125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component=require('component')
  2. local event=require('event')
  3. local modem=component.modem
  4.  
  5. SHCS={}
  6.  
  7. PORT=111
  8. services={}
  9. --Расскоментить для сервиса doorControl
  10. --services.doorControl=require('SHCS-doors')
  11.  
  12. function SHCS.registerService(dnsadr,service)
  13.   modem.send(dnsadr,PORT,'addService',service.type,service.name)
  14.   local i=0
  15.   while i<10 do
  16.     resp=table.pack(event.pull(5,'modem_message'))
  17.     if resp['n']==0 then
  18.       return false, 'timeout'
  19.     elseif resp[6]=='registered' then
  20.       return true
  21.     else
  22.       i=i+1
  23.     end
  24.   end
  25.   return false, 'too many spam'
  26. end
  27.  
  28. function SHCS.startService(service)
  29.   service:pre_start()
  30.   event.listen('modem_message',service.msgHandler)
  31. end
  32.  
  33. function SHCS.searchDns()
  34.   modem.broadcast(PORT,'searchDns')
  35.   local i=0
  36.   while i<10 do
  37.     resp=table.pack(event.pull(5,'modem_message'))
  38.     if resp['n']==0 then
  39.       return false, 'timeout'
  40.     elseif resp[6]=='dnsAddress' then
  41.       return resp[7]
  42.     else
  43.       i=i+1
  44.     end
  45.   end
  46.   return false, 'too many spam'
  47. end
  48.  
  49. modem.open(PORT)
  50.  
  51. SHCS['services']=services
  52.  
  53. return SHCS
RAW Paste Data