View Javadoc
1   //******************************************************************************
2   //
3   // File:    BackendFileReader.java
4   // Package: edu.rit.pj.cluster
5   // Unit:    Class edu.rit.pj.cluster.BackendFileReader
6   //
7   // This Java source file is copyright (C) 2006 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.pj.cluster;
41  
42  import java.io.File;
43  import java.io.IOException;
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  /**
48   * Class BackendFileReader provides an object that reads sequential files in the
49   * job backend process.
50   * <P>
51   * <I>Note:</I> Class BackendFileReader is not multiple thread safe; it assumes
52   * it is being called by a synchronized method in the job backend.
53   *
54   * @author Alan Kaminsky
55   * @version 20-Nov-2006
56   */
57  public class BackendFileReader {
58  
59  // Exported data members.
60      /**
61       * Input stream for reading from the job frontend's standard input.
62       */
63      public final BackendFileInputStream in;
64  
65  // Hidden data members.
66      private JobFrontendRef myJobFrontend;
67      private JobBackendRef myJobBackend;
68  
69      // Mapping from backend file descriptor to backend file input stream.
70      private Map<Integer, BackendFileInputStream> myInputStreamForBFD
71              = new HashMap<Integer, BackendFileInputStream>();
72  
73      // Mapping from frontend file descriptor to backend file input stream.
74      private Map<Integer, BackendFileInputStream> myInputStreamForFFD
75              = new HashMap<Integer, BackendFileInputStream>();
76  
77      // Next backend file descriptor.
78      private int myNextBFD = 1;
79  
80  // Exported constructors.
81      /**
82       * Construct a new backend file reader.
83       *
84       * @param theJobFrontend Job Frontend.
85       * @param theJobBackend Job Backend.
86       */
87      public BackendFileReader(JobFrontendRef theJobFrontend,
88              JobBackendRef theJobBackend) {
89          myJobFrontend = theJobFrontend;
90          myJobBackend = theJobBackend;
91  
92          // Set up input stream for stdin.
93          in = new BackendFileInputStream(myJobFrontend, myJobBackend, 1);
94          myInputStreamForFFD.put(1, in);
95      }
96  
97  // Exported operations.
98      /**
99       * Open a backend file input stream on the given file.
100      *
101      * @param file File.
102      * @return Backend file input stream.
103      * @exception IOException Thrown if an I/O error occurred.
104      * @throws java.io.IOException if any.
105      */
106     public BackendFileInputStream open(File file)
107             throws IOException {
108         BackendFileInputStream stream = null;
109         int bfd = 0;
110         int ffd = 0;
111 
112         synchronized (this) {
113             stream = new BackendFileInputStream(myJobFrontend, myJobBackend);
114             bfd = myNextBFD++;
115             myInputStreamForBFD.put(bfd, stream);
116         }
117 
118         ffd = stream.open(bfd, file);
119 
120         synchronized (this) {
121             myInputStreamForFFD.put(ffd, stream);
122         }
123         return stream;
124     }
125 
126     /**
127      * Report the result of opening the given input file.
128      *
129      * @param theJobFrontend Job Frontend that is calling this method.
130      * @param bfd Backend file descriptor.
131      * @param ffd Frontend file descriptor if success.
132      * @param exc Null if success, exception if failure.
133      */
134     public void inputFileOpenResult(JobFrontendRef theJobFrontend,
135             int bfd,
136             int ffd,
137             IOException exc) {
138         BackendFileInputStream stream = null;
139         synchronized (this) {
140             stream = myInputStreamForBFD.remove(bfd);
141         }
142         if (stream != null) {
143             stream.putResult(ffd, 0, 0L, exc);
144         }
145     }
146 
147     /**
148      * Report the result of reading the given input file.
149      *
150      * @param theJobFrontend Job Frontend that is calling this method.
151      * @param ffd Frontend file descriptor.
152      * @param len Number of bytes read, or -1 if EOF.
153      * @param exc Null if success, exception if failure.
154      */
155     public void inputFileReadResult(JobFrontendRef theJobFrontend,
156             int ffd,
157             int len,
158             IOException exc) {
159         BackendFileInputStream stream = null;
160         synchronized (this) {
161             stream = myInputStreamForFFD.get(ffd);
162         }
163         if (stream != null) {
164             stream.putResult(ffd, len, 0L, exc);
165         }
166     }
167 
168     /**
169      * Report the result of skipping the given input file.
170      *
171      * @param theJobFrontend Job Frontend that is calling this method.
172      * @param ffd Frontend file descriptor.
173      * @param len Number of bytes skipped.
174      * @param exc Null if success, exception if failure.
175      */
176     public void inputFileSkipResult(JobFrontendRef theJobFrontend,
177             int ffd,
178             long len,
179             IOException exc) {
180         BackendFileInputStream stream = null;
181         synchronized (this) {
182             stream = myInputStreamForFFD.get(ffd);
183         }
184         if (stream != null) {
185             stream.putResult(ffd, 0, len, exc);
186         }
187     }
188 
189     /**
190      * Report the result of closing the given input file.
191      *
192      * @param theJobFrontend Job Frontend that is calling this method.
193      * @param ffd Frontend file descriptor.
194      * @param exc Null if success, exception if failure.
195      */
196     public void inputFileCloseResult(JobFrontendRef theJobFrontend,
197             int ffd,
198             IOException exc) {
199         BackendFileInputStream stream = null;
200         synchronized (this) {
201             stream = myInputStreamForFFD.remove(ffd);
202         }
203         if (stream != null) {
204             stream.putResult(ffd, 0, 0L, exc);
205         }
206     }
207 
208 }