001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:48:48 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33634 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ROADnet;
031
032import com.brtt.antelope.Orb;
033import com.brtt.antelope.OrbImagePacket;
034import com.brtt.antelope.OrbRawPacket;
035
036import ptolemy.actor.TypedAtomicActor;
037import ptolemy.actor.TypedIOPort;
038import ptolemy.data.AWTImageToken;
039import ptolemy.data.StringToken;
040import ptolemy.data.expr.Parameter;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045
046/**
047 * This actor connects to an Antelope ORB and collects packets matching the
048 * given sourcename. Packets are reaped from the Orb and unpacked assuming that
049 * they're OrbImagePackets. The actor outputs ImageTokens to Ptolemy.
050 * 
051 * @author Tobin Fricke (tobin@splorg.org), University of California
052 * @version $Id: OrbImageSource.java 33634 2015-08-24 22:48:48Z crawl $
053 * @Pt.ProposedRating Red (tobin)
054 */
055
056public class OrbImageSource extends TypedAtomicActor {
057        /**
058         * @param container
059         *            The container.
060         * @param name
061         *            The name of this actor.
062         * @exception IllegalActionException
063         *                If the actor cannot be contained by the proposed
064         *                container.
065         * @exception NameDuplicationException
066         *                If the container already has an actor with this name.
067         */
068        public OrbImageSource(CompositeEntity container, String name)
069                        throws NameDuplicationException, IllegalActionException {
070                super(container, name);
071
072                output = new TypedIOPort(this, "output", false, true);
073                output.setMultiport(false);
074                output.setTypeEquals(BaseType.OBJECT); // FIXME -- it's an Image
075
076                orbname = new Parameter(this, "orbname");
077                srcname = new Parameter(this, "srcname");
078
079                orbname.setTypeEquals(BaseType.STRING);
080                srcname.setTypeEquals(BaseType.STRING);
081
082        }
083
084        /** Initialize the component and connect to the ORB. */
085
086        public void initialize() throws IllegalActionException {
087                try {
088                        super.initialize();
089                        _orb = new Orb(StringToken.convert(orbname.getToken())
090                                        .stringValue(), "r");
091                        _orb.select(StringToken.convert(srcname.getToken()).stringValue());
092                        _orb.after(0);
093                } catch (Exception e) {
094                        throw new IllegalActionException(this, "Couldn't connect to Orb."
095                                        + " (" + e.getMessage() + ")");
096                }
097
098        }
099
100        /**
101         * Reap one packet from the ORB, unstuff it as an OrbImagePacket, and output
102         * the resulting ImageToken (containing a java.awt.Image object). Note that
103         * this whole actor can be implemented as a composite actor utilizing
104         * ObjectToRecord, RecordDisassembler, and something that forms ImageTokens
105         * from java.awt.Image.
106         */
107
108        public void fire() throws IllegalActionException {
109                super.fire();
110                try {
111                        OrbRawPacket pkt = (OrbRawPacket) (_orb.reap(false));
112                        OrbImagePacket imgPkt = (OrbImagePacket) (OrbImagePacket
113                                        .unstuff(pkt));
114                        output.broadcast(new AWTImageToken(imgPkt.image));
115
116                } catch (Exception e) {
117                        throw new IllegalActionException(this, e.getMessage());
118                }
119        }
120
121        /**
122         * The name of the orb to connect to, in the format "hostname:port". Note
123         * that orbnames.pf-style names are not supported -- you have to use a valid
124         * IP address or resolvable DNS name, and you have to use a numeric port
125         * number.
126         */
127
128        public Parameter orbname;
129
130        /**
131         * The source name to request from the Orb. When this actor is initialized,
132         * orb.select() is called with the value of this parameter.
133         */
134
135        public Parameter srcname;
136
137        /** Samples from incoming waveform packets appear on this port. */
138
139        public TypedIOPort output;
140
141        /**
142         * This is our orb handle. Maybe one day it will also come on an input
143         * channel.
144         */
145
146        private Orb _orb;
147
148}