16 package com.sonycsl.echo.eoj;
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
43 private Receiver mReceiver = null;
47 private HashSet<Byte> mStatusChangeAnnouncementProperties;
48 private HashSet<Byte> mSetProperties;
49 private HashSet<Byte> mGetProperties;
54 mStatusChangeAnnouncementProperties =
new HashSet<Byte>();
55 mSetProperties =
new HashSet<Byte>();
56 mGetProperties =
new HashSet<Byte>();
62 Echo.getEventListener().onNewEchoObject(
this);
66 Echo.getEventListener().onNewEchoObject(
this);
72 mStatusChangeAnnouncementProperties.add(epc);
76 if(mStatusChangeAnnouncementProperties.contains(epc))
77 mStatusChangeAnnouncementProperties.remove(epc);
81 mStatusChangeAnnouncementProperties.clear();
85 byte[] ret =
new byte[mStatusChangeAnnouncementProperties.size()];
86 Iterator<Byte> it = mStatusChangeAnnouncementProperties.iterator();
87 for(
int i = 0; i < ret.length; i++) {
94 mSetProperties.add(epc);
99 if(mSetProperties.contains(epc))
100 mSetProperties.remove(epc);
104 mSetProperties.clear();
108 byte[] ret =
new byte[mSetProperties.size()];
109 Iterator<Byte> it = mSetProperties.iterator();
110 for(
int i = 0; i < ret.length; i++) {
117 mGetProperties.add(epc);
121 if(mGetProperties.contains(epc))
122 mGetProperties.remove(epc);
126 mGetProperties.clear();
130 byte[] ret =
new byte[mGetProperties.size()];
131 Iterator<Byte> it = mGetProperties.iterator();
132 for(
int i = 0; i < ret.length; i++) {
141 StringBuilder sb =
new StringBuilder();
142 sb.append(
"groupCode:");
144 sb.append(
",classCode:");
146 sb.append(
",instanceCode:");
148 sb.append(
",address:");
150 sb.append(
getNode().getAddressStr());
152 return new String(sb);
157 return (byte)((code >> 8) & 0xFF);
162 return (byte)(code & 0xFF);
187 if(node==null)
return false;
188 else return node.isSelfNode();
194 if(node==null)
return true;
195 else return node.isProxy();
212 mReceiver = receiver;
253 case EchoFrame.ESV_SETI:
256 case EchoFrame.ESV_SETC:
259 case EchoFrame.ESV_GET:
262 case EchoFrame.ESV_INF_REQ:
265 case EchoFrame.ESV_INFC:
268 case EchoFrame.ESV_SET_GET:
275 response.setTID(frame.getTID());
277 case EchoFrame.ESV_SETI:
278 case EchoFrame.ESV_SETC:
283 case EchoFrame.ESV_GET:
284 case EchoFrame.ESV_INF_REQ:
289 case EchoFrame.ESV_INFC:
291 response.addPropertyForResponse(p.epc);
301 boolean success =
false;
302 Echo.EventListener listener = Echo.getEventListener();
304 if(mSetProperties.contains(property.
epc)) {
306 if(listener != null) listener.isValidProperty(
this, property, valid);
310 if(listener != null) listener.setProperty(
this, property, success);
318 }
catch (Exception e) {
321 try{
if(listener != null) listener.onCatchException(e);}
catch(Exception ex){}
325 response.addPropertyForResponse(property.epc);
327 response.addPropertyForResponse(property.epc, property.edt);
333 Echo.EventListener listener = Echo.getEventListener();
336 edt = mGetProperties.contains(epc) ?
getProperty(epc) : null;
341 listener.getProperty(
this, property);
343 if(listener != null) listener.isValidProperty(
this, property, valid);
350 }
catch (Exception e) {
353 try{
if(listener != null) listener.onCatchException(e);}
catch(Exception ex){}
355 response.addPropertyForResponse(epc, edt);
362 public Setter
set(
boolean responseRequired) {
364 ,
getNode().getAddressStr(), responseRequired);
367 public Getter
get() {
376 protected Informer
inform(
boolean multicast) {
382 address =
getNode().getAddressStr();
394 public static class Receiver {
397 onReceiveFrame(eoj, frame);
400 case EchoFrame.ESV_SET_RES:
case EchoFrame.ESV_SETI_SNA:
case EchoFrame.ESV_SETC_SNA:
402 onSetProperty(eoj, frame.
getTID(), frame.getESV(), p, (p.pdc == 0));
405 case EchoFrame.ESV_GET_RES:
case EchoFrame.ESV_GET_SNA:
406 case EchoFrame.ESV_INF:
case EchoFrame.ESV_INF_SNA:
407 case EchoFrame.ESV_INFC:
409 onGetProperty(eoj, frame.
getTID(), frame.getESV(), p, (p.pdc != 0));
412 case EchoFrame.ESV_INFC_RES:
414 onInformProperty(eoj, frame.
getTID(), frame.getESV(), p);
420 public void onReceiveFrame(
EchoObject eoj, EchoFrame frame) {
424 protected boolean onSetProperty(
EchoObject eoj,
short tid, byte esv, EchoProperty property,
boolean success) {
428 protected boolean onGetProperty(
EchoObject eoj,
short tid, byte esv, EchoProperty property,
boolean success) {
432 protected boolean onInformProperty(
EchoObject eoj,
short tid, byte esv, EchoProperty property) {
438 protected static abstract class Sender {
439 protected short mSrcEchoClassCode;
440 protected byte mSrcEchoInstanceCode;
441 protected short mDstEchoClassCode;
442 protected byte mDstEchoInstanceCode;
443 protected String mDstEchoAddress;
447 public Sender(
short srcEchoClassCode, byte srcEchoInstanceCode
448 ,
short dstEchoClassCode, byte dstEchoInstanceCode
449 , String dstEchoAddress, byte esv) {
450 mSrcEchoClassCode = srcEchoClassCode;
451 mSrcEchoInstanceCode = srcEchoInstanceCode;
452 mDstEchoClassCode = dstEchoClassCode;
453 mDstEchoInstanceCode = dstEchoInstanceCode;
454 mDstEchoAddress = dstEchoAddress;
508 abstract EchoFrame send() throws IOException ;
509 public
void send(EchoFrame frame) throws IOException {
510 short tid = EchoSocket.nextTID();
512 EchoSocket.sendUDPFrame(frame);
514 abstract EchoFrame sendTCP() throws IOException ;
515 public
void sendTCP(EchoFrame frame) throws IOException {
516 short tid = EchoSocket.nextTID();
518 EchoSocket.sendTCPFrame(frame);
521 public void setSeoj(
short srcEchoClassCode, byte srcEchoInstanceCode) {
522 mSrcEchoClassCode = srcEchoClassCode;
523 mSrcEchoInstanceCode = srcEchoInstanceCode;
527 public void setDeoj(
short dstEchoClassCode, byte dstEchoInstanceCode
528 , String dstEchoAddress) {
529 mDstEchoClassCode = dstEchoClassCode;
530 mDstEchoInstanceCode = dstEchoInstanceCode;
531 mDstEchoAddress = dstEchoAddress;
536 public static class Setter
extends Sender {
539 protected ArrayList<EchoProperty> mPropertyList;
540 public Setter(
short dstEchoClassCode, byte dstEchoInstanceCode
541 , String dstEchoAddress,
boolean responseRequired) {
542 super(NodeProfile.ECHO_CLASS_CODE
544 , dstEchoClassCode, dstEchoInstanceCode, dstEchoAddress
546 mPropertyList =
new ArrayList<EchoProperty>();
550 public EchoFrame send() throws IOException {
552 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
553 , mDstEchoClassCode, mDstEchoInstanceCode
554 , mDstEchoAddress, mESV);
556 for(EchoProperty p : mPropertyList) {
557 frame.addProperty(p);
564 public EchoFrame sendTCP() throws IOException {
566 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
567 , mDstEchoClassCode, mDstEchoInstanceCode
568 , mDstEchoAddress, mESV);
570 for(EchoProperty p : mPropertyList) {
571 frame.addProperty(p);
577 public Setter reqSetProperty(byte epc, byte[] edt) {
578 mPropertyList.add(
new EchoProperty(epc, edt));
585 public static class Getter
extends Sender {
586 protected ArrayList<Byte> mEPCList;
590 public Getter(
short dstEchoClassCode, byte dstEchoInstanceCode
591 , String dstEchoAddress) {
592 super(NodeProfile.ECHO_CLASS_CODE
594 , dstEchoClassCode, dstEchoInstanceCode, dstEchoAddress
596 mEPCList =
new ArrayList<Byte>();
600 public EchoFrame send() throws IOException {
602 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
603 , mDstEchoClassCode, mDstEchoInstanceCode
604 , mDstEchoAddress, mESV);
607 for(Byte epc : mEPCList) {
608 frame.addProperty(
new EchoProperty(epc));
615 public EchoFrame sendTCP() throws IOException {
617 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
618 , mDstEchoClassCode, mDstEchoInstanceCode
619 , mDstEchoAddress, mESV);
622 for(Byte epc : mEPCList) {
623 frame.addProperty(
new EchoProperty(epc));
629 public Getter reqGetProperty(byte epc) {
637 public static class Informer
extends Sender {
638 protected ArrayList<Byte> mEPCList;
640 public Informer(
short echoClassCode, byte echoInstanceCode
643 isSelfObject ? echoClassCode : NodeProfile.ECHO_CLASS_CODE
644 , isSelfObject ? echoInstanceCode : Echo.getSelfNode().getNodeProfile().
getInstanceCode()
645 , isSelfObject ? NodeProfile.ECHO_CLASS_CODE : echoClassCode
646 , isSelfObject ? NodeProfile.INSTANCE_CODE : echoInstanceCode
649 mEPCList =
new ArrayList<Byte>();
653 public EchoFrame send() throws IOException {
655 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
656 , mDstEchoClassCode, mDstEchoInstanceCode
657 , mDstEchoAddress, mESV);
659 if(mESV == EchoFrame.ESV_INF_REQ) {
660 for(Byte epc : mEPCList) {
661 frame.addProperty(
new EchoProperty(epc));
664 EchoNode node = Echo.getSelfNode();
665 EchoObject seoj = node.getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode);
666 if(seoj.get() == null) {
669 for(Byte epc : mEPCList) {
670 byte[] edt = seoj.getProperty(epc);
673 EchoProperty
property =
new EchoProperty(epc, edt);
675 if(seoj.isValidProperty(property)) {
676 frame.addProperty(property);
686 public Informer reqInformProperty(byte epc) {
692 public EchoFrame sendTCP() throws IOException {
694 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
695 , mDstEchoClassCode, mDstEchoInstanceCode
696 , mDstEchoAddress, mESV);
698 if(mESV == EchoFrame.ESV_INF_REQ) {
699 for(Byte epc : mEPCList) {
700 frame.addProperty(
new EchoProperty(epc));
703 EchoNode node = Echo.getSelfNode();
707 EchoObject seoj = node.getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode);
708 if(seoj.get() == null) {
711 for(Byte epc : mEPCList) {
712 byte[] edt = seoj.getProperty(epc);
715 EchoProperty
property =
new EchoProperty(epc, edt);
717 if(seoj.isValidProperty(property)) {
718 frame.addProperty(property);
729 public static class InformerC
extends Sender {
730 protected ArrayList<Byte> mEPCList;
732 public InformerC(
short srcEchoClassCode, byte srcEchoInstanceCode
733 , String dstEchoAddress) {
734 super(NodeProfile.ECHO_CLASS_CODE
738 mEPCList =
new ArrayList<Byte>();
742 public EchoFrame send() throws IOException {
744 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
745 , mDstEchoClassCode, mDstEchoInstanceCode
746 , mDstEchoAddress, mESV);
748 EchoNode node = Echo.getSelfNode();
752 EchoObject seoj = node.getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode);
753 if(seoj.get() == null) {
756 for(Byte epc : mEPCList) {
757 byte[] edt = seoj.getProperty(epc);
760 EchoProperty
property =
new EchoProperty(epc, edt);
762 if(seoj.isValidProperty(property)) {
763 frame.addProperty(property);
772 public InformerC reqInformProperty(byte epc) {
778 public EchoFrame sendTCP() throws IOException {
779 EchoFrame frame =
new EchoFrame(mSrcEchoClassCode, mSrcEchoInstanceCode
780 , mDstEchoClassCode, mDstEchoInstanceCode
781 , mDstEchoAddress, mESV);
783 EchoNode node = Echo.getSelfNode();
787 EchoObject seoj = node.getInstance(mSrcEchoClassCode, mSrcEchoInstanceCode);
788 if(seoj.get() == null) {
791 for(Byte epc : mEPCList) {
792 byte[] edt = seoj.getProperty(epc);
795 EchoProperty
property =
new EchoProperty(epc, edt);
797 if(seoj.isValidProperty(property)) {
798 frame.addProperty(property);
final Receiver getReceiver()
final byte[] getGetProperties()
short getDstEchoClassCode()
static final byte ESV_SET_GET_SNA
static final short ECHO_CLASS_CODE
static final byte INSTANCE_CODE
synchronized byte[] getProperty(byte epc)
synchronized boolean setProperty(EchoProperty property)
final byte[] getStatusChangeAnnouncementProperties()
final void removeSetProperty(byte epc)
final void setNode(EchoNode node)
final void setReceiver(Receiver receiver)
final void addStatusChangeAnnouncementProperty(byte epc)
final void addGetProperty(byte epc)
static final byte ESV_SET_NO_RES
InformerC informC(String address)
final void addSetProperty(byte epc)
List< EchoProperty > getPropertyList()
final void clearGetProperties()
Informer inform(boolean multicast)
static final byte ESV_INFC_RES
final byte getClassGroupCode()
static final byte ESV_INFC
final byte getClassCode()
short getSrcEchoClassCode()
abstract byte getInstanceCode()
static final byte ESV_INF
String getSrcEchoAddress()
final void removeGetProperty(byte epc)
static final byte ESV_SETI
final void removeStatusChangeAnnouncementProperty(byte epc)
static final byte ESV_GET_SNA
static final byte ESV_GET_RES
final EchoFrame onReceiveRequest(EchoFrame frame)
final void clearStatusChangeAnnouncementProperties()
static final byte ESV_GET
Setter set(boolean responseRequired)
final void onReceiveGetRequest(byte epc, EchoFrame response)
final void clearSetProperties()
final byte[] getSetProperties()
static final String MULTICAST_ADDRESS
static final byte ESV_SET_RES
final int getEchoObjectCode()
static final byte ESV_INF_REQ
final boolean isSelfObject()
final void onReceiveSetRequest(EchoProperty property, EchoFrame response)
abstract short getEchoClassCode()
synchronized boolean isValidProperty(EchoProperty property)