Advertisement
bueddl

Untitled

Jun 10th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. -- event system
  2.  
  3. -- class 'event'
  4. events = {
  5.     eventList = {},
  6.     callbacks = {}
  7.    
  8. }
  9. events.__index = events
  10.  
  11. -- register an event type
  12. events.register = function(event)
  13.     table.insert(events.eventList, event)
  14. end
  15.  
  16. -- subscribe to an event and provide an callback function to be called
  17. events.subscribe = function(event, callbackFunc)
  18.     local eventId = events.getId(event)
  19.  
  20.     table.insert(events.callbacks[eventId], callbackFunc)
  21. end
  22.  
  23. -- get eventId by name
  24. events.getId = function(event)
  25.     for id, eventName in pairs(events.eventList) do
  26.         if type(eventName) == 'string' and
  27.             eventName == event then
  28.             return id
  29.         end
  30.     end
  31. end
  32.  
  33. -- fire an event, invokes all subscribed callback functions
  34. events.fire = function(event, param1, param2, param3, param4, param5)
  35.     local eventId = events.getId(event)
  36.  
  37.     if type(events.callbacks[eventId]) == 'table' then
  38.         for _,callbackFunc in pairs(event.callbacks[eventId]) do
  39.             if param5 ~= nil then
  40.                 callbackFunc(param1, param2, param3, param4, param5)
  41.             elseif param4 ~= nil then
  42.                 callbackFunc(param1, param2, param3, param4)
  43.             elseif param3 ~= nil then
  44.                 callbackFunc(param1, param2, param3)
  45.             elseif param2 ~= nil then
  46.                 callbackFunc(param1, param2)
  47.             elseif param1 ~= nil then
  48.                 callbackFunc(param1)
  49.             else
  50.                 callbackFunc()
  51.             end
  52.         end
  53.     end
  54. end
  55.  
  56. -- queue an event
  57. events.queue = function(event, param1, param2, param3, param4, param5)
  58.     os.queueEvent(event, param1, param2, param3, param4, param5)
  59. end
  60.  
  61. -- event queue
  62. events.main = function ()
  63.     -- stop Condition for main event loop
  64.     local stop = false
  65.  
  66.     -- main event loop
  67.     while not stop do
  68.  
  69.         local event, param1, param2, param3, param4, param5
  70.             = os.pullEvent()
  71.  
  72.         -- fire an catched event
  73.         events.fire(event, param1, param2, param3, param4, param5)
  74.     end
  75. end
  76.  
  77.  
  78.  
  79. -----------------------------------------------------------------------
  80.  
  81.  
  82. network = {
  83.     packetStack = {}
  84. }
  85. network.__index = index
  86.  
  87. -- queue Packet
  88. network.queuePacket = function(senderId, message)
  89.     table.insert(packetStack, {
  90.         sender  = senderId,
  91.         message = message
  92.     })
  93. end
  94.  
  95. -- main loop
  96. network.main = function()
  97.     -- stop Condition for main event loop
  98.     local stop = false
  99.  
  100.     -- main event loop
  101.     while not stop do
  102.  
  103.         local event, senderId, message, distance
  104.             = os.pullEvent('redstone_message')
  105.  
  106.         network.queuePacket(senderId, message)
  107.     end
  108. end
  109.  
  110.  
  111. -----------------------------------------------------------------------
  112.  
  113. -- register all events we want to listen for
  114. events.register('rednet_message')
  115. events.register('timer')
  116. events.register('disk')
  117. events.register('disk_eject')
  118. events.register('peripheral')
  119. events.register('peripheral_detach')
  120. events.register('redstone')
  121. events.register('network_receive')
  122.  
  123. parallell.waitForAll(events.main, network.main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement