ISelector

A selector is a multiplexor for I/O events associated to a Conduit. All selectors must implement this interface.

A selector needs to be initialized by calling the open() method to pass it the initial amount of conduits that it will handle and the maximum amount of events that will be returned per call to select(). In both cases, these values are only hints and may not even be used by the specific ISelector implementation you choose to use, so you cannot make any assumptions regarding what results from the call to select() (i.e. you may receive more or less events per call to select() than what was passed in the 'maxEvents' argument. The amount of conduits that the selector can manage will be incremented dynamically if necessary.

To add or modify conduit registrations in the selector, use the register() method. To remove conduit registrations from the selector, use the unregister() method.

To wait for events from the conduits you need to call any of the select() methods. The selector cannot be modified from another thread while blocking on a call to these methods.

Once the selector is no longer used you must call the close() method so that the selector can free any resources it may have allocated in the call to open().

Members

Functions

close
void close()

Free any operating system resources that may have been allocated in the call to open().

key
SelectionKey key(ISelectable conduit)

Return the selection key resulting from the registration of a conduit to the selector.

opApply
int opApply(int delegate(ref SelectionKey sk) dg)

Iterate through the currently registered selection keys. Note that you should not erase or add any items from the selector while iterating, although you can register existing conduits again.

open
void open(uint size, uint maxEvents)

Initialize the selector.

register
void register(ISelectable conduit, Event events, Object attachment)

Associate a conduit to the selector and track specific I/O events. If the conduit is already part of the selector, modify the events or atachment.

reregister
deprecated void reregister(ISelectable conduit, Event events, Object attachment)

Deprecated, use register instead

select
int select()

Wait indefinitely for I/O events from the registered conduits.

select
int select(TimeSpan timeout)

Wait for I/O events from the registered conduits for a specified amount of time.

select
int select(double timeout)

Wait for I/O events from the registered conduits for a specified amount of time.

selectedSet
ISelectionSet selectedSet()

Return the selection set resulting from the call to any of the select() methods.

unregister
void unregister(ISelectable conduit)

Remove a conduit from the selector.

Examples

1 import tango.io.selector.model.ISelector;
2 import tango.io.SocketConduit;
3 import tango.io.Stdout;
4 
5 ISelector selector;
6 SocketConduit conduit1;
7 SocketConduit conduit2;
8 MyClass object1;
9 MyClass object2;
10 int eventCount;
11 
12 // Initialize the selector assuming that it will deal with 2 conduits and
13 // will receive 2 events per invocation to the select() method.
14 selector.open(2, 2);
15 
16 selector.register(conduit, Event.Read, object1);
17 selector.register(conduit, Event.Write, object2);
18 
19 eventCount = selector.select();
20 
21 if (eventCount > 0)
22 {
23     char[16] buffer;
24     int count;
25 
26     foreach (SelectionKey key, selector.selectedSet())
27     {
28         if (key.isReadable())
29         {
30             count = (cast(SocketConduit) key.conduit).read(buffer);
31             if (count != IConduit.Eof)
32             {
33                 Stdout.format("Received '{0}' from peer\n", buffer[0..count]);
34                 selector.register(key.conduit, Event.Write, key.attachment);
35             }
36             else
37             {
38                 selector.unregister(key.conduit);
39                 key.conduit.close();
40             }
41         }
42 
43         if (key.isWritable())
44         {
45             count = (cast(SocketConduit) key.conduit).write("MESSAGE");
46             if (count != IConduit.Eof)
47             {
48                 Stdout.print("Sent 'MESSAGE' to peer\n");
49                 selector.register(key.conduit, Event.Read, key.attachment);
50             }
51             else
52             {
53                 selector.unregister(key.conduit);
54                 key.conduit.close();
55             }
56         }
57 
58         if (key.isError() || key.isHangup() || key.isInvalidHandle())
59         {
60             selector.unregister(key.conduit);
61             key.conduit.close();
62         }
63     }
64 }
65 
66 selector.close();

Meta