Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1.  
  2. public class SerwerRPC {
  3.  
  4. /**
  5. * Metoda licząca sumę kwadratów dwóch liczb.
  6. * @param x pierwsza liczba
  7. * @param y druga liczba
  8. * @return wartość wyrażenia x^2 + y^2
  9. */
  10. public Double squareSum(double x, double y) {
  11. return Math.pow(x, 2) + Math.pow(y, 2);
  12. }
  13.  
  14. /**
  15. * Metoda licząca wartość podanej funkcji trygonometrycznej dla wskazanego kąta.
  16. * @param function nazwa funkcji trygonometrycznej
  17. * @param arg kąt w stopniach
  18. * @param time czas oczekiwania na wynik
  19. * @return wartość podanej funkcji trygonometrycznej
  20. */
  21. public double getValueAsy(String function, int arg, int time) {
  22. double radians = arg * Math.PI/180.0;
  23. double result=0;
  24. if (function.equals("sin")) {
  25. result =Math.sin(radians);
  26. }
  27. else if (function.equals("cos")) {
  28. result= Math.cos(radians);
  29. }
  30. else if (function.equals("tg") || function.equals("tan")) {
  31. result=Math.tan(radians);
  32. }
  33. else if (function.equals("ctg")) {
  34. result=1/Math.tan(radians);
  35. }
  36. try {
  37. Thread.sleep(time);
  38. } catch (InterruptedException ex) {
  39. ex.printStackTrace();
  40. Thread.currentThread().interrupt();
  41.  
  42. }
  43. result*=100;
  44. int tmp = (int) result;
  45. result = tmp/100.0;
  46. System.out.println(result);
  47. return result;
  48. }
  49.  
  50.  
  51. public String getLiczbyPierwszeAsy(int minRange,int maxRange,int time){
  52. List<Integer> list = new ArrayList<>();
  53.  
  54. StringBuilder sb = new StringBuilder();
  55.  
  56.  
  57. for (int i=minRange;i<=maxRange;i++) {
  58. if(isPrimeNumber(i)) {
  59. sb.append(i+",");
  60. }
  61. }
  62. try {
  63. Thread.sleep(time);
  64. } catch (InterruptedException ex) {
  65. ex.printStackTrace();
  66. Thread.currentThread().interrupt();
  67.  
  68. }
  69.  
  70. return sb.toString();
  71. }
  72.  
  73. private boolean isPrimeNumber(int liczba)
  74. {
  75. if(liczba<2)
  76. {
  77. return false;
  78. }
  79. for(int i=2; i<=liczba/2; i++)
  80. {
  81. if(liczba%i==0)
  82. {
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * Metoda stwierdzająca czy jedna liczba jest wielokrotnością drugiej.
  90. * @param x pierwsza liczba
  91. * @param y druga liczba
  92. * @return prawda jeśli pierwsza liczba jest wielokrotnością drugiej lub fałsz gdy nie jest
  93. */
  94. public Boolean isMultiplicity(int x, int y) {
  95. return x % y == 0;
  96. }
  97. /**
  98. * Metoda wyświetlająca zadeklarowane funkcje w klasie.
  99. * @return tekst zawierający nazwy metod
  100. */
  101. public String show() {
  102. StringBuilder sb = new StringBuilder();
  103. sb.append("Dostepne uslugi: \n" );
  104. sb.append("squareSum parametry:double, double \n");
  105. sb.append("getValueAsy parametry:String, int, int \n");
  106. sb.append("isMultiplicity parametry:int, int \n");
  107. sb.append("getLiczbyPierwszeAsy: int,int,int \n");
  108. sb.append("show");
  109. return sb.toString();
  110. }
  111. /**
  112. * Metoda uruchamiająca serwer.
  113. */
  114. public static void main(String[] args) {
  115. try {
  116. System.out.println("Startuje serwer XML-RPC...");
  117. int port = 10014;
  118. WebServer server = new WebServer(port);
  119. server.addHandler("mojserwer", new SerwerRPC());
  120. server.start();
  121. System.out.println("Serwer wystartował pomyślnie.");
  122. System.out.println("Nasłuchuje na porcie: " + port);
  123. System.out.println("Aby zatrzymać serwer naciśnij ctrl+c");
  124. } catch (Exception exception) {
  125. System.err.println("Serwer XML-RPC: " + exception);
  126. }
  127. }
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. public class KlientRPC {
  137.  
  138. public static void main(String[] args) {
  139. try {
  140. Scanner sc = new Scanner(System.in);
  141. System.out.println("Podaj adres serwera (domyślnie http://localhost:10014):");
  142. String address = sc.next();
  143. XmlRpcClient srv = new XmlRpcClient(address);
  144.  
  145. Vector<Integer> params = new Vector<>();
  146. String result = (String) srv.execute("mojserwer.show", params);
  147. System.out.println(result);
  148.  
  149. int g = 0;
  150. while (g==0) {
  151. System.out.println("Podaj nazwę usługi:");
  152. String serviceName = sc.next();
  153. Vector<Object> params2 = new Vector<>();
  154. if (serviceName.equals("getValueAsy")) {
  155. System.out.println("Podaj funkcję trygonometryczną");
  156. params2.add(sc.next());
  157. System.out.println("Podaj kąt");
  158. params2.add(sc.nextInt());
  159. System.out.println("Podaj czas oczekiwania");
  160. params2.add(sc.nextInt());
  161. AC cb = new AC();
  162. srv.executeAsync("mojserwer.getValueAsy", params2, cb);
  163. System.out.println("Wywołano asynchronicznie");
  164. }
  165. else if (serviceName.equals("squareSum")) {
  166. System.out.println("Podaj pierwszą liczbę");
  167. params2.add(sc.nextDouble());
  168. System.out.println("Podaj drugą liczbę");
  169. params2.add(sc.nextDouble());
  170. Object result2 = srv.execute("mojserwer.squareSum", params2);
  171. System.out.println("Wynik:" + result2);
  172. }
  173. else if(serviceName.equals("isMultiplicity")) {
  174. System.out.println("Podaj pierwszą liczbę");
  175. params2.add(sc.nextInt());
  176. System.out.println("Podaj drugą liczbę");
  177. params2.add(sc.nextInt());
  178. Object result2 = srv.execute("mojserwer.isMultiplicity", params2);
  179. System.out.println("Wynik:" + result2);
  180. }
  181. else if(serviceName.equals("getLiczbyPierwszeAsy")) {
  182. System.out.println("Podaj dolny zakres: ");
  183. params2.add(sc.nextInt());
  184. System.out.println("Podaj górny zakres: ");
  185. params2.add(sc.nextInt());
  186. System.out.println("Podaj czas: ");
  187. params2.add(sc.nextInt());
  188.  
  189. AC cb = new AC();
  190. srv.executeAsync("mojserwer.getLiczbyPierwszeAsy", params2, cb);
  191. System.out.println("Wywołano asynchronicznie");
  192. // System.out.println("Wynik:" + result2);
  193. //
  194. // Object result2 = srv.execute("mojserwer.getLiczbyPierwszeAsy", params2);
  195. // System.out.println("Wynik:" + result2);
  196. }
  197. else if(serviceName.equals("show")) {
  198. Object result2 = srv.execute("mojserwer.show", params2);
  199. System.out.println(result2);
  200. }
  201.  
  202. else {
  203. System.out.println("Nie ma takiej usługi.");
  204. }
  205. }
  206.  
  207. sc.close();
  208.  
  209. } catch (Exception exception) {
  210. System.err.println("Klient XML-RPC: " + exception);
  211. }
  212.  
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement