Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1 | //:: # Copyright 2013, Big Switch Networks, Inc. |
| 2 | //:: # |
| 3 | //:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | //:: # the following special exception: |
| 5 | //:: # |
| 6 | //:: # LOXI Exception |
| 7 | //:: # |
| 8 | //:: # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | //:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | //:: # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | //:: # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | //:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | //:: # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | //:: # |
| 15 | //:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | //:: # |
| 17 | //:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | //:: # a copy of the EPL at: |
| 19 | //:: # |
| 20 | //:: # http::: #www.eclipse.org/legal/epl-v10.html |
| 21 | //:: # |
| 22 | //:: # Unless required by applicable law or agreed to in writing, software |
| 23 | //:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | //:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | //:: # EPL for the specific language governing permissions and limitations |
| 26 | //:: # under the EPL. |
| 27 | //:: |
| 28 | //:: from loxi_ir import * |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 29 | //:: import os |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 30 | //:: import itertools |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 31 | //:: include('_copyright.java') |
| 32 | |
| 33 | //:: include('_autogen.java') |
| 34 | |
| 35 | package ${msg.package}; |
| 36 | |
| 37 | //:: include("_imports.java", msg=msg) |
| 38 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 39 | class ${impl_class} implements ${msg.interface.inherited_declaration()} { |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 40 | //:: if genopts.instrument: |
| 41 | private static final Logger logger = LoggerFactory.getLogger(${impl_class}.class); |
| 42 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 43 | // version: ${version} |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 44 | final static byte WIRE_VERSION = ${version.int_version}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 45 | //:: if msg.is_fixed_length: |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 46 | final static int LENGTH = ${msg.length}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 47 | //:: else: |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 48 | final static int MINIMUM_LENGTH = ${msg.min_length}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 49 | //:: #endif |
| 50 | |
| 51 | //:: for prop in msg.data_members: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 52 | //:: if prop.java_type.public_type != msg.interface.member_by_name(prop.name).java_type.public_type: |
| 53 | //:: raise Exception("Interface and Class types do not match up: C: {} <-> I: {}".format(prop.java_type.public_type, msg.interface.member_by_name(prop.name).java_type.public_type)) |
| 54 | //:: #endif |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 55 | //:: if prop.default_value: |
| 56 | private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value}; |
| 57 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 58 | //:: #end |
| 59 | |
| 60 | // OF message fields |
| 61 | //:: for prop in msg.data_members: |
| 62 | private final ${prop.java_type.public_type} ${prop.name}; |
| 63 | //:: #endfor |
Andreas Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 64 | // |
| 65 | //:: if all(prop.default_value for prop in msg.data_members): |
| 66 | // Immutable default instance |
| 67 | final static ${impl_class} DEFAULT = new ${impl_class}( |
| 68 | ${", ".join(prop.default_name for prop in msg.data_members)} |
| 69 | ); |
| 70 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 71 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 72 | //:: if msg.data_members: |
| 73 | // package private constructor - used by readers, builders, and factory |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 74 | ${impl_class}(${ |
| 75 | ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) { |
| 76 | //:: for prop in msg.data_members: |
| 77 | this.${prop.name} = ${prop.name}; |
| 78 | //:: #endfor |
| 79 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 80 | //:: else: |
| 81 | final static ${impl_class} INSTANCE = new ${impl_class}(); |
| 82 | // private empty constructor - use shared instance! |
| 83 | private ${impl_class}() { |
| 84 | } |
| 85 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 86 | |
| 87 | // Accessors for OF message fields |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 88 | //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 89 | |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 90 | //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)): |
| 91 | //:: include("custom/%s.java" % msg.name, msg=msg) |
| 92 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 93 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 94 | //:: if msg.data_members: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 95 | public ${msg.interface.name}.Builder createBuilder() { |
| 96 | return new BuilderWithParent(this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 99 | static class BuilderWithParent implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 100 | final ${impl_class} parentMessage; |
| 101 | |
| 102 | // OF message fields |
| 103 | //:: for prop in msg.data_members: |
| 104 | private boolean ${prop.name}Set; |
| 105 | private ${prop.java_type.public_type} ${prop.name}; |
| 106 | //:: #endfor |
| 107 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 108 | BuilderWithParent(${impl_class} parentMessage) { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 109 | this.parentMessage = parentMessage; |
| 110 | } |
| 111 | |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 112 | //:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 113 | |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 114 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 115 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 116 | public ${msg.interface.name} build() { |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 117 | //:: for prop in msg.data_members: |
| 118 | ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name}; |
| 119 | //:: if not prop.is_nullable and not prop.java_type.is_primitive: |
| 120 | if(${prop.name} == null) |
| 121 | throw new NullPointerException("Property ${prop.name} must not be null"); |
| 122 | //:: #endif |
| 123 | //:: #endfor |
| 124 | |
| 125 | // |
| 126 | //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)): |
| 127 | //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 128 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 129 | return new ${impl_class}( |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 130 | //:: for i, prop in enumerate(msg.data_members): |
| 131 | //:: comma = "," if i < len(msg.data_members)-1 else "" |
| 132 | ${prop.name}${comma} |
| 133 | //:: #endfor |
| 134 | ); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 135 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 136 | //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)): |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 137 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 138 | //:: #endif |
| 139 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 142 | static class Builder implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 143 | // OF message fields |
| 144 | //:: for prop in msg.data_members: |
| 145 | private boolean ${prop.name}Set; |
| 146 | private ${prop.java_type.public_type} ${prop.name}; |
| 147 | //:: #endfor |
| 148 | |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 149 | //:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 150 | // |
| 151 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 152 | public ${msg.interface.name} build() { |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 153 | //:: for prop in msg.data_members: |
| 154 | //:: if prop.default_value: |
| 155 | ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name}; |
| 156 | //:: else: |
| 157 | if(!this.${prop.name}Set) |
| 158 | throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set"); |
| 159 | //:: #endif |
| 160 | //:: if not prop.is_nullable and not prop.java_type.is_primitive: |
| 161 | if(${prop.name} == null) |
| 162 | throw new NullPointerException("Property ${prop.name} must not be null"); |
| 163 | //:: #endif |
| 164 | //:: #endfor |
| 165 | |
| 166 | //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)): |
| 167 | //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 168 | //:: #endif |
| 169 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 170 | return new ${impl_class}( |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 171 | //:: for i, prop in enumerate(msg.data_members): |
| 172 | //:: comma = "," if i < len(msg.data_members)-1 else "" |
| 173 | ${prop.name}${comma} |
| 174 | //:: #endfor |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 175 | ); |
| 176 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 177 | //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)): |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 178 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=False) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 179 | //:: #endif |
| 180 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 181 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 182 | //:: else: |
| 183 | // no data members - do not support builder |
| 184 | public ${msg.interface.name}.Builder createBuilder() { |
| 185 | throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded"); |
| 186 | } |
| 187 | //:: #endif |
| 188 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 189 | |
| 190 | final static Reader READER = new Reader(); |
| 191 | static class Reader implements OFMessageReader<${msg.interface.name}> { |
| 192 | @Override |
| 193 | public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 194 | //:: for prop in msg.members: |
| 195 | //:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value): |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 196 | int start = bb.readerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 197 | //:: break |
| 198 | //:: #endif |
| 199 | //:: #endfor |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 200 | //:: fields_with_length_member = {} |
| 201 | //:: for prop in msg.members: |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 202 | //:: if prop.is_virtual: |
| 203 | //:: continue |
| 204 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 205 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 206 | length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)}; |
| 207 | //:: elif prop.is_pad: |
| 208 | // pad: ${prop.length} bytes |
| 209 | bb.skipBytes(${prop.length}); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 210 | //:: elif prop.is_length_value: |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 211 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 212 | //:: if prop.is_fixed_value: |
| 213 | if(${prop.name} != ${prop.value}) |
| 214 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 215 | //:: else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 216 | if(${prop.name} < MINIMUM_LENGTH) |
| 217 | throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name}); |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 218 | //:: #endif |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 219 | if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) { |
| 220 | // Buffer does not have all data yet |
| 221 | bb.readerIndex(start); |
| 222 | return null; |
| 223 | } |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 224 | //:: if genopts.instrument: |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 225 | if(logger.isTraceEnabled()) |
| 226 | logger.trace("readFrom - length={}", ${prop.name}); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 227 | //:: #endif |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 228 | //:: elif prop.is_fixed_value: |
| 229 | // fixed value property ${prop.name} == ${prop.value} |
| 230 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 231 | if(${prop.name} != ${prop.priv_value}) |
| 232 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 233 | //:: elif prop.is_field_length_value: |
| 234 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 235 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 236 | //:: else: |
| 237 | // fixme: todo ${prop.name} |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 238 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 239 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 240 | //:: if msg.align: |
Andreas Wundsam | 87bdcf4 | 2013-10-23 15:19:01 -0700 | [diff] [blame] | 241 | //:: if msg.length_includes_align: |
| 242 | // align message to ${msg.align} bytes (length contains aligned value) |
| 243 | bb.skipBytes(length - (bb.readerIndex() - start)); |
| 244 | //:: else: |
| 245 | // align message to ${msg.align} bytes (length does not contain alignment) |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 246 | bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length ); |
| 247 | //:: #endif |
Andreas Wundsam | 87bdcf4 | 2013-10-23 15:19:01 -0700 | [diff] [blame] | 248 | //:: #endif |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 249 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 250 | //:: if msg.data_members: |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 251 | //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)): |
| 252 | //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 253 | //:: #endif |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 254 | ${impl_class} ${msg.variable_name} = new ${impl_class}( |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 255 | ${",\n ".join( |
| 256 | [ prop.name for prop in msg.data_members])} |
| 257 | ); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 258 | //:: if genopts.instrument: |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 259 | if(logger.isTraceEnabled()) |
| 260 | logger.trace("readFrom - read={}", ${msg.variable_name}); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 261 | //:: #endif |
| 262 | return ${msg.variable_name}; |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 263 | //:: else: |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 264 | //:: if genopts.instrument: |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 265 | if(logger.isTraceEnabled()) |
| 266 | logger.trace("readFrom - returning shared instance={}", INSTANCE); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 267 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 268 | return INSTANCE; |
| 269 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 273 | public void putTo(PrimitiveSink sink) { |
| 274 | FUNNEL.funnel(this, sink); |
| 275 | } |
| 276 | |
| 277 | final static ${impl_class}Funnel FUNNEL = new ${impl_class}Funnel(); |
| 278 | static class ${impl_class}Funnel implements Funnel<${impl_class}> { |
| 279 | private static final long serialVersionUID = 1L; |
| 280 | @Override |
| 281 | public void funnel(${impl_class} message, PrimitiveSink sink) { |
| 282 | //:: for prop in msg.members: |
| 283 | //:: if prop.is_virtual: |
| 284 | //:: continue |
| 285 | //:: elif prop.is_data: |
| 286 | ${prop.java_type.funnel_op(version, "message." + prop.name, pub_type=True)}; |
| 287 | //:: elif prop.is_pad: |
| 288 | // skip pad (${prop.length} bytes) |
| 289 | //:: elif prop.is_fixed_value: |
| 290 | // fixed value property ${prop.name} = ${prop.value} |
| 291 | ${prop.java_type.funnel_op(version, prop.priv_value, pub_type=False)}; |
| 292 | //:: else: |
| 293 | // FIXME: skip funnel of ${prop.name} |
| 294 | //:: #endif |
| 295 | //:: #endfor |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 300 | public void writeTo(ChannelBuffer bb) { |
| 301 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | final static Writer WRITER = new Writer(); |
| 305 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 306 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 307 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 308 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 309 | int startIndex = bb.writerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 310 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 311 | //:: fields_with_length_member = {} |
| 312 | //:: for prop in msg.members: |
| 313 | //:: if prop.c_name in fields_with_length_member: |
| 314 | int ${prop.name}StartIndex = bb.writerIndex(); |
| 315 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 316 | //:: if prop.is_virtual: |
| 317 | //:: continue |
| 318 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 319 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 320 | //:: elif prop.is_pad: |
| 321 | // pad: ${prop.length} bytes |
| 322 | bb.writeZero(${prop.length}); |
| 323 | //:: elif prop.is_fixed_value: |
| 324 | // fixed value property ${prop.name} = ${prop.value} |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 325 | ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 326 | //:: elif prop.is_length_value: |
| 327 | // ${prop.name} is length of variable message, will be updated at the end |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 328 | //:: if not msg.is_fixed_length: |
| 329 | int lengthIndex = bb.writerIndex(); |
| 330 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 331 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 332 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 333 | //:: elif prop.is_field_length_value: |
| 334 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 335 | // ${prop.name} is length indicator for ${prop.member.field_name}, will be |
| 336 | // udpated when ${prop.member.field_name} has been written |
| 337 | int ${prop.name}Index = bb.writerIndex(); |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 338 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 339 | //:: else: |
| 340 | // FIXME: todo write ${prop.name} |
| 341 | //:: #endif |
| 342 | //:: if prop.c_name in fields_with_length_member: |
| 343 | //:: length_member_name = fields_with_length_member[prop.c_name] |
| 344 | // update field length member ${length_member_name} |
| 345 | int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex; |
| 346 | bb.setShort(${length_member_name}Index, ${prop.name}Length); |
| 347 | //:: #endif |
| 348 | //:: #endfor |
| 349 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 350 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 351 | // update length field |
| 352 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 353 | //:: if msg.align: |
| 354 | int alignedLength = ((length + ${msg.align-1})/${msg.align} * ${msg.align}); |
| 355 | //:: #endif |
| 356 | bb.setShort(lengthIndex, ${"alignedLength" if msg.length_includes_align else "length"}); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 357 | //:: if msg.align: |
| 358 | // align message to ${msg.align} bytes |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 359 | bb.writeZero(alignedLength - length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 360 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 361 | //:: #end |
| 362 | |
| 363 | } |
| 364 | } |
| 365 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 366 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 367 | public String toString() { |
| 368 | StringBuilder b = new StringBuilder("${msg.name}("); |
| 369 | //:: for i, prop in enumerate(msg.data_members): |
| 370 | //:: if i > 0: |
| 371 | b.append(", "); |
| 372 | //:: #endif |
| 373 | b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name }); |
| 374 | //:: #endfor |
| 375 | b.append(")"); |
| 376 | return b.toString(); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | @Override |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 381 | public boolean equals(Object obj) { |
| 382 | if (this == obj) |
| 383 | return true; |
| 384 | if (obj == null) |
| 385 | return false; |
| 386 | if (getClass() != obj.getClass()) |
| 387 | return false; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 388 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 389 | ${msg.name} other = (${msg.name}) obj; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 390 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 391 | |
| 392 | //:: for prop in msg.data_members: |
| 393 | //:: if prop.java_type.is_primitive: |
| 394 | if( ${prop.name} != other.${prop.name}) |
| 395 | return false; |
| 396 | //:: elif prop.java_type.is_array: |
| 397 | if (!Arrays.equals(${prop.name}, other.${prop.name})) |
| 398 | return false; |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 399 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 400 | if (${prop.name} == null) { |
| 401 | if (other.${prop.name} != null) |
| 402 | return false; |
| 403 | } else if (!${prop.name}.equals(other.${prop.name})) |
| 404 | return false; |
| 405 | //:: #endif |
| 406 | //:: #endfor |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public int hashCode() { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 412 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 413 | final int prime = 31; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 414 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 415 | int result = 1; |
| 416 | |
| 417 | //:: for prop in msg.data_members: |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 418 | //:: if prop.java_type.pub_type == 'long': |
| 419 | result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32)); |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 420 | //:: elif prop.java_type.pub_type == 'boolean': |
| 421 | result = prime * result + (${prop.name} ? 1231 : 1237); |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 422 | //:: elif prop.java_type.is_primitive: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 423 | result = prime * result + ${prop.name}; |
| 424 | //:: elif prop.java_type.is_array: |
| 425 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 426 | //:: else: |
| 427 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 428 | //:: #endif |
| 429 | //:: #endfor |
| 430 | return result; |
| 431 | } |
| 432 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 433 | } |