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