root/lu/SocketRunLoop.lua

Revision 814 (checked in by rsz, 3 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               SocketRunLoop.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       February 1, 2005
6 -- Legal:               Copyright (C) 2005 Raphaël Szwarc
7 --------------------------------------------------------------------------------
8
9 -- import dependencies
10 local LURunLoop = require( "LURunLoop" )
11 local LUMap = require( "LUMap" )
12 local SocketTask = require( "SocketTask" )
13 local socket = require( "socket" )
14
15 -- define the class
16 local super = LURunLoop
17 local self = super()
18
19 -- constant(s)
20 local Timeout = 0.1
21
22  -- method to add a task to this run loop
23 function self:addTask( aTask )
24         super.addTask( self, aTask )
25        
26         self:runTask( aTask )
27        
28         return self
29 end
30
31 function self:sockets()
32         if self._sockets == nil then
33                 self._sockets = LUMap( nil, "kv" )
34         end
35        
36         return self._sockets
37 end
38
39 function self:readers()
40         if self._readers == nil then
41                 self._readers = LUMap( nil, "kv" )
42         end
43        
44         return self._readers
45 end
46
47 function self:writers()
48         if self._writers == nil then
49                 self._writers = LUMap( nil, "kv" )
50         end
51        
52         return self._writers
53 end
54
55 -- method to access this run loop selected tasks
56 function self:selectedTasks()
57         local someTasks = LUMap()
58         local someSockets = self:sockets()
59         local someReaders = self:readers():values():content()
60         local someWriters = self:writers():values():content()
61        
62         someReaders, someWriters = socket.select( someReaders, someWriters, Timeout )
63        
64         for _, aSocket in ipairs( someReaders ) do
65                 local aTask = someSockets:get( aSocket )
66                
67                 someTasks:put( aTask, aSocket )
68         end
69        
70         for _, aSocket in ipairs( someWriters ) do
71                 local aTask = someSockets:get( aSocket )
72                
73                 someTasks:put( aTask, aSocket )
74         end
75        
76         return someTasks
77 end
78
79 -- method to 'run' the given task
80 function self:runTask( aTask )
81         local someTasks = self:tasks()
82         local aStatus = aTask:status()
83        
84         if aStatus == "dead" then
85                 someTasks:remove( aTask )
86         elseif aStatus == "suspended" then
87                 local aStatus, aType = aTask:resume()
88
89                 if aStatus == true then
90                         local aSocket = aTask:target():socket()
91
92                         self:sockets():put( aSocket, aTask )
93                        
94                         if aType == "reader" then
95                                 self:readers():put( aTask, aSocket )
96                         else
97                                 self:writers():put( aTask, aSocket )
98                         end
99                 else
100                         someTasks:remove( aTask )
101                 end
102         end
103        
104         return self
105 end
106
107 -- method to 'run' through all this run loop tasks
108 function self:run()
109         for aTask, _ in self:selectedTasks():iterator() do
110                 self:runTask( aTask )
111         end
112
113         for aTask, _ in self:tasks():iterator() do
114                 self:runTask( aTask )
115         end
116
117         return self
118 end
119
120 return self
Note: See TracBrowser for help on using the browser.