package Applications.EventChannelFactory; import org.omg.CosEventChannelAdminImpl.*; import org.omg.CORBA.*; import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Channel extends JPanel implements EventChannelMonitor, ActionListener { private JTextArea textArea = new JTextArea (); private Misc varios = new Misc(); private BorderLayout borderLayout1 = new BorderLayout (); private JButton Aceptar = new JButton ("Aceptar"); public JButton Cancelar = new JButton ("Cancelar"); public JButton Creado = new JButton ("Creado"); private JButton Monitorizar = new JButton ("Monitorizar"); public JTextField Nombre = new JTextField(); private JTextField Tipo = new JTextField(); private JTextField Direccion = new JTextField(); private JTextField Puerto = new JTextField(); private JTextField TiempoVidaEventos = new JTextField(); private JTextField TamañoColas = new JTextField(); private JTextField NumActualEventos = new JTextField(); private JTextField NumTotalEventos = new JTextField(); private JTextField NumPushSuppliers = new JTextField(); private JTextField NumPullSuppliers = new JTextField(); private JTextField NumPushConsumers = new JTextField(); private JTextField NumPullConsumers = new JTextField(); private JPanel panel_1_2 = new JPanel (); private JSplitPane panel_2 = new JSplitPane (JSplitPane.VERTICAL_SPLIT); private JPanel panel_2_1 = (new JPanel () {public Insets getInsets() { return new Insets ( 5, 5, 5, 5 );}} ); private JScrollPane panel_2_2 = new JScrollPane(); private EventChannelServant eventchannelservant; /** Constructor de la clase. No acepta parámetros. */ public Channel () { try { this.setPreferredSize(new Dimension(550, 175)); this.setLayout(borderLayout1); this.add ( panel_2, BorderLayout.CENTER ); this.add ( panel_1_2, BorderLayout.SOUTH ); Aceptar.addActionListener(this); panel_1_2.add ( Aceptar , null ); Monitorizar.setVisible ( false ); Monitorizar.addActionListener(this); panel_1_2.add ( Monitorizar, null ); panel_1_2.add ( Cancelar, null ); panel_2.add ( panel_2_1, JSplitPane.LEFT ); panel_2.setEnabled(false); panel_2_2.getViewport().add ( textArea, null ); panel_2_1.setLayout (new GridLayout(3, 2, 2, 2)); panel_2_1.add (varios.CreaLabelTexto ("Nombre:" , "Nombre del canal." , Nombre , "CANAL" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Tipo:" , "Tipo de canal." , Tipo , "" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Dirección:" , "Dirección del servidor de nombres." , Direccion , "127.0.0.1", true), null); panel_2_1.add (varios.CreaLabelTexto ("Puerto:" , "Puerto de escucha del servidor de nombres" , Puerto , "900" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Vida de los eventos:", "Tiempo ( en segundos ) tras el cual los eventos son eliminados.", TiempoVidaEventos, "60" , true), null); panel_2_1.add (varios.CreaLabelTexto ("Tamaño de las colas:", "Tamaño máximo de las colas de los Proxies." , TamañoColas , "32" , true), null); } catch(Exception e) { varios.PopUpError (this, "Error en clase Channel " + e ); System.out.println ( "Error en clase Channel " + e ); } } private void AmpliaInformacion ( ) { panel_2.setVisible (false); panel_2_1.setLayout (new GridLayout(6, 2, 2, 2)); panel_2_1.add (varios.CreaLabelTexto ("Número de eventos:" , "Número de eventos actualmente en el canal." , NumActualEventos, "0", false), null); panel_2_1.add (varios.CreaLabelTexto ("Número total de eventos:", "Número total de eventos que han pasado por el canal." , NumTotalEventos , "0", false), null); panel_2_1.add (varios.CreaLabelTexto ("Push Suppliers:" , "Número de Push Suppliers actualmente conectados al canal.", NumPushSuppliers, "0", false), null); panel_2_1.add (varios.CreaLabelTexto ("Pull Suppliers:" , "Número de Pull Suppliers actualmente conectados al canal.", NumPullSuppliers, "0", false), null); panel_2_1.add (varios.CreaLabelTexto ("Push Consumers:" , "Número de Push Consumers actualmente conectados al canal.", NumPushConsumers, "0", false), null); panel_2_1.add (varios.CreaLabelTexto ("Pull Consumers:" , "Número de Pull Consumers actualmente conectados al canal.", NumPullConsumers, "0", false), null); panel_2.add ( panel_2_2, JSplitPane.RIGHT ); panel_2.resetToPreferredSizes(); panel_2.setVisible(true); } public void actionPerformed(ActionEvent e) { if ( e.getActionCommand().equals("Aceptar")) { Aceptar ( e ); } else if ( e.getActionCommand().equals("Monitorizar")) { panel_2.setVisible ( !panel_2.isVisible() ); } } private void Aceptar (ActionEvent e) { if ( Nombre.getText().equals ("") ) { varios.PopUpError ( this, "El canal necesita un nombre." ); return; } try { int tamaño_colas = String2int ( TamañoColas.getText() ); if ( tamaño_colas == -1 ) { varios.PopUpError ( this, "El tamaño de la cola debe ser un entero no negativo." ); return; } int tiempo_vida_eventos = String2int ( TiempoVidaEventos.getText() ); if ( tiempo_vida_eventos == -1 ) { varios.PopUpError ( this, "El tiempo de vida de los eventos debe ser un entero no negativo." ); return; } EventChannelParams params = new EventChannelParams ( Nombre.getText() , Tipo.getText() , Direccion.getText() , Puerto.getText() , tamaño_colas , tiempo_vida_eventos , this ); eventchannelservant = new EventChannelServant ( params ); String resultado_conexion; resultado_conexion = eventchannelservant.Conectar (); if ( resultado_conexion == null ) { Mensaje ( INFO, "", "Canal " + Nombre.getText() + " activo."); Aceptar.setVisible(false); Cancelar.setVisible(false); Monitorizar.setVisible(true); Nombre.setEditable(false); Tipo.setEditable(false); Direccion.setEditable(false); Puerto.setEditable(false); TamañoColas.setEditable(false); TiempoVidaEventos.setEditable(false); AmpliaInformacion(); Creado.doClick(); } else { varios.PopUpError ( this, resultado_conexion ); } } catch(Exception ex) { varios.PopUpError (this, "Error en clase Channel " + ex ); System.out.println ( "Error en clase Channel " + ex ); } } public void Destruir () { eventchannelservant.destroy(); } /** Método que transforma un String representado a un número entero en un int. Difiere del método intValue() de la clase Integer en que devuelve -1 tanto si el String no representa a un entero como si éste es negativo. */ private int String2int ( String texto ) { int valor; try { Integer aux = new Integer ( texto ); valor = aux.intValue(); } catch ( Exception e ){ return -1;} if ( valor >= 0 ) return valor; else return -1; } /* Ahora viene la parte en la que se implementa el interfaz Monitor */ public synchronized void Mensaje ( int tipo, String origen, String mensaje ) { if ( tipo == this._ERROR ) mensaje = "Error en " + origen + ". "+ mensaje; textArea.append ( mensaje + "\n" ); } private int mumeroPushSuppliers = 0; private int mumeroPushConsumers = 0; private int mumeroPullSuppliers = 0; private int mumeroPullConsumers = 0; private int numTotalEventos = 0; private int numActualEventos = 0; public void Accion ( int accion ) { switch ( accion ) { case AÑADIDO_ELEMENTO: numTotalEventos++; numActualEventos++; NumActualEventos.setText(""+numActualEventos); NumTotalEventos.setText(""+numTotalEventos); break; case ELIMINADO_ELEMENTO: numActualEventos--; NumActualEventos.setText(""+numActualEventos); break; case CONECTADO_PUSH_SUPPLIER: mumeroPushSuppliers ++; NumPushSuppliers.setText (""+mumeroPushSuppliers); Mensaje (CONEXION, "", "Conexión de un Push Supplier al canal."); break; case CONECTADO_PULL_SUPPLIER: mumeroPullSuppliers ++; NumPullSuppliers.setText (""+mumeroPullSuppliers); Mensaje (CONEXION, "", "Conexión de un Pull Supplier al canal."); break; case CONECTADO_PUSH_CONSUMER: mumeroPushConsumers ++; NumPushConsumers.setText (""+mumeroPushConsumers); Mensaje (CONEXION, "", "Conexión de un Push Consumer al canal."); break; case CONECTADO_PULL_CONSUMER: mumeroPullConsumers ++; NumPullConsumers.setText (""+mumeroPullConsumers); Mensaje (CONEXION, "", "Conexión de un Pull Consumer al canal."); break; case DESCONECTADO_PUSH_SUPPLIER: mumeroPushSuppliers --; NumPushSuppliers.setText (""+mumeroPushSuppliers); Mensaje (DESCONEXION, "", "Desconexión de un Push Supplier del canal."); break; case DESCONECTADO_PULL_SUPPLIER: mumeroPullSuppliers --; NumPullSuppliers.setText (""+mumeroPullSuppliers); Mensaje (DESCONEXION, "", "Desconexión de un Pull Supplier del canal."); break; case DESCONECTADO_PUSH_CONSUMER: mumeroPushConsumers --; NumPushConsumers.setText (""+mumeroPushConsumers); Mensaje (DESCONEXION, "", "Desconexión de un Push Consumer del canal."); break; case DESCONECTADO_PULL_CONSUMER: mumeroPullConsumers --; NumPullConsumers.setText (""+mumeroPullConsumers); Mensaje (DESCONEXION, "", "Desconexión de un Pull Consumer del canal."); break; default: Mensaje (_ERROR, "", "Accion desconocida! " + accion); } } public void insert ( Any data ) { } }