Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #import "GRXForwardingWriter.h" |
| 20 | |
Muxi Yan | c92d90a | 2018-04-11 18:10:02 -0700 | [diff] [blame] | 21 | @interface GRXForwardingWriter ()<GRXWriteable> |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 22 | @end |
| 23 | |
| 24 | @implementation GRXForwardingWriter { |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 25 | GRXWriter *_writer; |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 26 | id<GRXWriteable> _writeable; |
| 27 | } |
| 28 | |
| 29 | - (instancetype)init { |
| 30 | return [self initWithWriter:nil]; |
| 31 | } |
| 32 | |
| 33 | // Designated initializer |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 34 | - (instancetype)initWithWriter:(GRXWriter *)writer { |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 35 | if (!writer) { |
Jorge Canizales | c678c30 | 2015-08-07 18:28:29 -0700 | [diff] [blame] | 36 | return nil; |
| 37 | } |
| 38 | if (writer.state != GRXWriterStateNotStarted) { |
Jorge Canizales | fd51dff | 2015-08-08 16:12:52 -0700 | [diff] [blame] | 39 | [NSException raise:NSInvalidArgumentException |
| 40 | format:@"The writer argument must not have already started."]; |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 41 | } |
| 42 | if ((self = [super init])) { |
| 43 | _writer = writer; |
| 44 | } |
| 45 | return self; |
| 46 | } |
| 47 | |
| 48 | // This is used to send a completion or an error to the writeable. It nillifies |
| 49 | // our reference to it in order to guarantee no more messages are sent to it, |
| 50 | // and to release it. |
| 51 | - (void)finishOutputWithError:(NSError *)errorOrNil { |
| 52 | id<GRXWriteable> writeable = _writeable; |
| 53 | _writeable = nil; |
| 54 | [writeable writesFinishedWithError:errorOrNil]; |
| 55 | } |
| 56 | |
| 57 | // This is used to stop the input writer. It nillifies our reference to it |
| 58 | // to release it. |
| 59 | - (void)finishInput { |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 60 | GRXWriter *writer = _writer; |
Jorge Canizales | 7b52c98 | 2015-07-16 18:41:21 -0700 | [diff] [blame] | 61 | _writer = nil; |
| 62 | writer.state = GRXWriterStateFinished; |
| 63 | } |
| 64 | |
| 65 | #pragma mark GRXWriteable implementation |
| 66 | |
| 67 | - (void)writeValue:(id)value { |
| 68 | [_writeable writeValue:value]; |
| 69 | } |
| 70 | |
| 71 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
| 72 | _writer = nil; |
| 73 | [self finishOutputWithError:errorOrNil]; |
| 74 | } |
| 75 | |
| 76 | #pragma mark GRXWriter implementation |
| 77 | |
| 78 | - (GRXWriterState)state { |
| 79 | return _writer ? _writer.state : GRXWriterStateFinished; |
| 80 | } |
| 81 | |
| 82 | - (void)setState:(GRXWriterState)state { |
| 83 | if (state == GRXWriterStateFinished) { |
| 84 | _writeable = nil; |
| 85 | [self finishInput]; |
| 86 | } else { |
| 87 | _writer.state = state; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
| 92 | _writeable = writeable; |
| 93 | [_writer startWithWriteable:self]; |
| 94 | } |
| 95 | |
| 96 | - (void)finishWithError:(NSError *)errorOrNil { |
| 97 | [self finishOutputWithError:errorOrNil]; |
| 98 | [self finishInput]; |
| 99 | } |
| 100 | |
| 101 | @end |