1 //******************************************************************************
2 //
3 // File: Proxy.java
4 // Package: edu.rit.pj.cluster
5 // Unit: Class edu.rit.pj.cluster.Proxy
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.IOException;
43
44 import edu.rit.mp.Buf;
45 import edu.rit.mp.Channel;
46 import edu.rit.mp.ChannelGroup;
47 import edu.rit.mp.ObjectBuf;
48 import edu.rit.mp.Status;
49
50 /**
51 * Class Proxy is the abstract base class for a proxy object for sending
52 * messages to a PJ process.
53 *
54 * @author Alan Kaminsky
55 * @version 20-Nov-2006
56 */
57 public abstract class Proxy {
58
59 // Hidden data members.
60 private ChannelGroup myChannelGroup;
61 private Channel myChannel;
62
63 // Exported constructors.
64 /**
65 * Construct a new proxy. The proxy will use the given channel in the given
66 * channel group to send messages to the far end process.
67 *
68 * @param theChannelGroup Channel group.
69 * @param theChannel Channel.
70 */
71 public Proxy(ChannelGroup theChannelGroup,
72 Channel theChannel) {
73 myChannelGroup = theChannelGroup;
74 myChannel = theChannel;
75 }
76
77 // Exported operations.
78 /**
79 * Send the given message to this proxy's far end process.
80 *
81 * @param theMessage Message.
82 * @exception IOException Thrown if an I/O error occurred.
83 * @throws java.io.IOException if any.
84 */
85 public void send(Message theMessage)
86 throws IOException {
87 myChannelGroup.send(myChannel,
88 theMessage.getTag(),
89 ObjectBuf.buffer(theMessage));
90 }
91
92 /**
93 * Send a message with the given tag and items to this proxy's far end
94 * process.
95 *
96 * @param theTag Message tag.
97 * @param theSrc Item source buffer.
98 * @exception IOException Thrown if an I/O error occurred.
99 * @throws java.io.IOException if any.
100 */
101 public void send(int theTag,
102 Buf theSrc)
103 throws IOException {
104 myChannelGroup.send(myChannel, theTag, theSrc);
105 }
106
107 /**
108 * Receive a message from this proxy's far end process. A message will be
109 * received from this proxy's channel in this proxy's channel group. The
110 * message must have a tag of 0. The message items are stored in the given
111 * item destination buffer.
112 * <P>
113 * The <code>receive()</code> method does not return until the message has been
114 * fully received.
115 *
116 * @param theDst Item destination buffer.
117 * @return Status object giving the outcome of the message reception.
118 * @exception IOException Thrown if an I/O error occurred.
119 * @throws java.io.IOException if any.
120 */
121 public Status receive(Buf theDst)
122 throws IOException {
123 return myChannelGroup.receive(myChannel, theDst);
124 }
125
126 /**
127 * Receive a message with the given tag from this proxy's far end process. A
128 * message will be received from this proxy's channel in this proxy's
129 * channel group. If <code>theTag</code> is null, a message will be received
130 * with any tag. The message items are stored in the given item destination
131 * buffer.
132 * <P>
133 * The <code>receive()</code> method does not return until the message has been
134 * fully received.
135 *
136 * @param theTag Message tag, or null to receive any tag.
137 * @param theDst Item destination buffer.
138 * @return Status object giving the outcome of the message reception.
139 * @exception IOException Thrown if an I/O error occurred.
140 * @throws java.io.IOException if any.
141 */
142 public Status receive(Integer theTag,
143 Buf theDst)
144 throws IOException {
145 return myChannelGroup.receive(myChannel, theTag, theDst);
146 }
147
148 /**
149 * Close communication with this proxy's far end process.
150 */
151 public void close() {
152 myChannel.close();
153 }
154
155 }