001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.iso.channel; 020/** 021 * sends back requests, posibly applying filters 022 * @author <a href="mailto:apr@cs.com.uy">Alejandro P. Revilla</a> 023 * @version $Revision$ $Date$ 024 * @since 1.2.2 025 */ 026 027import org.jpos.iso.FilteredBase; 028import org.jpos.iso.ISOException; 029import org.jpos.iso.ISOMsg; 030import org.jpos.iso.ISOPackager; 031import org.jpos.util.*; 032 033import java.io.IOException; 034 035public class LoopbackChannel extends FilteredBase implements LogSource { 036 boolean usable = true; 037 private int[] cnt; 038 String name; 039 BlockingQueue queue; 040 Logger logger; 041 String realm; 042 043 public LoopbackChannel () { 044 super(); 045 cnt = new int[SIZEOF_CNT]; 046 queue = new BlockingQueue(); 047 } 048 049 /** 050 * setPackager is optional on LoopbackChannel, it is 051 * used for debugging/formating purposes only 052 */ 053 public void setPackager(ISOPackager packager) { 054 // N/A 055 } 056 057 public void connect () { 058 cnt[CONNECT]++; 059 usable = true; 060 setChanged(); 061 notifyObservers(); 062 } 063 064 /** 065 * disconnects ISOChannel 066 */ 067 public void disconnect () { 068 usable = false; 069 setChanged(); 070 notifyObservers(); 071 } 072 073 public void reconnect() { 074 usable = true; 075 setChanged(); 076 notifyObservers(); 077 } 078 079 public boolean isConnected() { 080 return usable; 081 } 082 083 public void send (ISOMsg m) 084 throws IOException,ISOException { 085 if (!isConnected()) 086 throw new ISOException ("unconnected ISOChannel"); 087 LogEvent evt = new LogEvent (this, "loopback-send", m); 088 m = applyOutgoingFilters (m, evt); 089 queue.enqueue (m); 090 cnt[TX]++; 091 notifyObservers(); 092 Logger.log (evt); 093 } 094 095 public void send (byte[] b) 096 throws IOException,ISOException { 097 if (!isConnected()) 098 throw new ISOException ("unconnected ISOChannel"); 099 LogEvent evt = new LogEvent (this, "loopback-send", b); 100 queue.enqueue (b); 101 cnt[TX]++; 102 notifyObservers(); 103 Logger.log (evt); 104} 105 106 public ISOMsg receive() throws IOException, ISOException 107 { 108 if (!isConnected()) 109 throw new ISOException ("unconnected ISOChannel"); 110 try { 111 ISOMsg m = (ISOMsg) ((ISOMsg) queue.dequeue()).clone(); 112 LogEvent evt = new LogEvent (this, "loopback-receive", m); 113 m = applyIncomingFilters (m, evt); 114 cnt[RX]++; 115 notifyObservers(); 116 Logger.log (evt); 117 return m; 118 } catch (InterruptedException e) { 119 throw new IOException (e.toString()); 120 } catch (BlockingQueue.Closed e) { 121 throw new IOException (e.toString()); 122 } 123 } 124 125 public void setUsable(boolean usable) { 126 this.usable = usable; 127 setChanged(); 128 notifyObservers(); 129 } 130 131 public int[] getCounters() { 132 return cnt; 133 } 134 135 public void setName (String name) { 136 this.name = name; 137 NameRegistrar.register ("channel."+name, this); 138 } 139 140 public String getName() { 141 return name; 142 } 143 144 public ISOPackager getPackager() { 145 return null; 146 } 147 148 public void resetCounters() { 149 for (int i=0; i<SIZEOF_CNT; i++) 150 cnt[i] = 0; 151 } 152 public void setLogger (Logger logger, String realm) { 153 this.logger = logger; 154 this.realm = realm; 155 } 156 public String getRealm () { 157 return realm; 158 } 159 public Logger getLogger() { 160 return logger; 161 } 162}