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

📄 mainframe.java

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import javax.swing.*;

import timeServer.TimeProduct;
import client.tools.Tools;
import dnsServer.DnsServerService;

public class MainFrame extends JFrame {

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

	private JMenuItem mnuServerInfo;

	private ClockPanel clock;

	private boolean runFlag;

	private TimeProduct timeProduct;

	private String dnsHost = "127.0.0.1";

	private String serviceHost = "127.0.0.1";

	private String rmiObjName;

	private int dnsPort = 1099;

	private int timerPort = 1100;

	private int lags = 1;

	private Thread thread;

	public MainFrame() {
		super("客户端");
		clock = new ClockPanel();
		runFlag = true;
		setMenus();
		getContentPane().add(clock);
		setSize(400, 400);
		Tools.center(this);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void setMenus() {
		JMenuBar mnuBar = new JMenuBar();

		JMenu mnuFile = new JMenu("File");

		mnuFile.setMnemonic('F');
		mnuServerInfo = new JMenuItem("查询服务");
		mnuServerInfo.addActionListener(new MnuServerInfoL());
		mnuServerInfo.setMnemonic('S');
		JMenuItem mnuExit = new JMenuItem("退出");
		mnuExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnuExit.setMnemonic('X');
		mnuFile.add(mnuServerInfo);
		mnuFile.add(mnuExit);

		mnuBar.add(mnuFile);

		setJMenuBar(mnuBar);
	}

	private void getTimeProduct(String url, int port, String serviceName) {

		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}

		// 地址字符串格式

		String ServiceURL = "rmi://" + url + ":" + port + "/" + serviceName;
		try {
			// 发现服务,得到远程对象
			timeProduct = (TimeProduct) Naming.lookup(ServiceURL);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(MainFrame.this, "服务调用失败", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			// e.printStackTrace();
		}
	}

	class UpdateTimeThread extends Thread {
		public UpdateTimeThread() {

		}

		public void run() {
			if (timeProduct == null)
				return;
			int count = 0;
			while (runFlag) {
				try {
					long time = timeProduct.getTime();
					//if (count == lags) {
						clock.update(time);
						count=0;
					//} else {
					//	count++;
					//}
				} catch (RemoteException e) {
					e.printStackTrace();
					// 当前服务出错,到目录服务器重新查询新的服务,并反馈这次执行情况
					lookupServiceAndGetTimer1();
				}
				try {
					sleep(1000*lags);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	class MnuServerInfoL implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			ServerInfoInputDialog dialog = new ServerInfoInputDialog(
					MainFrame.this);
			if (dialog.getOption() == ServerInfoInputDialog.CANCEL_OPTION)
				return;
			dnsHost = dialog.getHostname();
			dnsPort = dialog.getPort();
			timerPort = dialog.getPort1();
			rmiObjName = dialog.getRmiObjName();
			lags = dialog.getLags();
			runFlag = false;
			if (thread != null) {
				try {
					thread.join();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
			// 到目录服务器上查询所需服务的时间服务器地址
			lookupServiceAndGetTimer();
			// serviceHost="172.16.64.235";
			// lookupServiceAndGetTimer1();
			runFlag = true;
			thread = new UpdateTimeThread();
			thread.start();
		}
	}

	private void lookupServiceAndGetTimer() {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}

		// 得到目录服务器的提供的查询服务接口
		String ServiceURL = "rmi://" + dnsHost + ":" + dnsPort
				+ "/DnsServerService";
		try {
			// 发现服务,得到远程对象
			DnsServerService dnsServerService;
			dnsServerService = (DnsServerService) Naming.lookup(ServiceURL);
			serviceHost = dnsServerService.lookupService(rmiObjName);
			if (serviceHost == null) {
				JOptionPane.showMessageDialog(MainFrame.this, "没有满足要求的服务",
						"Info", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			getTimeProduct(serviceHost, timerPort, rmiObjName);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(MainFrame.this, "查询失败", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			// e.printStackTrace();
		}
	}

	private void lookupServiceAndGetTimer1() {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}

		// 得到目录服务器的提供的查询服务接口
		String ServiceURL = "rmi://" + dnsHost + ":" + dnsPort
				+ "/DnsServerService";
		try {
			// 发现服务,得到远程对象

			DnsServerService dnsServerService;
			dnsServerService = (DnsServerService) Naming.lookup(ServiceURL);
			serviceHost = dnsServerService.lookupService(rmiObjName,
					serviceHost);
			getTimeProduct(serviceHost, timerPort, rmiObjName);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(MainFrame.this, "再次查询失败", "ERROR",
					JOptionPane.ERROR_MESSAGE);
			// e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// System.setProperty("java.security.policy", "client.policy");
		// System.setSecurityManager(new RMISecurityManager());
		new MainFrame();
	}
}

⌨️ 快捷键说明

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