001: // @(#)ReaderWriterEncrypt.java 1.11 "@(#)ReaderWriterEncrypt.java 1.11 99/09/23 Sun Microsystems"
002:
003: package com.sun.portal.netlet.econnection;
004:
005: import java.io.DataOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.io.InterruptedIOException;
009: import java.io.OutputStream;
010:
011: public class ReaderWriterEncrypt extends ReaderWriterCrypt implements
012: MessageConstants, GWRunnable {
013: private long activityTime = System.currentTimeMillis();
014:
015: private int netletKeepAliveInterval = 0;
016: private long bytesTransferred = 0;
017: private String threadName;
018:
019: public ReaderWriterEncrypt(ReaderWriterLock l,
020: InputStream inStream, OutputStream outStream,
021: int netletKeepAliveInterval) {
022: super (l, inStream, outStream);
023: this .netletKeepAliveInterval = netletKeepAliveInterval;
024: }
025:
026: public long getBytesTransferred() {
027: return bytesTransferred;
028: }
029:
030: public void resetTrafficInfo() {
031: bytesTransferred = 0;
032: }
033:
034: public void run() {
035: threadName = Thread.currentThread().getName();
036: DataCipherMsg dMsg = new DataCipherMsg();
037: try {
038: int numBytes = 0;
039: byte[] buffer = new byte[MAXBUFFERSIZE];
040: while (go) {
041: try {
042: // should really read in 8 less bytes, due to cipher pad
043: numBytes = in.read(buffer, 0,
044: (MAXBUFFERSIZE - 4096));
045: if (numBytes > 0) {
046: dMsg.setDataByRef(buffer, numBytes);
047: // JSS changes - implemented atomic methods to avoid JSS
048: // crashes
049: // if (dMsg.writeMsg(out) == 0) {
050: if (rwLock.atomicWriteMsg(dMsg, out) == 0) {
051:
052: sent = true;
053: activityTime = System.currentTimeMillis();
054: }
055: } else if (numBytes == 0) {
056: System.out
057: .println("ReaderWriterEncrypt received 0 bytes");
058: } else {
059: go = false;
060: return;
061: }
062: } catch (InterruptedIOException iioe) {
063: // JSS changes - implemented atomic methods to avoid JSS
064: // crashes
065: // int rc = writeDummyMsg(out);
066: int rc = rwLock.atomicWriteDummyMsg(out);
067: if (rc == -1) {
068: go = false;
069: return;
070: }
071: } catch (Exception e) {
072: System.out
073: .println("ReaderWriterEncrypt caught exception:");
074: e.printStackTrace();
075: go = false;
076: return;
077: }
078: }
079: } finally {
080: // JSS changes - implemeted atomic methods to avoid JSS crashes
081: // dMsg.writeCloseConnectionMsg(out);
082: // stop();
083: rwLock.atomicWriteCloseConnectionMsg(dMsg, out);
084: rwLock.atomicStop(this );
085:
086: rwLock.notifyFinished(this );
087: rwLock = null;
088: }
089: }
090:
091: protected int writeDummyMsg(DataOutputStream out) {
092: try {
093: out.writeByte(VERSION);
094: out.writeShort(DUMMY_MSG);
095: out.writeInt(0);
096: out.writeInt(0);
097: } catch (IOException ioe) {
098: System.out
099: .println("ReaderWriterEncrypt caught exception while sending dummy msg:");
100: ioe.printStackTrace();
101: return -1;
102: }
103: return 0;
104: }
105:
106: /*
107: * Added by Rajesh T for RFE 4492648.
108: * @ returns the time in milliseconds when the last activity took place in
109: * this ReaderWriter
110: *
111: * @see com.sun.portal.netlet.eproxy.NetletGroup.
112: */
113:
114: public long getLastActivityTime() {
115: return activityTime;
116: }
117:
118: //Methods of GWRunnable
119: public String getType() {
120: return RDR_WRITER_ENCRYPT;
121: }
122:
123: public String getThreadName() {
124: return threadName;
125: } //Methods of GWRunnable
126: }
|