Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SerwerRPC {
- /**
- * Metoda licząca sumę kwadratów dwóch liczb.
- * @param x pierwsza liczba
- * @param y druga liczba
- * @return wartość wyrażenia x^2 + y^2
- */
- public Double squareSum(double x, double y) {
- return Math.pow(x, 2) + Math.pow(y, 2);
- }
- /**
- * Metoda licząca wartość podanej funkcji trygonometrycznej dla wskazanego kąta.
- * @param function nazwa funkcji trygonometrycznej
- * @param arg kąt w stopniach
- * @param time czas oczekiwania na wynik
- * @return wartość podanej funkcji trygonometrycznej
- */
- public double getValueAsy(String function, int arg, int time) {
- double radians = arg * Math.PI/180.0;
- double result=0;
- if (function.equals("sin")) {
- result =Math.sin(radians);
- }
- else if (function.equals("cos")) {
- result= Math.cos(radians);
- }
- else if (function.equals("tg") || function.equals("tan")) {
- result=Math.tan(radians);
- }
- else if (function.equals("ctg")) {
- result=1/Math.tan(radians);
- }
- try {
- Thread.sleep(time);
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- Thread.currentThread().interrupt();
- }
- result*=100;
- int tmp = (int) result;
- result = tmp/100.0;
- System.out.println(result);
- return result;
- }
- public String getLiczbyPierwszeAsy(int minRange,int maxRange,int time){
- List<Integer> list = new ArrayList<>();
- StringBuilder sb = new StringBuilder();
- for (int i=minRange;i<=maxRange;i++) {
- if(isPrimeNumber(i)) {
- sb.append(i+",");
- }
- }
- try {
- Thread.sleep(time);
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- Thread.currentThread().interrupt();
- }
- return sb.toString();
- }
- private boolean isPrimeNumber(int liczba)
- {
- if(liczba<2)
- {
- return false;
- }
- for(int i=2; i<=liczba/2; i++)
- {
- if(liczba%i==0)
- {
- return false;
- }
- }
- return true;
- }
- /**
- * Metoda stwierdzająca czy jedna liczba jest wielokrotnością drugiej.
- * @param x pierwsza liczba
- * @param y druga liczba
- * @return prawda jeśli pierwsza liczba jest wielokrotnością drugiej lub fałsz gdy nie jest
- */
- public Boolean isMultiplicity(int x, int y) {
- return x % y == 0;
- }
- /**
- * Metoda wyświetlająca zadeklarowane funkcje w klasie.
- * @return tekst zawierający nazwy metod
- */
- public String show() {
- StringBuilder sb = new StringBuilder();
- sb.append("Dostepne uslugi: \n" );
- sb.append("squareSum parametry:double, double \n");
- sb.append("getValueAsy parametry:String, int, int \n");
- sb.append("isMultiplicity parametry:int, int \n");
- sb.append("getLiczbyPierwszeAsy: int,int,int \n");
- sb.append("show");
- return sb.toString();
- }
- /**
- * Metoda uruchamiająca serwer.
- */
- public static void main(String[] args) {
- try {
- System.out.println("Startuje serwer XML-RPC...");
- int port = 10014;
- WebServer server = new WebServer(port);
- server.addHandler("mojserwer", new SerwerRPC());
- server.start();
- System.out.println("Serwer wystartował pomyślnie.");
- System.out.println("Nasłuchuje na porcie: " + port);
- System.out.println("Aby zatrzymać serwer naciśnij ctrl+c");
- } catch (Exception exception) {
- System.err.println("Serwer XML-RPC: " + exception);
- }
- }
- }
- public class KlientRPC {
- public static void main(String[] args) {
- try {
- Scanner sc = new Scanner(System.in);
- System.out.println("Podaj adres serwera (domyślnie http://localhost:10014):");
- String address = sc.next();
- XmlRpcClient srv = new XmlRpcClient(address);
- Vector<Integer> params = new Vector<>();
- String result = (String) srv.execute("mojserwer.show", params);
- System.out.println(result);
- int g = 0;
- while (g==0) {
- System.out.println("Podaj nazwę usługi:");
- String serviceName = sc.next();
- Vector<Object> params2 = new Vector<>();
- if (serviceName.equals("getValueAsy")) {
- System.out.println("Podaj funkcję trygonometryczną");
- params2.add(sc.next());
- System.out.println("Podaj kąt");
- params2.add(sc.nextInt());
- System.out.println("Podaj czas oczekiwania");
- params2.add(sc.nextInt());
- AC cb = new AC();
- srv.executeAsync("mojserwer.getValueAsy", params2, cb);
- System.out.println("Wywołano asynchronicznie");
- }
- else if (serviceName.equals("squareSum")) {
- System.out.println("Podaj pierwszą liczbę");
- params2.add(sc.nextDouble());
- System.out.println("Podaj drugą liczbę");
- params2.add(sc.nextDouble());
- Object result2 = srv.execute("mojserwer.squareSum", params2);
- System.out.println("Wynik:" + result2);
- }
- else if(serviceName.equals("isMultiplicity")) {
- System.out.println("Podaj pierwszą liczbę");
- params2.add(sc.nextInt());
- System.out.println("Podaj drugą liczbę");
- params2.add(sc.nextInt());
- Object result2 = srv.execute("mojserwer.isMultiplicity", params2);
- System.out.println("Wynik:" + result2);
- }
- else if(serviceName.equals("getLiczbyPierwszeAsy")) {
- System.out.println("Podaj dolny zakres: ");
- params2.add(sc.nextInt());
- System.out.println("Podaj górny zakres: ");
- params2.add(sc.nextInt());
- System.out.println("Podaj czas: ");
- params2.add(sc.nextInt());
- AC cb = new AC();
- srv.executeAsync("mojserwer.getLiczbyPierwszeAsy", params2, cb);
- System.out.println("Wywołano asynchronicznie");
- // System.out.println("Wynik:" + result2);
- //
- // Object result2 = srv.execute("mojserwer.getLiczbyPierwszeAsy", params2);
- // System.out.println("Wynik:" + result2);
- }
- else if(serviceName.equals("show")) {
- Object result2 = srv.execute("mojserwer.show", params2);
- System.out.println(result2);
- }
- else {
- System.out.println("Nie ma takiej usługi.");
- }
- }
- sc.close();
- } catch (Exception exception) {
- System.err.println("Klient XML-RPC: " + exception);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement