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