当前位置: 首页 > 工具软件 > IceChat > 使用案例 >

ice glacier2 java_coordinator.java 源代码在线查看 - ICE3.3.0--聊天程序服务器端demo 资源下载 虫虫电子下载站...

姜增
2023-12-01

// ********************************************************************** Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. This copy of Chat Demo is licensed to you under the terms// described in the CHAT_DEMO_LICENSE file included in this// distribution. **********************************************************************package ChatDemoGUI;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.Cursor;import java.awt.Component;import java.awt.Dimension;import java.awt.Frame;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.security.cert.CertificateFactory;import java.security.cert.Certificate;import java.io.InputStream;import javax.swing.DefaultListModel;import javax.swing.SwingUtilities;import javax.swing.JOptionPane;import javax.swing.JDialog;import javax.swing.JPopupMenu;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JTextArea;import javax.swing.JButton;import javax.swing.BoxLayout;import javax.swing.text.DefaultEditorKit;import javax.swing.ImageIcon;import com.jgoodies.forms.layout.FormLayout;import com.jgoodies.forms.builder.DefaultFormBuilder;class Coordinator{ public enum ClientState { Disconnected, Connecting, Connected, Disconnecting } class CertificateVerifier implements IceSSL.CertificateVerifier { class AcceptInvalidCertDialog implements Runnable { public boolean show() { try { SwingUtilities.invokeAndWait(this); } catch(Exception ex) { } return _accept; } public void run() { final String msg = "The server certificate does not match the official ZeroC chat server\n" + "certificate, do you want to continue and connect to this chat server?"; _accept = JOptionPane.showConfirmDialog(_mainView, msg, "Warning - Chat Demo", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION; } private boolean _accept = false; } public CertificateVerifier(Certificate caCert) { _caCert = caCert; } public boolean verify(IceSSL.ConnectionInfo info) { try { if(info.certs != null && info.certs.length > 0) { info.certs[0].verify(_caCert.getPublicKey()); return true; } } catch(Exception ex) { return new AcceptInvalidCertDialog().show(); } return false; } final private Certificate _caCert; }; public Coordinator(MainView mainView, LoginView loginView, ChatView chatView, DefaultListModel users, String[] a) { _args = a; _mainView = mainView; _loginView = loginView; _chatView = chatView; _users = users; _shutdownHook = new Thread("Shutdown hook") { public void run() { logout(); } }; Runtime.getRuntime().addShutdownHook(_shutdownHook); } synchronized public void login(LoginInfo info) { String routerEndpoints = "Glacier2/router:ssl -p 4064 -h " + info.host + " -t 10000"; try { Ice.InitializationData initData = new Ice.InitializationData(); initData.properties = Ice.Util.createProperties(); initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory"); initData.properties.setProperty("IceSSL.TrustOnly.Client", "CN=Glacier2"); initData.properties.setProperty("Ice.Default.Router", routerEndpoints); initData.properties.setProperty("Ice.ACM.Client", "0"); initData.properties.setProperty("Ice.RetryIntervals", "-1"); _communicator = Ice.Util.initialize(new Ice.StringSeqHolder(_args), initData); // // Read the CA certificate embedded in the Jar file. // InputStream is = getClass().getResourceAsStream("/certs/zeroc_ca_cert.pem"); CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate caCert = cf.generateCertificate(is); is.close(); // // Install a certificate verifier to ensure the server certificate is signed // by the CA cert that is bundled with the executable. // IceSSL.Plugin plugin = (IceSSL.Plugin)_communicator.getPluginManager().getPlugin("IceSSL"); plugin.setCertificateVerifier(new CertificateVerifier(caCert)); Glacier2.RouterPrx router = Glacier2.RouterPrxHelper.uncheckedCast(_communicator.getDefaultRouter()); Glacier2.SessionPrx s = router.createSession(info.username, info.password); Chat.ChatSessionPrx session = Chat.ChatSessionPrxHelper.uncheckedCast(s); Ice.ObjectAdapter adapter = _communicator.createObjectAdapterWithRouter("ChatDemo.Client", router); Ice.Identity callbackId = new Ice.Identity(); callbackId.name = Ice.Util.generateUUID(); callbackId.category = router.getCategoryForClient(); Chat.ChatRoomCallbackPrx callback = Chat.ChatRoomCallbackPrxHelper.uncheckedCast( adapter.add(new ChatRoomCallbackI(this), callbackId)); session.setCallback(callback); long sessionPingPeriod = router.getSessionTimeout() * 1000 / 2; _session = new Session(this, session, sessionPingPeriod); info.save(); _username = ChatUtils.formatUsername(info.username); SwingUtilities.invokeLater(new Runnable() { public void run() { setState(ClientState.Connected); } }); } catch(final Glacier2.CannotCreateSessionException ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed (Glacier2.CannotCreateSessionException):\n" + ex.reason); } catch(final Glacier2.PermissionDeniedException ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed (Glacier2.PermissionDeniedException):\n" + ex.reason); } catch(Ice.ConnectionRefusedException ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed (Ice.ConnectionRefusedException).\n" + "Please check your server:\n" + "a Glacier2 router should be running on " + info.host + " and\n" + "listening on port 4064, and the server firewall needs\n" + "to be configured to allow TCP connections to this port."); } catch(Ice.TimeoutException ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed (Ice.TimeoutException)\n." + "Please check your server:\n" + "a Glacier2 router should be running on " + info.host + " and\n" + "listening on port 4064, and the server firewall needs\n" + "to be configured to allow TCP connections to this port."); } catch(Ice.SocketException ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed (Ice.SocketException ).\n" + "Please check your server:\n" + "a Glacier2 router should be running on " + info.host + " and\n" + "listening on port 4064, and the server firewall needs\n" + "to be configured to allow TCP connections to this port."); } catch(Ice.DNSException ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed (Ice.DNSException ).\n" + "Please check your DNS configuration:\n" + "Host \"" + info.host + "\" should point to the ip address of a Glacier2 router\n" + "that should be listening on port 4064, and the server firewall needs\n" + "to be configured to allow TCP connections to this port."); } catch(final Exception ex) { if(_communicator != null) { _communicator.destroy(); _communicator = null; } setError("Login failed:\n" + ChatUtils.stack2string(ex)); } } synchronized public void logout() { SwingUtilities.invokeLater(new Runnable() { public void run() { setState(ClientState.Disconnecting); } }); if(_session != null) { _session.close(); _session = null; } try { if(_communicator != null) { _communicator.destroy(); _communicator = null; } } catch(Ice.LocalException e) { } SwingUtilities.invokeLater(new Runnable() { public void run() { setState(ClientState.Disconnected); } }); _username = ""; }

 类似资料: