IOS Streaming Browser 1.0
An IOS streaming browser to stream the display to others or to a projector

HTTPServer Class Reference

#import <HTTPServer.h>

Collaboration diagram for HTTPServer:

Public Member Functions

(NSString *) - documentRoot
(void) - setDocumentRoot:
(Class) - connectionClass
(void) - setConnectionClass:
(NSString *) - interface
(void) - setInterface:
(UInt16) - port
(UInt16) - listeningPort
(void) - setPort:
(NSString *) - domain
(void) - setDomain:
(NSString *) - name
(NSString *) - publishedName
(void) - setName:
(NSString *) - type
(void) - setType:
(void) - republishBonjour
(NSDictionary *) - TXTRecordDictionary
(void) - setTXTRecordDictionary:
(BOOL) - start:
(BOOL) - stop
(BOOL) - isRunning
(void) - addWebSocket:
(NSUInteger) - numberOfHTTPConnections
(NSUInteger) - numberOfWebSocketConnections
(void) - unpublishBonjour
(void) - publishBonjour

Static Public Member Functions

(void) + startBonjourThreadIfNeeded
(void) + performBonjourBlock:waitUntilDone:

Protected Attributes

dispatch_queue_t serverQueue
dispatch_queue_t connectionQueue
GCDAsyncSocketasyncSocket
NSString * documentRoot
Class connectionClass
NSString * interface
UInt16 port
NSNetService * netService
NSString * domain
NSString * type
NSString * name
NSString * publishedName
NSDictionary * txtRecordDictionary
NSMutableArray * connections
NSMutableArray * webSockets
NSLock * connectionsLock
NSLock * webSocketsLock
BOOL isRunning

Detailed Description

Definition at line 21 of file HTTPServer.h.


Member Function Documentation

- (void) addWebSocket: (WebSocket *)  ws

Adds a web socket

Adds a web sockets array

Definition at line 695 of file HTTPServer.m.

                    :(WebSocket *)ws
{
    // Locks the web socker
        [webSocketsLock lock];
    
    // Adds a web socket
        [webSockets addObject:ws];
    
    // Unlocks the web socket
        [webSocketsLock unlock];
}
- (Class) connectionClass

The connection class is the class used to handle incoming HTTP connections.

The default value is [HTTPConnection class]. You can override HTTPConnection, and then set this to [MyHTTPConnection class].

If you change the connectionClass while the server is running, the change will affect future incoming http connections.

- (NSString *) documentRoot

Specifies the document root to serve files from. For example, if you set this to "/Users/<your_username>/Sites", then it will serve files out of the local Sites directory (including subdirectories).

The default value is nil. The default server configuration will not serve any files until this is set.

If you change the documentRoot while the server is running, the change will affect future incoming http connections.

- (NSString *) domain

Bonjour domain for publishing the service. The default value is "local.".

Note: Bonjour publishing requires you set a type.

If you change the domain property after the bonjour service has already been published (server already started), you'll need to invoke the republishBonjour method to update the broadcasted bonjour service.

- (NSString *) interface

Set what interface you'd like the server to listen on. By default this is nil, which causes the server to listen on all available interfaces like en1, wifi etc.

The interface may be specified by name (e.g. "en1" or "lo0") or by IP address (e.g. "192.168.4.34"). You may also use the special strings "localhost" or "loopback" to specify that the socket only accept connections from the local machine.

- (BOOL) isRunning

Whether the server is running

- (UInt16) listeningPort

Gets the listening port

Get the servers listening port by dispatching q block on the serverQueue returns UInt16

Definition at line 306 of file HTTPServer.m.

{
    // Creates a local attribute
        __block UInt16 result;
        
    // Submits a block for synchronous execution on the serverQueue
        dispatch_sync(serverQueue, ^{
        
                if (isRunning)  // check if server is running
        {
            // Get the sockets local port
                        result = [asyncSocket localPort];
            
                }else{ // if server is not running
            
            
                        result = 0;
        }
        
        }); // END OF BLOCK
        
        return result;
}
- (NSString *) name

Bonjour name for publishing the service. The default value is "".

If using an empty string ("") for the service name when registering, the system will automatically use the "Computer Name". Using an empty string will also handle name conflicts by automatically appending a digit to the end of the name.

Note: Bonjour publishing requires you set a type.

If you change the name after the bonjour service has already been published (server already started), you'll need to invoke the republishBonjour method to update the broadcasted bonjour service.

The publishedName method will always return the actual name that was published via the bonjour service. If the service is not running this method returns nil.

- (NSUInteger) numberOfHTTPConnections

Gets the number of HTTP connections

Returns the number of http client connections that are currently connected to the server. returns NSUInteger

Definition at line 715 of file HTTPServer.m.

{
    
        NSUInteger result = 0;
        
    // Locks the connection
        [connectionsLock lock];
    
    // Gets the count of connections to the this server
        result = [connections count];
    
    // Unlocks the connection
        [connectionsLock unlock];
        
        return result;
}
- (NSUInteger) numberOfWebSocketConnections

Gets the number of web socket connections

Returns the number of websocket client connections that are currently connected to the server. returns NSUInteger

Definition at line 736 of file HTTPServer.m.

{
    // Local attribute
        NSUInteger result = 0;
        
    // Locks the web socket
        [webSocketsLock lock];
    
    // Gets the count of web socket connections
        result = [webSockets count];
    
    // Unlocks the web socket
        [webSocketsLock unlock];
        
        return result;
}
+ (void) performBonjourBlock: (dispatch_block_t)  block
waitUntilDone: (BOOL)  waitUntilDone 

Performs a block of code on the bonjour thread param dispatch_block_t param BOOL

- (UInt16) port

The port number to run the HTTP server on.

The default port number is zero, meaning the server will automatically use any available port. This is the recommended port value, as it avoids possible port conflicts with other applications. Technologies such as Bonjour can be used to allow other applications to automatically discover the port number.

Note: As is common on most OS's, you need root privledges to bind to port numbers below 1024.

You can change the port property while the server is running, but it won't affect the running server. To actually change the port the server is listening for connections on you'll need to restart the server.

The listeningPort method will always return the port number the running server is listening for connections on. If the server is not running this method returns 0.

- (void) publishBonjour

Publish as a list of network services

- (NSString *) publishedName

Gets the published name of the server

- (void) republishBonjour

Republishes the service via bonjour if the server is running. If the service was not previously published, this method will publish it (if the server is running).

Definition at line 905 of file HTTPServer.m.

{
        // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
                
        
                [self unpublishBonjour]; 
                [self publishBonjour];
        }); // END OF BLOCK
}
- (void) setConnectionClass: (Class)  value

Sets the connection class

Sets the connection class on the serverQueue param Class

Definition at line 233 of file HTTPServer.m.

                          :(Class)value
{
        // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
        
                connectionClass = value;
        
        }); // END OF BLOCK
}
- (void) setDocumentRoot: (NSString *)  value

Sets the document root

Set the documents root param NSString

Definition at line 181 of file HTTPServer.m.

                       :(NSString *)value
{
        
        // Document root used to be of type NSURL.
        // Add type checking for early warning to developers upgrading from older versions.
        if (value && ![value isKindOfClass:[NSString class]])
        {
                return;
        }
    
        // Creates a local attributes and makes a copy of the document root
        NSString *valueCopy = [value copy];
        
    
    // Submits a block for asynchronous execution on the serverQueue.  This block sets the servers document root
        dispatch_async(serverQueue, ^{
        
                [documentRoot release];
        
                documentRoot = [valueCopy retain];
        
        }); // END OF BLOCK
        
        [valueCopy release];
}
- (void) setDomain: (NSString *)  value

Sets the domain

Set the domain on which to broadcast this service param NSString

Definition at line 369 of file HTTPServer.m.

                 :(NSString *)value
{
        // Copies the value into a local attribute
        NSString *valueCopy = [value copy];
        
    // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{

                [domain release];
                domain = [valueCopy retain];
        
        }); // END OF BLOCK
        
        [valueCopy release];
}
- (void) setInterface: (NSString *)  value

Sets the interface

Set the server interface param NSString

Definition at line 266 of file HTTPServer.m.

                    :(NSString *)value
{
    // copies the value into a local attribute
        NSString *valueCopy = [value copy];
        
    // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
        
                [interface release];
                interface = [valueCopy retain];
        
        }); // END OF BLOCK
        
        [valueCopy release];
}
- (void) setName: (NSString *)  value

Sets the published name of the server

Sets the published name of the server param NSString

Definition at line 448 of file HTTPServer.m.

               :(NSString *)value
{
    // Copies the value into a local attribute
        NSString *valueCopy = [value copy];
        
    // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
        
                [name release];
                name = [valueCopy retain];
        
        }); // END OF BLOCK
        
        [valueCopy release];
}
- (void) setPort: (UInt16)  value

Sets the listening port

Set the servers port param UInt16

Definition at line 334 of file HTTPServer.m.

               :(UInt16)value
{
        // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
        
                port = value;
        
        }); //END OF BLOCK
}
- (void) setTXTRecordDictionary: (NSDictionary *)  value

Sets the TXT record dictionary

Sets the TXT record dictionary param NSDictionary

Definition at line 528 of file HTTPServer.m.

                              :(NSDictionary *)value
{
        // Copies the value into a local attribute
        NSDictionary *valueCopy = [value copy];
        
    
    // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
        
                [txtRecordDictionary release];
                txtRecordDictionary = [valueCopy retain];
                
                // Update the txtRecord of the netService if it has already been published
                if (netService)
                {
            // Create a local attribute for the netService
                        NSNetService *theNetService = netService;
                        NSData *txtRecordData = nil;
            
            // If there is a textRecordDictionary
                        if (txtRecordDictionary)
            {
                // Gets the data from the dictionary
                                txtRecordData = [NSNetService dataFromTXTRecordDictionary:txtRecordDictionary];
                        }
            
            // The prototype of blocks submitted to dispatch queues, which take no arguments and have no return value.
                        dispatch_block_t bonjourBlock = ^{
                
                // Sets the network service text record data
                                [theNetService setTXTRecordData:txtRecordData];
                
                        }; // END OF BLOCK
                        
            // Perform the bonjourBlock and don't wait for it to execute
                        [[self class] performBonjourBlock:bonjourBlock waitUntilDone:NO];
                }
        }); // END OF BLOCK
        
        [valueCopy release];
}
- (void) setType: (NSString *)  value

Set the type of service to be published via Bonjour param NSString

Definition at line 488 of file HTTPServer.m.

               :(NSString *)value
{
    // Copies the value into a local attribute
        NSString *valueCopy = [value copy];
        
    // Submits a block for asynchronous execution on the serverQueue
        dispatch_async(serverQueue, ^{
        
                [type release];
                type = [valueCopy retain];
        
        }); // END OF BLOCK
        
        [valueCopy release];
}
- (BOOL) start: (NSError **)  errPtr

Starts the server

Starts the server param NSError returns BOOL

Definition at line 580 of file HTTPServer.m.

             :(NSError **)errPtr
{
        // Creates local attributes
        __block BOOL success = YES;
        __block NSError *err = nil;
        
    // Submits a block for synchronous execution on serverQueue
        dispatch_sync(serverQueue, ^{
        
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                
        // if the socket accepts on an interface and port
                success = [asyncSocket acceptOnInterface:interface port:port error:&err];
        
        // If the socket accepts on a specific interface and port
                if (success)
                {
            // flag that the server is running
                        isRunning = YES;
            
            // publish the server as a network service
                        [self publishBonjour];
                }
                else // if server does not accept connections on a specific interface and port then thow and error
                {
                        [err retain];
                }
                
                [pool release];
        });  // END OF BLOCK
        
    // If there is an error pointer
        if (errPtr)
    {
                *errPtr = [err autorelease];
        }else{ // if there is not an error
                [err release];
        }
    
    // Return that the server is running
        return success;
}
+ (void) startBonjourThreadIfNeeded

Starts the bonjour thread if needed

- (BOOL) stop

Stops the server

Stops the server returns BOOL

Definition at line 628 of file HTTPServer.m.

{
        // Submits a block for synchronous execution on the serverQueue
        dispatch_sync(serverQueue, ^{
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                
                // First stop publishing the service via bonjour
                [self unpublishBonjour];
                
                // Stop listening / accepting incoming connections
                [asyncSocket disconnect];
                isRunning = NO;
                
                // Now stop all HTTP connections the server owns
                [connectionsLock lock];
        
        // Enumerates through the HTTP connections and stops them
                for (HTTPConnection *connection in connections)
                {
            // Stops the connection
                        [connection stop];
                }
        
        // Remove all the connections
                [connections removeAllObjects];
                [connectionsLock unlock];
                
                // Now stop all WebSocket connections the server owns
                [webSocketsLock lock];
        
        // Enumerate through the web sockets
                for (WebSocket *webSocket in webSockets)
                {
                        [webSocket stop];
                }
        // remove all the web sockets
                [webSockets removeAllObjects];
                [webSocketsLock unlock];
                
                [pool release];
        }); // END OF BLOCK
        
        return YES;
}
- (NSDictionary *) TXTRecordDictionary

Gets the TXT record dictionary

Gets the TXTRecordDictionary The extra data to use for this service via Bonjour. returns NSDictionary

Definition at line 509 of file HTTPServer.m.

{
    // Creates a local attribute
        __block NSDictionary *result;
        
    // Submits a block for synchronous execution on the serverQueue
        dispatch_sync(serverQueue, ^{
        
                result = [txtRecordDictionary retain];
        
        }); // END OF BLOCK
        
        return [result autorelease];
}
- (NSString *) type

Bonjour type for publishing the service. The default value is nil. The service will not be published via bonjour unless the type is set.

If you wish to publish the service as a traditional HTTP server, you should set the type to be "_http._tcp.".

If you change the type after the bonjour service has already been published (server already started), you'll need to invoke the republishBonjour method to update the broadcasted bonjour service.

- (void) unpublishBonjour

Unpublish the bonjour from the list of published network services


Field Documentation

- (GCDAsyncSocket*) asyncSocket [protected]

for reading and writing data

Definition at line 40 of file HTTPServer.h.

- (Class) connectionClass [protected]

Default is HTTP connection

The connection class is the class that will be used to handle connections. That is, when a new connection is created, an instance of this class will be intialized. The default connection class is HTTPConnection. If you use a different connection class, it is assumed that the class extends HTTPConnection returns Class

Definition at line 55 of file HTTPServer.h.

- (dispatch_queue_t) connectionQueue [protected]

Dispatch queues are lightweight objects to which blocks may be submitted. The system manages a pool of threads which process dispatch queues and invoke blocks submitted to them.

Definition at line 35 of file HTTPServer.h.

- (NSMutableArray*) connections [protected]

The connections to the server

Definition at line 108 of file HTTPServer.h.

- (NSLock*) connectionsLock [protected]

Locks the http connection. Note: uses POSIX threads to implement its locking behavior

Definition at line 118 of file HTTPServer.h.

- (NSString *) documentRoot [protected]

the document root

The document root is filesystem root for the webserver. Thus requests for /index.html will be referencing the index.html file within the document root directory. All file requests are relative to this document root. returns NSString

Definition at line 50 of file HTTPServer.h.

- (NSString *) domain [protected]

The domain the service should be published on, the default is 'local'

Gets the domain on which to broadcast this service via Bonjour. The default domain is "local". returns NSString

Definition at line 79 of file HTTPServer.h.

- (NSString *) interface [protected]

The interface the server should listen on, "en1", "lo0", etc

What interface to bind the listening socket to. returns NSString

Definition at line 60 of file HTTPServer.h.

- (BOOL) isRunning [protected]

Whether the server is running returns BOOL

Definition at line 126 of file HTTPServer.h.

- (NSString *) name [protected]

Default is the computers name that the server is running on

Gets the name to use for this service via Bonjour. The default name is an empty string, which should result in the published name being the host name of the computer. returns NSString

Definition at line 89 of file HTTPServer.h.

- (NSNetService*) netService [protected]

Represents a network service

Definition at line 74 of file HTTPServer.h.

- (UInt16) port [protected]

The listening port

The port to listen for connections on. By default this port is initially set to zero, which allows the kernel to pick an available port for us. After the HTTP server has started, the port being used may be obtained by this method.

Definition at line 65 of file HTTPServer.h.

- (NSString *) publishedName [protected]

The published server name

Gets the published name of the server returns NSString

Definition at line 94 of file HTTPServer.h.

- (dispatch_queue_t) serverQueue [protected]

Dispatch queues are lightweight objects to which blocks may be submitted. The system manages a pool of threads which process dispatch queues and invoke blocks submitted to them.

Definition at line 30 of file HTTPServer.h.

- (NSDictionary*) txtRecordDictionary [protected]

Dictionary consisting of "zero or more strings, packed together in memory without any intervening gaps or padding bytes for word alignment. The format of each constituent string within the DNS TXT record is a single length byte, followed by 0-255 bytes of text data."

Definition at line 99 of file HTTPServer.h.

- (NSString *) type [protected]

tcp or udp

Gets the type of service to publish via Bonjour. No type is set by default, and one must be set in order for the service to be published. returns NSString

Definition at line 84 of file HTTPServer.h.

- (NSMutableArray*) webSockets [protected]

The web socket connections

Definition at line 113 of file HTTPServer.h.

- (NSLock*) webSocketsLock [protected]

Locks the websocket. Note: uses POSIX threads to implement its locking behavior

Definition at line 123 of file HTTPServer.h.


The documentation for this class was generated from the following files:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Properties Defines