⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverframe.java

📁 实现了一个由时间服务器
💻 JAVA
字号:
package timeServer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import dnsServer.DnsServerService;

public class ServerFrame extends JFrame {

	private JTextField dnsServerUrl;

	private JTextField regServiceName;

	private JFormattedTextField txtPort;

	private JFormattedTextField txtPort1;

	public static final int OK_OPTION = 0;

	public static final int CANCEL_OPTION = 1;

	JTextArea outputText = null;

	JScrollPane jScrollPane = null;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1338871871345052085L;

	public ServerFrame() {
		setTitle("时间服务器");

		init();

		setSize(400, 550);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		Dimension frameSize = this.getSize();
		int x = (screenWidth - frameSize.width) / 2;
		int y = (screenHeight - frameSize.height) / 2;
		setLocation(x, y);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// 启动注册表
		try {
			LocateRegistry.createRegistry(Integer.valueOf(txtPort1.getText()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	private void init() {
		dnsServerUrl = new JTextField(20);
		regServiceName = new JTextField(20);
		NumberFormat nf = NumberFormat.getIntegerInstance();
		nf.setGroupingUsed(false);
		txtPort = new JFormattedTextField(nf);
		txtPort.setValue(1099);
		txtPort.setColumns(20);
		txtPort1 = new JFormattedTextField(nf);
		txtPort1.setValue(1100);
		txtPort1.setColumns(20);
		JButton btnOk = new JButton("注册/启动");
		btnOk.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				outputText.append("向目录服务器注册自己提供的服务,并启动该服务...\n");

				// 向目录服务器dnsServer注册服务
				InetAddress ip;
				String url;
				try {
					ip = InetAddress.getLocalHost();
					url = ip.getHostAddress();
					if (registerService(dnsServerUrl.getText(), txtPort
							.getText(), regServiceName.getText())
							&& startService(url, txtPort1.getText(),
									regServiceName.getText())) {

						outputText.append("服务注册成功并启动...\n");
					} else {
						outputText.append("服务注册和启动失败...\n");
					}
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
					outputText.append("服务注册失败...\n");
				}
			}
		});

		JButton btnCancel = new JButton("注销/关闭");
		btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				outputText.append("向目录服务器注销自己提供的服务,并关闭该服务...\n");
				// 添加向dnsServer注销服务部分

				InetAddress ip;
				String url;
				try {
					ip = InetAddress.getLocalHost();
					url = ip.getHostAddress();
					if (logoutService(dnsServerUrl.getText(),
							txtPort.getText(), regServiceName.getText())
							&& stopService(url, txtPort1.getText(),
									regServiceName.getText())) {

						outputText.append("服务注销成功并关闭...\n");
					} else {
						outputText.append("服务注销失败...\n");
					}
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
					outputText.append("服务注销失败...\n");
				}
			}
		});
		JPanel panel = new JPanel();
		panel.add(btnOk);
		panel.add(btnCancel);
		JPanel inputPanel = new JPanel();

		Container c = getContentPane();

		GridBagLayout gbLayout = new GridBagLayout();
		GridBagConstraints gbCon = new GridBagConstraints();
		gbCon.fill = GridBagConstraints.HORIZONTAL;
		c.setLayout(gbLayout);

		inputPanel.setLayout(gbLayout);
		Tools.addComponent(inputPanel, 0, 0, 1, 1, gbCon,
				new JLabel("目录服务器地址:"));
		Tools.addComponent(inputPanel, 1, 0, 2, 1, gbCon, dnsServerUrl);

		Tools.addComponent(inputPanel, 0, 1, 1, 1, gbCon, new JLabel(
				"目录服务器端口号:"));
		Tools.addComponent(inputPanel, 1, 1, 2, 1, gbCon, txtPort);

		Tools.addComponent(inputPanel, 0, 2, 1, 1, gbCon, new JLabel(
				"时间服务器端口号:"));
		Tools.addComponent(inputPanel, 1, 2, 2, 1, gbCon, txtPort1);

		Tools.addComponent(inputPanel, 0, 3, 1, 1, gbCon, new JLabel(
				"注册或注销服务名:"));
		Tools.addComponent(inputPanel, 1, 3, 2, 1, gbCon, regServiceName);
		Tools.addComponent(inputPanel, 0, 4, 3, 1, gbCon, panel);

		Container container = this.getContentPane();
		BorderLayout bd = new BorderLayout();
		container.setLayout(bd);
		container.add(inputPanel, BorderLayout.NORTH);

		container.add(getJScrollPane(), BorderLayout.CENTER);
		outputText.append("服务器信息:\n时间服务器界面启动...\n");
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(outputText = new JTextArea());
			outputText.setFont(new Font("SanSerif", Font.BOLD, 14));
			jScrollPane.setBounds(new java.awt.Rectangle(20, 47, 274, 338));
		}
		return jScrollPane;
	}

	// 向目录服务器注册本地提供的服务
	private boolean logoutService(String dnsUrl, String port, String serviceName) {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}

		// 地址字符串格式
		String ServiceURL = "rmi://" + dnsUrl + ":" + port
				+ "/DnsServerService";
		try {
			// 发现服务,得到远程对象
			DnsServerService dnsServerService;
			dnsServerService = (DnsServerService) Naming.lookup(ServiceURL);
			InetAddress ip;
			String url;
			try {
				ip = InetAddress.getLocalHost();
				url = ip.getHostAddress();
				dnsServerService.logout(url, serviceName);
				return true;
			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			// JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(),
			// "ERROR", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		return false;
	}

	// 向目录服务器注册本地提供的服务
	private boolean registerService(String dnsUrl, String port,
			String serviceName) {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}

		// 地址字符串格式
		String ServiceURL = "rmi://" + dnsUrl + ":" + port
				+ "/DnsServerService";
		try {
			// 发现服务,得到远程对象
			DnsServerService dnsServerService;
			dnsServerService = (DnsServerService) Naming.lookup(ServiceURL);
			InetAddress ip;
			String url;
			try {
				ip = InetAddress.getLocalHost();
				url = ip.getHostAddress();
				dnsServerService.registerService(url, serviceName);
				return true;
			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			// JOptionPane.showMessageDialog(MainFrame.this, e.getMessage(),
			// "ERROR", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		return false;
	}

	// 启动服务
	private boolean startService(String url, String port, String serviceName) {

		String ServiceURL = "rmi://" + url + ":" + port + "/" + serviceName;
		try {
			// 建立服务对象
			TimeProductImpl impl = new TimeProductImpl();
			// 绑定
			Naming.rebind(ServiceURL, impl);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return false;
	}

	// 关闭服务
	private boolean stopService(String url, String port, String serviceName) {
		String ServiceURL = "rmi://" + url + ":" + port + "/" + serviceName;
		try {
			Naming.unbind(ServiceURL);

			return true;
		} catch (NotBoundException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ServerFrame();

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -