masonclient.java

来自「使用eclipse集成GT4插件环境」· Java 代码 · 共 299 行

JAVA
299
字号
package com.buu.grid.hzk.mason.client;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import java.rmi.RemoteException;

import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import javax.xml.rpc.ServiceException;

import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.apache.axis.types.URI.MalformedURIException;

import com.buu.grid.hzk.mason.impl.*;
import com.buu.grid.hzk.mason.masonService.stubs.*;
import com.buu.grid.hzk.mason.masonService.stubs.bindings.*;
import com.buu.grid.hzk.mason.masonService.stubs.service.*;

import de.fb12.utils.beanutils.Beanutils;

import org.oasis.wsrf.lifetime.Destroy;
import org.oasis.wsrf.properties.GetResourcePropertyResponse;
import org.oasis.wsrf.properties.InvalidResourcePropertyQNameFaultType;
import org.oasis.wsrf.properties.ResourceUnknownFaultType;
import org.oasis.wsrf.lifetime.ResourceNotDestroyedFaultType;

import org.globus.wsrf.encoding.DeserializationException;
import org.globus.wsrf.encoding.ObjectDeserializer;
import org.globus.wsrf.encoding.ObjectSerializer;
import org.globus.wsrf.encoding.SerializationException;

import org.globus.axis.util.Util;

import org.xml.sax.InputSource;

public class MasonClient implements MasonNamespaces {

	static {
		Util.registerTransport();
	}

	private MasonPortType port = null;

	String CLIENT_DESC = "Mason/etc/client-security-descriptor.xml";

	private EndpointReferenceType instanceEPR;

	private String factoryURI = "http://127.0.0.1:8080/wsrf/services/MasonFactoryService";

	private MasonFactoryPortType factoryPort = null;

	public MasonPortType getPort(String factoryURI, String instanceURI,
			boolean useSecurity) throws MalformedURIException,
			ServiceException, RemoteException {
		MasonFactoryServiceAddressingLocator factoryLocator = new MasonFactoryServiceAddressingLocator();
		EndpointReferenceType factoryEPR;
		MasonServiceAddressingLocator locator = new MasonServiceAddressingLocator();
		factoryEPR = new EndpointReferenceType();
		factoryEPR.setAddress(new Address(factoryURI));
		factoryPort = factoryLocator.getMasonFactoryPortTypePort(factoryEPR);
		if (useSecurity) {
			((javax.xml.rpc.Stub) factoryPort)._setProperty(
					org.globus.wsrf.security.Constants.CLIENT_DESCRIPTOR_FILE,
					CLIENT_DESC);

		}
		CreateResourceResponse createResponse = factoryPort
				.createResource(new CreateResource());
		instanceEPR = createResponse.getEndpointReference();
		MasonPortType port = locator.getMasonPortTypePort(instanceEPR);
		if (useSecurity) {
			((javax.xml.rpc.Stub) port)._setProperty(
					org.globus.wsrf.security.Constants.CLIENT_DESCRIPTOR_FILE,
					CLIENT_DESC);

		}
		return port;
	}

	public MasonClient(String factoryURI, String instanceURI,
			boolean useSecurity) throws MalformedURIException, RemoteException,
			ServiceException {
		this.port = getPort(factoryURI, instanceURI, useSecurity);
		this.factoryURI = factoryURI;
	}

	public MasonClient(EndpointReferenceType epr, boolean useSecurity)
			throws ServiceException {
		MasonServiceAddressingLocator locator = new MasonServiceAddressingLocator();
		// Get instance PortType
		port = locator.getMasonPortTypePort(epr);
		// save the EPR
		instanceEPR = epr;
		if (useSecurity) {
			((javax.xml.rpc.Stub) port)._setProperty(
					org.globus.wsrf.security.Constants.CLIENT_DESCRIPTOR_FILE,
					CLIENT_DESC);

		}
	}

	/**
	 * 
	 * @generated
	 */
	public java.lang.Integer getA() throws RemoteException  {
		
		java.lang.Integer retVal=  this.port.getA(new GetA()).getGetAReturn();
		return retVal;
	
	}
	
	
	/**
	 * 
	 * @generated
	 */
	public void setA(java.lang.Integer a) throws RemoteException  {
	 this.port.setA(new SetA(a));
	}
	
	
	/**
	 * 
	 * @generated
	 */
	public void addA(java.lang.Integer a) throws RemoteException  {
	 this.port.addA(new AddA(a));
	}
	
	
	/**
	 * 
	 * @generated
	 */
	public void testlucas(java.lang.Integer p) throws RemoteException  {
	 this.port.testlucas(new Testlucas(p));
	}
	
	
	/**
	 * 
	 * @generated
	 */
	public java.lang.Integer getF() throws RemoteException  {
		
		java.lang.Integer retVal=  this.port.getF(new GetF()).getGetFReturn();
		return retVal;
	
	}
	
	
	/**
	 * 
	 * @generated
	 */
	public void setF(java.lang.Integer f) throws RemoteException  {
	 this.port.setF(new SetF(f));
	}
	
	


  

	/**
	 * @generated
	 */
	public org.oasis.wsrf.properties.GetResourcePropertyResponse getResourceProperty(javax.xml.namespace.QName getResourcePropertyRequest)
			throws org.oasis.wsrf.properties.InvalidResourcePropertyQNameFaultType,
			org.oasis.wsrf.properties.ResourceUnknownFaultType, RemoteException
              {
                return this.port.getResourceProperty(getResourcePropertyRequest);
              }


	public void writeEPRtoFile(String eprFilename)
			throws SerializationException, IOException {
		String endpointString = ObjectSerializer.toString(instanceEPR,
				MasonNamespaces.RESOURCE_REFERENCE);
		FileWriter fileWriter = new FileWriter(eprFilename);
		BufferedWriter bfWriter = new BufferedWriter(fileWriter);
		bfWriter.write(endpointString);
		bfWriter.close();
	}

	public static EndpointReferenceType readEPRfromFile(String eprFilename)
			throws DeserializationException, IOException {
		FileInputStream fis = new FileInputStream(eprFilename);
		EndpointReferenceType epr = (EndpointReferenceType) ObjectDeserializer
				.deserialize(new InputSource(fis), EndpointReferenceType.class);
		fis.close();
		return epr;
	}
	
	/*
	 *素数的筛法 
	 */
	public int[] Eratosthenes(int p) {
        int[] arr = new int[p];
        int q;
        int i;
        int j;
        int n = p;
        arr[1] = 0;
        arr[0] = 0;
        for (i = 2; i < arr.length; i++) {
            arr[i] = 1;
        }
        q = 2;
        while (q * q < n) {
            j = q * q;
            while (j < n) {
                arr[j] = 0;
                j = j + q;
            }
            do {
                q = q + 1;
            } while (arr[q] != 1);
        }

        int k = 0;
        for (i = 0; i < n; i++) {
            if (arr[i] == 1) {
                k++;
            }
        }
        int[] result = new int[k];
        int t = 0;
        for (i = 0; i < n; i++) {
            if (arr[i] == 1) {
                result[t] = i;
                t++;
            }
        }
        return result;
    }
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		System.out.println("请输入所求梅森素数的范围,上限n:");
		int p = cin.nextInt();

		System.out.println("请输入服务器的ip地址:");
		String ip = cin.next();

		String instanceURI = "http://" + ip + ":8080/wsrf/services/MasonService";
		String factoryURI = "http://" + ip
				+ ":8080/wsrf/services/MasonFactoryService";
		
		System.out.println("instanceURI:"+instanceURI);
		System.out.println("factoryURI:"+factoryURI);
		
		

		boolean useSecurity = false;

		try {
			MasonClient client = new MasonClient(factoryURI, instanceURI,
					useSecurity);
			int[] result=client.Eratosthenes(p);
			for(int i=0;i<result.length;i++)
			{
				System.out.print(result[i]+", ");
			}
			System.out.println();

			long sum_start=System.nanoTime();
			for (int i = 0; i < result.length; i++) {
				client.testlucas(result[i]);
				int f = client.getF();

				if (f == 0) {
					BigInteger Mp = ((BigInteger.valueOf(2)).pow(result[i]))
							.subtract(BigInteger.valueOf(1));
					System.out.println(result[i]+ "为梅森素数!");
					System.out.println("对应的梅森素数为:" + Mp);
				}
			}
			long sum_end=System.nanoTime();
			System.out.println("花费的总时间为:"+(sum_end-sum_start)/1000000+"毫秒");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

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