001/*
002 * Copyright (c) 2002-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:47:39 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33633 $'
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.srb;
031
032import edu.sdsc.grid.io.srb.SRBFile;
033import edu.sdsc.grid.io.srb.SRBFileSystem;
034import edu.sdsc.grid.io.srb.SRBRandomAccessFile;
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.TypedIOPort;
037import ptolemy.data.ArrayToken;
038import ptolemy.data.BooleanToken;
039import ptolemy.data.ObjectToken;
040import ptolemy.data.StringToken;
041import ptolemy.data.Token;
042import ptolemy.data.UnsignedByteToken;
043import ptolemy.data.type.ArrayType;
044import ptolemy.data.type.BaseType;
045import ptolemy.kernel.CompositeEntity;
046import ptolemy.kernel.util.IllegalActionException;
047import ptolemy.kernel.util.NameDuplicationException;
048
049//////////////////////////////////////////////////////////////////////////
050//// SRBReader
051/**
052 * <p>
053 * SRBReader/StreamGet is a Kepler Actor which has a functionality similar to
054 * the SRB command namely "Sget".However SRBReader actor downloads data from the
055 * SRB remote file with a streaming process as a byte of arrays instead of a
056 * parallel get. The following actor expects as input a reference to the SRB
057 * file system. This reference connection is obtained via the SRBConnect Actor
058 * in Kepler. <i>See SRBConnect and its documentation.</i>
059 * </p>
060 * <p>
061 * The file reference system is created with a unique SRB user account and with
062 * this connection reference as input the SRBReader actor is able to gain access
063 * to the SRB file space. Once an alive SRB file connection system has been
064 * established the actor gets the remote SRB file path and creates a
065 * SRBRandomAccessFile stream. The contents of file are broadcasted as a
066 * sequence of bytes of arrays, after reading from the stream in a loop.
067 * </p>
068 * <p>
069 * <B>Actor Input:</B> Accepts a reference to the SRB files system, an SRB
070 * remote file name as input.
071 * </p>
072 * <p>
073 * <B>Actor Output:</B> The SRBStreamGet actor outputs the content of the SRB
074 * remote file a sequence of byte arrays. Also outputs a endofFile value once
075 * end of file has been reached and no more bytes are to be read.
076 * 
077 * 
078 * </p>
079 * <p>
080 * The following actor accesses SRB file reference system and SRB file space
081 * with the SRB Jargon API provided. The JARGON is a pure API for developing
082 * programs with a data grid interface and I/O for SRB file systems.
083 * </p>
084 * <A href="http://www.sdsc.edu/srb"><I>Further information on SRB</I> </A>
085 * 
086 * @author Bing Zhu and Efrat Jaeger
087 * @version $Id: SRBReader.java 33633 2015-08-24 22:47:39Z crawl $
088 * @since Ptolemy II 3.0.2
089 */
090public class SRBReader extends TypedAtomicActor {
091
092        /**
093         * Construct an actor with the given container and name.
094         * 
095         * @param container
096         *            The container.
097         * @param name
098         *            The name of this actor.
099         * @exception IllegalActionException
100         *                If the actor cannot be contained by the proposed
101         *                container.
102         * @exception NameDuplicationException
103         *                If the container already has an actor with this name.
104         */
105        public SRBReader(CompositeEntity container, String name)
106                        throws NameDuplicationException, IllegalActionException {
107
108                super(container, name);
109
110                SRBFileSystem = new TypedIOPort(this, "SRBFileSystem", true, false);
111                output = new TypedIOPort(this, "output", false, true);
112                remoteFileName = new TypedIOPort(this, "remoteFileName", true, false);
113                endOfFile = new TypedIOPort(this, "endOfFile", false, true);
114
115                // Set the type constraint.
116                SRBFileSystem.setTypeEquals(BaseType.GENERAL);
117                output.setTypeEquals(new ArrayType(BaseType.UNSIGNED_BYTE));
118                remoteFileName.setTypeEquals(BaseType.STRING);
119                endOfFile.setTypeEquals(BaseType.BOOLEAN);
120
121                _attachText("_iconDescription", "<svg>\n"
122                                + "<rect x=\"-25\" y=\"-20\" " + "width=\"68\" height=\"40\" "
123                                + "style=\"fill:white\"/>\n"
124                                + "<polygon points=\"-15,-10 -7,-10 -3,-14 4,-14 8,-10"
125                                + " 33,-10 33,10, -15,10\" " + "style=\"fill:red\"/>\n"
126                                + "<text x=\"-5\" y=\"7\" " + "style=\"font-size:14\">\n"
127                                + "SRB \n" + "</text>\n" + "<text x=\"-20\" y=\"19\""
128                                + "style=\"font-size:11; fill:black; font-family:SansSerif\">"
129                                + "stream read</text>\n" + "</svg>\n");
130
131        }
132
133        // /////////////////////////////////////////////////////////////////
134        // // ports and parameters ////
135
136        /**
137         * Connection reference
138         */
139        public TypedIOPort SRBFileSystem;
140
141        /**
142         * Output. Array of bytes.
143         */
144        public TypedIOPort output;
145
146        /**
147         * The SRB file to be read.
148         */
149        public TypedIOPort remoteFileName;
150
151        /**
152         * End of file when reached, outputs true
153         */
154        public TypedIOPort endOfFile;
155
156        // /////////////////////////////////////////////////////////////////
157        // // public methods ////
158
159        /**
160         * Accepts an SRBFileSystem reference and a file name and outputs the file
161         * content (Array of Bytes).
162         */
163        public void fire() throws IllegalActionException {
164                SRBFile srbFile;
165                SRBRandomAccessFile srbRandomAccessFile = null;
166                byte[] bytesRead = new byte[20000];
167                ;
168                int nBytesRead;
169
170                try {
171                        srbFileSystem = (SRBFileSystem) ((ObjectToken) SRBFileSystem.get(0))
172                                        .getValue();
173                        String _srbFileName = ((StringToken) remoteFileName.get(0))
174                                        .stringValue();
175                        srbFile = new SRBFile(srbFileSystem, _srbFileName);
176                        srbRandomAccessFile = new SRBRandomAccessFile(srbFile, "r");
177
178                        nBytesRead = srbRandomAccessFile.read(bytesRead);
179                        while (nBytesRead > 0) {
180                                Token _bytes[] = new Token[nBytesRead];
181
182                                for (int i = 0; i < nBytesRead; i++) {
183                                        _bytes[i] = new UnsignedByteToken(bytesRead[i]);
184                                }
185                                output.send(0, new ArrayToken(_bytes));
186
187                                nBytesRead = srbRandomAccessFile.read(bytesRead);
188                                if (nBytesRead == 0) {
189                                        endOfFile.broadcast(BooleanToken.TRUE);
190                                } else
191                                        endOfFile.broadcast(BooleanToken.FALSE);
192                        }
193
194                } catch (Exception ex) {
195                        srbFile = null;
196                        srbRandomAccessFile = null;
197                        srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
198                        throw new IllegalActionException(this, ex, "Reading exception.");
199                }
200                srbFile = null;
201                srbRandomAccessFile = null;
202        }
203
204        /**
205         * Post fire the actor. Return false to indicate that the process has
206         * finished.
207         */
208        public boolean postfire() {
209                return false; // FIX ME
210        }
211
212        /**
213         * Disconnect from SRB.
214         */
215        public void wrapup() {
216                srbFileSystem = SRBUtil.closeConnection(srbFileSystem);
217        }
218
219        private SRBFileSystem srbFileSystem = null;
220
221}