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: |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 223 | if(logger.isTraceEnabled()) |
| 224 | logger.trace("readFrom - length={}", ${prop.name}); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 225 | //:: #endif |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 226 | //:: elif prop.is_fixed_value: |
| 227 | // fixed value property ${prop.name} == ${prop.value} |
| 228 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 229 | if(${prop.name} != ${prop.priv_value}) |
| 230 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 231 | //:: elif prop.is_field_length_value: |
| 232 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 233 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 234 | //:: else: |
| 235 | // fixme: todo ${prop.name} |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 236 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 237 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 238 | //:: if msg.align: |
Andreas Wundsam | 87bdcf4 | 2013-10-23 15:19:01 -0700 | [diff] [blame] | 239 | //:: if msg.length_includes_align: |
| 240 | // align message to ${msg.align} bytes (length contains aligned value) |
| 241 | bb.skipBytes(length - (bb.readerIndex() - start)); |
| 242 | //:: else: |
| 243 | // align message to ${msg.align} bytes (length does not contain alignment) |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 244 | bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length ); |
| 245 | //:: #endif |
Andreas Wundsam | 87bdcf4 | 2013-10-23 15:19:01 -0700 | [diff] [blame] | 246 | //:: #endif |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 247 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 248 | //:: if msg.data_members: |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 249 | //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)): |
| 250 | //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 251 | //:: #endif |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 252 | ${impl_class} ${msg.variable_name} = new ${impl_class}( |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 253 | ${",\n ".join( |
| 254 | [ prop.name for prop in msg.data_members])} |
| 255 | ); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 256 | //:: if genopts.instrument: |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 257 | if(logger.isTraceEnabled()) |
| 258 | logger.trace("readFrom - read={}", ${msg.variable_name}); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 259 | //:: #endif |
| 260 | return ${msg.variable_name}; |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 261 | //:: else: |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 262 | //:: if genopts.instrument: |
Andreas Wundsam | e8802ac | 2013-11-05 11:47:04 -0800 | [diff] [blame] | 263 | if(logger.isTraceEnabled()) |
| 264 | logger.trace("readFrom - returning shared instance={}", INSTANCE); |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 265 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 266 | return INSTANCE; |
| 267 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 271 | public void putTo(PrimitiveSink sink) { |
| 272 | FUNNEL.funnel(this, sink); |
| 273 | } |
| 274 | |
| 275 | final static ${impl_class}Funnel FUNNEL = new ${impl_class}Funnel(); |
| 276 | static class ${impl_class}Funnel implements Funnel<${impl_class}> { |
| 277 | private static final long serialVersionUID = 1L; |
| 278 | @Override |
| 279 | public void funnel(${impl_class} message, PrimitiveSink sink) { |
| 280 | //:: for prop in msg.members: |
| 281 | //:: if prop.is_virtual: |
| 282 | //:: continue |
| 283 | //:: elif prop.is_data: |
| 284 | ${prop.java_type.funnel_op(version, "message." + prop.name, pub_type=True)}; |
| 285 | //:: elif prop.is_pad: |
| 286 | // skip pad (${prop.length} bytes) |
| 287 | //:: elif prop.is_fixed_value: |
| 288 | // fixed value property ${prop.name} = ${prop.value} |
| 289 | ${prop.java_type.funnel_op(version, prop.priv_value, pub_type=False)}; |
| 290 | //:: else: |
| 291 | // FIXME: skip funnel of ${prop.name} |
| 292 | //:: #endif |
| 293 | //:: #endfor |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 298 | public void writeTo(ChannelBuffer bb) { |
| 299 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | final static Writer WRITER = new Writer(); |
| 303 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 304 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 305 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 306 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 307 | int startIndex = bb.writerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 308 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 309 | //:: fields_with_length_member = {} |
| 310 | //:: for prop in msg.members: |
| 311 | //:: if prop.c_name in fields_with_length_member: |
| 312 | int ${prop.name}StartIndex = bb.writerIndex(); |
| 313 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 314 | //:: if prop.is_virtual: |
| 315 | //:: continue |
| 316 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 317 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 318 | //:: elif prop.is_pad: |
| 319 | // pad: ${prop.length} bytes |
| 320 | bb.writeZero(${prop.length}); |
| 321 | //:: elif prop.is_fixed_value: |
| 322 | // fixed value property ${prop.name} = ${prop.value} |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 323 | ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 324 | //:: elif prop.is_length_value: |
| 325 | // ${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] | 326 | //:: if not msg.is_fixed_length: |
| 327 | int lengthIndex = bb.writerIndex(); |
| 328 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 329 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 330 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 331 | //:: elif prop.is_field_length_value: |
| 332 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 333 | // ${prop.name} is length indicator for ${prop.member.field_name}, will be |
| 334 | // udpated when ${prop.member.field_name} has been written |
| 335 | int ${prop.name}Index = bb.writerIndex(); |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 336 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 337 | //:: else: |
| 338 | // FIXME: todo write ${prop.name} |
| 339 | //:: #endif |
| 340 | //:: if prop.c_name in fields_with_length_member: |
| 341 | //:: length_member_name = fields_with_length_member[prop.c_name] |
| 342 | // update field length member ${length_member_name} |
| 343 | int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex; |
| 344 | bb.setShort(${length_member_name}Index, ${prop.name}Length); |
| 345 | //:: #endif |
| 346 | //:: #endfor |
| 347 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 348 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 349 | // update length field |
| 350 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 351 | //:: if msg.align: |
| 352 | int alignedLength = ((length + ${msg.align-1})/${msg.align} * ${msg.align}); |
| 353 | //:: #endif |
| 354 | bb.setShort(lengthIndex, ${"alignedLength" if msg.length_includes_align else "length"}); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 355 | //:: if msg.align: |
| 356 | // align message to ${msg.align} bytes |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 357 | bb.writeZero(alignedLength - length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 358 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 359 | //:: #end |
| 360 | |
| 361 | } |
| 362 | } |
| 363 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 364 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 365 | public String toString() { |
| 366 | StringBuilder b = new StringBuilder("${msg.name}("); |
| 367 | //:: for i, prop in enumerate(msg.data_members): |
| 368 | //:: if i > 0: |
| 369 | b.append(", "); |
| 370 | //:: #endif |
| 371 | b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name }); |
| 372 | //:: #endfor |
| 373 | b.append(")"); |
| 374 | return b.toString(); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | @Override |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 379 | public boolean equals(Object obj) { |
| 380 | if (this == obj) |
| 381 | return true; |
| 382 | if (obj == null) |
| 383 | return false; |
| 384 | if (getClass() != obj.getClass()) |
| 385 | return false; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 386 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 387 | ${msg.name} other = (${msg.name}) obj; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 388 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 389 | |
| 390 | //:: for prop in msg.data_members: |
| 391 | //:: if prop.java_type.is_primitive: |
| 392 | if( ${prop.name} != other.${prop.name}) |
| 393 | return false; |
| 394 | //:: elif prop.java_type.is_array: |
| 395 | if (!Arrays.equals(${prop.name}, other.${prop.name})) |
| 396 | return false; |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 397 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 398 | if (${prop.name} == null) { |
| 399 | if (other.${prop.name} != null) |
| 400 | return false; |
| 401 | } else if (!${prop.name}.equals(other.${prop.name})) |
| 402 | return false; |
| 403 | //:: #endif |
| 404 | //:: #endfor |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public int hashCode() { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 410 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 411 | final int prime = 31; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 412 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 413 | int result = 1; |
| 414 | |
| 415 | //:: for prop in msg.data_members: |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 416 | //:: if prop.java_type.pub_type == 'long': |
| 417 | result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32)); |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 418 | //:: elif prop.java_type.pub_type == 'boolean': |
| 419 | result = prime * result + (${prop.name} ? 1231 : 1237); |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 420 | //:: elif prop.java_type.is_primitive: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 421 | result = prime * result + ${prop.name}; |
| 422 | //:: elif prop.java_type.is_array: |
| 423 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 424 | //:: else: |
| 425 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 426 | //:: #endif |
| 427 | //:: #endfor |
| 428 | return result; |
| 429 | } |
| 430 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 431 | |
| 432 | } |