-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from wz2b/tag_struct_patch
Struct/array tag patches
- Loading branch information
Showing
5 changed files
with
156 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,104 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012 Oak Ridge National Laboratory. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*******************************************************************************/ | ||
/******************************************************************************* | ||
* Copyright (c) 2012 Oak Ridge National Laboratory. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*******************************************************************************/ | ||
package etherip.types; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** Control Net Path for element path | ||
* | ||
* | ||
* <p>Example (with suitable static import): | ||
* <p><code>CNPath path = Symbol.name("my_tag")</code> | ||
* @author Kay Kasemir | ||
*/ | ||
public class CNSymbolPath extends CNPath | ||
{ | ||
private String symbol; | ||
class PathAndIndex{ | ||
private String path; | ||
private Integer index; | ||
public PathAndIndex(String path, Integer index) { | ||
this.path = path; | ||
this.index = index; | ||
} | ||
public String getPath() { | ||
return path; | ||
} | ||
public Integer getIndex() { | ||
return index; | ||
} | ||
|
||
}; | ||
|
||
private final Pattern PATTERN_BRACKETS = Pattern.compile("\\[(\\d+)\\]"); | ||
|
||
private List<PathAndIndex> paths = new ArrayList<>(); | ||
|
||
/** Initialize | ||
* @param symbol Name of symbol | ||
*/ | ||
protected CNSymbolPath(final String symbol) | ||
{ | ||
this.symbol = symbol; | ||
for(String s:symbol.split("\\.")){ | ||
Matcher m = PATTERN_BRACKETS.matcher(s); | ||
Integer index = null; | ||
String path = s; | ||
while(m.find()){ | ||
String match = m.group().replace("[","").replace("]", ""); | ||
index = Integer.parseInt(match); | ||
path = path.replace("[" + match + "]", ""); | ||
} | ||
paths.add(new PathAndIndex(path, index)); | ||
} | ||
} | ||
/** {@inheritDoc} */ | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public int getRequestSize() | ||
{ // End of string is padded if length is odd | ||
return 2 + symbol.length() + (needPad() ? 1 : 0); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public int getRequestSize() | ||
{ // End of string is padded if length is odd | ||
int count = 0; | ||
for(PathAndIndex s:paths){ | ||
count += 2 + s.getPath().length() + (needPad(s.getPath()) ? 1 : 0); | ||
if(s.getIndex()!=null) | ||
count += 2; | ||
} | ||
return count; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void encode(final ByteBuffer buf, final StringBuilder log) | ||
public void encode(final ByteBuffer buf, final StringBuilder log) | ||
{ | ||
// spec 4 p.21: "ANSI extended symbol segment" | ||
buf.put((byte) (getRequestSize() / 2)); | ||
buf.put((byte) 0x91); | ||
buf.put((byte) symbol.length()); | ||
buf.put(symbol.getBytes()); | ||
if (needPad()) | ||
buf.put((byte) 0); | ||
for(PathAndIndex pi:paths){ | ||
String s = pi.getPath(); | ||
buf.put((byte) 0x91); | ||
buf.put((byte) s.length()); | ||
buf.put(s.getBytes()); | ||
if (needPad(s)) | ||
buf.put((byte) 0); | ||
Integer index = pi.getIndex(); | ||
if(index!=null){ | ||
//Path Segment 28, from wireshark | ||
buf.put((byte) 0x28); | ||
buf.put(index.byteValue()); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** @return Is path of odd length, requiring a pad byte? */ | ||
private boolean needPad() | ||
{ | ||
// Findbugs: x%2==1 fails for negative numbers | ||
return (symbol.length() % 2) != 0; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
private boolean needPad(String s) | ||
{ | ||
final StringBuilder buf = new StringBuilder(); | ||
buf.append("Path Symbol(0x91) '").append(symbol).append("'"); | ||
if (needPad()) | ||
buf.append(", 0x00"); | ||
return buf.toString(); | ||
// Findbugs: x%2==1 fails for negative numbers | ||
return (s.length() % 2) != 0; | ||
} | ||
} |