jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | #include "InvokeMessage.h" |
| 18 | #include "HostChannel.h" |
| 19 | #include "scoped_ptr/scoped_ptr.h" |
| 20 | |
| 21 | InvokeMessage::~InvokeMessage() { |
| 22 | delete[] args; |
| 23 | } |
| 24 | |
| 25 | char InvokeMessage::getType() const { |
| 26 | return TYPE; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Receive an Invoke message from the channel (note that the message |
| 31 | * type has already been read). Caller is responsible for destroying |
| 32 | * returned message. Returns null on error. |
| 33 | */ |
| 34 | InvokeMessage* InvokeMessage::receive(HostChannel& channel) { |
| 35 | std::string methodName; |
| 36 | if (!channel.readString(methodName)) { |
| 37 | // TODO(jat): error handling |
| 38 | printf("Failed to read method name\n"); |
| 39 | return 0; |
| 40 | } |
| 41 | Value thisRef; |
| 42 | if (!channel.readValue(thisRef)) { |
| 43 | // TODO(jat): error handling |
| 44 | printf("Failed to read thisRef\n"); |
| 45 | return 0; |
| 46 | } |
| 47 | int numArgs; |
| 48 | if (!channel.readInt(numArgs)) { |
| 49 | // TODO(jat): error handling |
| 50 | printf("Failed to read #args\n"); |
| 51 | return 0; |
| 52 | } |
| 53 | scoped_array<Value> args(new Value[numArgs]); |
| 54 | for (int i = 0; i < numArgs; ++i) { |
| 55 | if (!channel.readValue(args[i])) { |
| 56 | // TODO(jat): error handling |
| 57 | printf("Failed to read arg[%d]\n", i); |
| 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | return new InvokeMessage(thisRef, methodName, numArgs, args.release()); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Request the server invoke a method on an object and return the result. |
| 66 | * |
| 67 | * Note that the method to be invoked is sent as an integer (a dispatch id) to the server. |
| 68 | */ |
| 69 | bool InvokeMessage::send(HostChannel& channel, const Value& thisRef, int methodDispatchId, |
| 70 | int numArgs, const Value* args) { |
| 71 | if (!channel.sendByte(TYPE)) return false; |
| 72 | if (!channel.sendInt(methodDispatchId)) return false; |
| 73 | if (!channel.sendValue(thisRef)) return false; |
| 74 | if (!channel.sendInt(numArgs)) return false; |
| 75 | for (int i = 0; i < numArgs; ++i) { |
| 76 | if (!channel.sendValue(args[i])) return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |