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