BLIP/BLIPWriter.m
author Jens Alfke <jens@mooseyard.com>
Tue Jun 03 16:56:33 2008 -0700 (2008-06-03)
changeset 11 29e8b03c05d4
parent 1 8267d5c429c4
child 18 3be241de1630
permissions -rw-r--r--
* Initial checkin of BLIP.py. (Receiving seems to work.)
* FIXED: Abbreviation list in BLIPProperties was messed up.
* Renamed some instance variables to use 'request' instead of 'query'.
* Test client doesn't throw an assertion-failure now when the number of unresponded requests exceeds 100.
     1 //
     2 //  BLIPFrameWriter.m
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/18/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "BLIPReader.h"
    10 #import "BLIPWriter.h"
    11 #import "BLIP_Internal.h"
    12 
    13 #import "Logging.h"
    14 #import "Test.h"
    15 
    16 
    17 #define kDefaultFrameSize 4096
    18 
    19 
    20 @implementation BLIPWriter
    21 
    22 
    23 - (void) dealloc
    24 {
    25     [_outBox release];
    26     [super dealloc];
    27 }
    28 
    29 - (void) disconnect
    30 {
    31     [_outBox makeObjectsPerformSelector: @selector(_connectionClosed) withObject: nil];
    32     setObj(&_outBox,nil);
    33     [super disconnect];
    34 }
    35 
    36 @synthesize numRequestsSent=_numRequestsSent;
    37 
    38 
    39 - (BOOL) isBusy
    40 {
    41     return _outBox.count>0 || [super isBusy];
    42 }
    43 
    44 
    45 - (void) _queueMessage: (BLIPMessage*)msg isNew: (BOOL)isNew
    46 {
    47     int n = _outBox.count, index;
    48     if( msg.urgent && n > 1 ) {
    49         // High-priority gets queued after the last existing high-priority message,
    50         // leaving one regular-priority message in between if possible.
    51         for( index=n-1; index>0; index-- ) {
    52             BLIPMessage *otherMsg = [_outBox objectAtIndex: index];
    53             if( [otherMsg urgent] ) {
    54                 index = MIN(index+2, n);
    55                 break;
    56             } else if( isNew && otherMsg._bytesWritten==0 ) {
    57                 // But have to keep message starts in order
    58                 index = index+1;
    59                 break;
    60             }
    61         }
    62         if( index==0 )
    63             index = 1;
    64     } else {
    65         // Regular priority goes at the end of the queue:
    66         index = n;
    67     }
    68     if( ! _outBox )
    69         _outBox = [[NSMutableArray alloc] init];
    70     [_outBox insertObject: msg atIndex: index];
    71     
    72     if( isNew ) {
    73         LogTo(BLIP,@"%@ queuing outgoing %@ at index %i",self,msg,index);
    74         if( n==0 )
    75             [self queueIsEmpty];
    76     }
    77 }
    78 
    79 
    80 - (BOOL) sendMessage: (BLIPMessage*)message
    81 {
    82     if( _shouldClose ) {
    83         Warn(@"%@: Attempt to send a message after the connection has started closing",self);
    84         return NO;
    85     }
    86     Assert(!message.sent,@"message has already been sent");
    87     [self _queueMessage: message isNew: YES];
    88     return YES;
    89 }
    90 
    91 
    92 - (BOOL) sendRequest: (BLIPRequest*)q response: (BLIPResponse*)response
    93 {
    94     if( !_shouldClose ) {
    95         [q _assignedNumber: ++_numRequestsSent];
    96         if( response ) {
    97             [response _assignedNumber: _numRequestsSent];
    98             [(BLIPReader*)self.reader _addPendingResponse: response];
    99         }
   100     }
   101     return [self sendMessage: q];
   102 }
   103 
   104 
   105 - (void) queueIsEmpty
   106 {
   107     if( _outBox.count > 0 ) {
   108         // Pop first message in queue:
   109         BLIPMessage *msg = [[_outBox objectAtIndex: 0] retain];
   110         [_outBox removeObjectAtIndex: 0];
   111         
   112         // As an optimization, allow message to send a big frame unless there's a higher-priority
   113         // message right behind it:
   114         size_t frameSize = kDefaultFrameSize;
   115         if( msg.urgent || _outBox.count==0 || ! [[_outBox objectAtIndex: 0] urgent] )
   116             frameSize *= 4;
   117         
   118         if( [msg _writeFrameTo: self maxSize: frameSize] ) {
   119             // add it back so it can send its next frame later:
   120             [self _queueMessage: msg isNew: NO];
   121         }
   122         [msg release];
   123     } else {
   124         LogTo(BLIPVerbose,@"%@: no more work for writer",self);
   125     }
   126 }
   127 
   128 
   129 
   130 @end
   131 
   132 
   133 /*
   134  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   135  
   136  Redistribution and use in source and binary forms, with or without modification, are permitted
   137  provided that the following conditions are met:
   138  
   139  * Redistributions of source code must retain the above copyright notice, this list of conditions
   140  and the following disclaimer.
   141  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   142  and the following disclaimer in the documentation and/or other materials provided with the
   143  distribution.
   144  
   145  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   146  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   147  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   148  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   149  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   150   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   151  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   152  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   153  */