/**
* @copyright 2013 Computer Science Department, Recursive InterNetworking Architecture (RINA) laboratory, Boston University.
* All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation
* for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all
* copies and that both the copyright notice and this permission notice appear in supporting documentation.
* The RINA laboratory of the Computer Science Department at Boston University makes no
* representations about the suitability of this software for any purpose.
*/
package rina.object.internal;
import java.util.LinkedList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.protobuf.ByteString;
import rina.object.gpb.SubscriptionEvent_t.eventType_t;
import rina.object.gpb.SubscriptionEvent_t.subscriptionEvent_t;
/**
* This corresponds to SubscriptionEvent.java in google.object.gpb which is created by GPB
* @author Yuefeng Wang. Computer Science Department, Boston University
*
*/
public class SubscriptionEvent {
private Log log = LogFactory.getLog(this.getClass());
public enum EventType { PUB, SUB };
private EventType eventType = null;
private LinkedList<String> atrributeList = null;
// this will be generated by RIB Daemon when RIB Daemon API is called
private int subscriptionID = -1;
//Note: the member list is String type, but in the DIF , address is an integer.
//So for an IPC subscription event, address needs to be casted when used
private LinkedList<String> memberList = null;
private double updatePeriod = -1;
// this is the event value
// read for a sub event
// write for a pub event
private Object subValue = null;
private byte[] pubValue = null;
public SubscriptionEvent(EventType eventType, double updatePeriod, String attribute )
{
this.eventType = eventType;
this.updatePeriod = updatePeriod;
this.atrributeList = new LinkedList<String>();
this.atrributeList.add(attribute);
this.memberList = new LinkedList<String>();
}
public SubscriptionEvent(EventType eventType, double updatePeriod, String attribute, String member )
{
this.eventType = eventType;
this.updatePeriod = updatePeriod;
this.atrributeList = new LinkedList<String>();
this.atrributeList.add(attribute);
this.memberList = new LinkedList<String>();
this.memberList.add(member);
}
public SubscriptionEvent(EventType eventType, double updatePeriod, LinkedList<String> atrributeList , LinkedList<String> memberList )
{
this.eventType = eventType;
this.updatePeriod = updatePeriod;
this.atrributeList = atrributeList;
this.memberList = memberList;
}
public SubscriptionEvent(subscriptionEvent_t event)
{
if(event.getEventType() == eventType_t.PUB)
{
this.eventType = EventType.PUB;
this.pubValue = event.getValue().toByteArray();
}else if(event.getEventType() == eventType_t.SUB)
{
this.eventType = EventType.SUB;
}
this.updatePeriod = event.getUpdatePeriod();
this.atrributeList = new LinkedList<String>();
for(int i = 0; i< event.getAttributeListCount(); i++)
{
this.atrributeList.add(event.getAttributeList(i));
}
this.memberList = new LinkedList<String>();
for(int i = 0; i< event.getMemberListCount(); i++)
{
this.memberList.add(event.getMemberList(i));
}
}
public void print()
{
this.log.debug("eventType/subscriptionID/updatePeriod/attributeList/memberList:" + this.eventType + "/" +
this.subscriptionID + "/" + this.updatePeriod + "/" + this.atrributeList + "/" + this.memberList);
}
/**
* Convert to GPB type
* @return
*/
public subscriptionEvent_t convert()
{
subscriptionEvent_t.Builder event = subscriptionEvent_t.newBuilder();
if(this.eventType == EventType.PUB)
{
event.setEventType(eventType_t.PUB);
}else if (this.eventType == EventType.SUB)
{
event.setEventType(eventType_t.SUB);
}
event.setSubscriptionID(this.subscriptionID);
event.setUpdatePeriod(this.updatePeriod);
for(int i = 0; i< this.atrributeList.size(); i++)
{
event.addAttributeList(this.atrributeList.get(i));
}
for(int i = 0; i< this.memberList.size(); i++)
{
event.addMemberList(this.memberList.get(i));
}
return event.buildPartial();
}
/**
* This is used by the publisher to send to its subscriber
* Here we remove the memberlist since the subscribers don't have to know who are other subscriber
* Only update period, attribute and value are needed.
* @return
*/
public subscriptionEvent_t getSendToSubscribers(String itself)
{
subscriptionEvent_t.Builder event = subscriptionEvent_t.newBuilder();
if (this.eventType == EventType.SUB)
{
this.log.error("This is a sub event, not a publisher event to send to subscribers");
return null;
}else if(this.eventType == EventType.PUB)
{
event.setEventType(eventType_t.PUB);
}
event.setUpdatePeriod(this.updatePeriod);
event.addMemberList(itself);
for(int i = 0; i< this.atrributeList.size(); i++)
{
event.addAttributeList(this.atrributeList.get(i));
}
if(this.getPubValue() == null)
{
this.log.error("Pub event has an empty content to publish. Please check pub event with ID: " + this.subscriptionID);
return null;
}
event.setValue(ByteString.copyFrom(this.getPubValue()));
return event.buildPartial();
}
public subscriptionEvent_t getSendToPublisher(String itself)
{
subscriptionEvent_t.Builder event = subscriptionEvent_t.newBuilder();
if (this.eventType == EventType.PUB)
{
this.log.error("This is a pub event, not a sub event to send to publisher");
return null;
}else if(this.eventType == EventType.SUB)
{
event.setEventType(eventType_t.SUB);
}
event.setUpdatePeriod(this.updatePeriod);
for(int i = 0; i< this.atrributeList.size(); i++)
{
event.addAttributeList(this.atrributeList.get(i)); // actually only one is used
}
event.addMemberList(itself);
return event.buildPartial();
}
public EventType getEventType() {
return eventType;
}
public void setEventType(EventType eventType) {
this.eventType = eventType;
}
public LinkedList<String> getAtrributeList() {
return atrributeList;
}
public void setAtrributeList(LinkedList<String> atrributeList) {
this.atrributeList = atrributeList;
}
public int getSubscriptionID() {
return subscriptionID;
}
public void setSubscriptionID(int subscriptionID) {
this.subscriptionID = subscriptionID;
}
public LinkedList<String> getMemberList() {
return memberList;
}
public void setMemberList(LinkedList<String> memberList) {
this.memberList = memberList;
}
public double getUpdatePeriod() {
return updatePeriod;
}
public void setUpdatePeriod(double updatePeriod) {
this.updatePeriod = updatePeriod;
}
public synchronized Object getSubValue() {
return subValue;
}
public synchronized void setSubValue(Object subValue) {
this.subValue = subValue;
}
public synchronized byte[] getPubValue() {
return pubValue;
}
public synchronized void setPubValue(byte[] pubValue) {
this.pubValue = pubValue;
}
}