/* * DataChannelMux.java * * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ package util.comm; import java.util.Vector; /** * This class implements a simple Data Channel multiplexor. Normally, many writers * can write to the same channel, however they must all write the same type otherwise * the clients won't know what type of objects to expect out of the other end. A mux * channel can read from several channels simultaneously and write the output to an * output channel in the form of a MuxItem object. This object contains both the object * written and an identifier of the channel it came in on. */ public class DataChannelMux implements Runnable { Vector scythes = null; // private state variables for instances of the MUX private Thread DCM_TID = null; private String DCM_Channel; private int DCM_Index; private DataChannel DCM_Input; private DataChannel DCM_Output; public DataChannelMux(String inChans[], String outChan) { super(); DCM_Output = DataChannel.getChannel(outChan); scythes = new Vector(); for (int i = 0; i < inChans.length; i++) { DataChannelMux dcm = new DataChannelMux(inChans[i], i, DCM_Output); scythes.addElement(dcm); dcm.start(); } } DataChannelMux(String chanName, int userIndex, DataChannel outChan) { super(); DCM_Channel = chanName; DCM_Index = userIndex; DCM_TID = new Thread(this); DCM_Input = DataChannel.getChannel(chanName, DCM_TID, 8); DCM_Output = outChan; } public boolean addChannel(String chanName, int userIndex) { if (scythes == null) return false; DataChannelMux dcm = new DataChannelMux(chanName, userIndex, DCM_Output); scythes.addElement(dcm); dcm.start(); return true; } public void start() { DCM_TID.start(); } public void run() { if ((DCM_Output == null) || (DCM_Input == null)) return; while (true) { Object x = null; try { x = DCM_Input.getValue(); } catch (DataChannelOverrun e1) { System.out.println("MUX overrun."); } catch (DataChannelShutdown e2) { return; // time to exit. } catch (DataChannelException e3) { } // ignore others MuxItem m = new MuxItem(DCM_Channel, DCM_Index, x); DCM_Output.putValue(m); } } }