1 //****************************************************************************** 2 // 3 // File: LoopbackChannel.java 4 // Package: edu.rit.mp 5 // Unit: Class edu.rit.mp.LoopbackChannel 6 // 7 // This Java source file is copyright (C) 2008 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.mp; 41 42 import java.io.IOException; 43 import java.io.InterruptedIOException; 44 import java.net.InetSocketAddress; 45 46 /** 47 * Class LoopbackChannel provides a channel for sending and receiving messages 48 * within the same process in the Message Protocol (MP). 49 * 50 * @author Alan Kaminsky 51 * @version 13-May-2008 52 */ 53 class LoopbackChannel 54 extends Channel { 55 56 // Hidden constructors. 57 /** 58 * Construct a new loopback channel. 59 * 60 * @param theChannelGroup Enclosing channel group. 61 */ 62 LoopbackChannel(ChannelGroup theChannelGroup) { 63 super(theChannelGroup); 64 } 65 66 // Exported operations. 67 /** 68 * Obtain the channel group ID of this channel's near end channel group. 69 * 70 * @return Near end channel group ID. 71 */ 72 public int nearEndChannelGroupId() { 73 return myChannelGroup.myChannelGroupId; 74 } 75 76 /** 77 * Obtain the channel group ID of this channel's far end channel group. 78 * 79 * @return Far end channel group ID. 80 */ 81 public int farEndChannelGroupId() { 82 return myChannelGroup.myChannelGroupId; 83 } 84 85 /** 86 * Obtain this channel's near end address. This is the host and port of the 87 * near end of this channel's connection. 88 * 89 * @return Near end address. 90 */ 91 public InetSocketAddress nearEndAddress() { 92 return new InetSocketAddress(0); 93 } 94 95 /** 96 * Obtain this channel's far end address. This is the host and port of the 97 * far end of this channel's connection. 98 * 99 * @return Far end address. 100 */ 101 public InetSocketAddress farEndAddress() { 102 return new InetSocketAddress(0); 103 } 104 105 // Hidden operations. 106 /** 107 * Send a message via this channel. The I/O request object must be newly 108 * constructed with the message tag and source buffer fields filled in. This 109 * method is allowed to return immediately and let the message be sent in a 110 * separate thread. The calling thread should use the I/O request object to 111 * wait for the message send to complete. 112 * 113 * @param theIORequest I/O request object. 114 * 115 * @exception IOException Thrown if an I/O error occurred. 116 */ 117 void send(IORequest theIORequest) 118 throws IOException { 119 synchronized (this) { 120 // Check whether channel is closed. 121 if (myWriteState == WRITE_CLOSED) { 122 throw new IOException("edu.rit.mp.LoopbackChannel: Channel closed"); 123 } 124 } 125 126 // Wait until there is a matching receive request. 127 IORequest recvRequest; 128 try { 129 recvRequest = myIORequestList.waitForMatch(theIORequest); 130 } catch (InterruptedException exc) { 131 IOException exc2 132 = new InterruptedIOException("edu.rit.mp.LoopbackChannel: Send interrupted"); 133 exc2.initCause(exc); 134 throw exc2; 135 } 136 137 // Copy source buffer to destination buffer. 138 recvRequest.myBuf.copy(theIORequest.myBuf); 139 140 // Set up status object. 141 recvRequest.myStatus 142 = new Status(this, 143 theIORequest.myTagLb, 144 theIORequest.myBuf.myLength); 145 146 // Report success. 147 theIORequest.reportSuccess(); 148 recvRequest.reportSuccess(); 149 } 150 151 }