Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. public class userapplication {
  4.  
  5. static int packetCounter = 0;
  6.  
  7. /**
  8. * @param args
  9. * @throws IOException
  10. */
  11. public static void main(String[] args) throws IOException {
  12.  
  13. userapplication echopackets = new userapplication ();// αντικειμενο που θα μας βοηθησει στην κληση της συναρτησης echo
  14.  
  15.  
  16. // δημιουργια αρχειων
  17. PrintWriter responseTimesEcho = new PrintWriter("responseTimesEcho.txt", "UTF-8");
  18. PrintWriter responseTimesEchoWithoutDelay = new PrintWriter("responseTimesEchoWithoutDelay.txt", "UTF-8");
  19. PrintWriter temper = new PrintWriter("temper.txt", "UTF-8");
  20. FileOutputStream im = new FileOutputStream("M1.jpg");
  21. // κωδικοι Ιθακης
  22. String EchoCode = "E9260";
  23. String EchoCodeWithoutDelay ="E0000";
  24. String imageCode = "M8151FLOW=ON";
  25. String EchoCode2= EchoCode+ "T00";
  26. int clientPort = 48020;
  27. int serverPort = 38020;
  28.  
  29.  
  30. //μεταβλητες που θα βοηθησουν στο να τρεχει ενα κομματι κωδικα οσο χρονο επιθυμουμε
  31. long timeRun = System.currentTimeMillis();
  32. long endRun = timeRun + 30000;
  33.  
  34.  
  35. //βρογχος ωστε να τρεχει συγκεκριμενο χρονο ο κωδικας που βρισκεται μεσα σε αυτον
  36. while (System.currentTimeMillis() < endRun) {
  37.  
  38. echopackets.echo(responseTimesEcho, EchoCode,clientPort, serverPort);//κληση της echo για 4 λεπτα
  39.  
  40. }
  41.  
  42. responseTimesEcho.close();
  43.  
  44. timeRun = System.currentTimeMillis();
  45. endRun = timeRun + 30000;
  46. packetCounter = 0;
  47.  
  48.  
  49. //βρογχος ωστε να τρεχει συγκεκριμενο χρονο ο κωδικας που βρισκεται μεσα σε αυτον
  50. while (System.currentTimeMillis() < endRun) {
  51.  
  52. echopackets.echo(responseTimesEchoWithoutDelay,EchoCodeWithoutDelay,clientPort,serverPort);//κληση της echo με code Ε0000 για να μην υπαρχει καθυστερηση απο τον server
  53.  
  54. }
  55.  
  56.  
  57. responseTimesEchoWithoutDelay.close();
  58.  
  59. // ληψη θερμοκρασιας απο τον αισθητηρα 00 με χρηση του επιθεματος Τ00
  60.  
  61. echopackets.temperatures(temper,EchoCode2,clientPort,serverPort);//κληση της temperature με echocode που περιλαμβανει και κωδικο ΤΧΧ
  62.  
  63.  
  64.  
  65.  
  66. temper.close();
  67.  
  68.  
  69.  
  70.  
  71. //ληψη εικονας
  72.  
  73. echopackets.image(im,imageCode, clientPort, serverPort);
  74. im.close();
  75.  
  76.  
  77.  
  78. }
  79.  
  80. //συναρτηση που υλοποιει ενα request του client προς τον Server Ithaki
  81.  
  82. public void Requests (String c , int sp ) throws IOException {
  83.  
  84. DatagramSocket s = new DatagramSocket();
  85.  
  86. String packetInfo = c;
  87.  
  88.  
  89.  
  90. int serverPort = sp;
  91.  
  92. byte [] code = packetInfo.getBytes();
  93.  
  94. byte[] hostIP = { (byte)155,(byte)207,(byte)18,(byte)208 };
  95.  
  96. InetAddress hostAddress = InetAddress.getByAddress(hostIP);
  97.  
  98. DatagramPacket p = new DatagramPacket(code,code.length, hostAddress,serverPort);
  99.  
  100. s.send(p);
  101.  
  102. s.close();
  103. }
  104.  
  105. //συναρτηση echo οπου γινεται η ληψη του πακετου απο το server, , και χρονομετρουνται τα response times του συστηματος
  106.  
  107. public void echo (PrintWriter resTiEcho, String code,int clp,int sp) throws IOException {
  108.  
  109.  
  110.  
  111. int clientPort = clp;
  112.  
  113. DatagramSocket r = new DatagramSocket(clientPort);
  114. r.setSoTimeout(1200);
  115.  
  116. byte[] rxbuffer = new byte[32];
  117.  
  118. DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
  119.  
  120. String message = new String(rxbuffer,0,q.getLength());
  121.  
  122. long startT;
  123. long endT;
  124.  
  125.  
  126. int responseTimes;
  127.  
  128.  
  129.  
  130.  
  131. Requests (code,sp); // κληση της Request για να γινει αιτημα του client προς τον server
  132.  
  133. try {
  134.  
  135. startT = System.currentTimeMillis();// χρονος εκκινησης του προγραμματος
  136.  
  137. r.receive(q);
  138.  
  139. endT = System.currentTimeMillis();
  140.  
  141. responseTimes = (int)(endT - startT); // ευρεση χρονου αποκρισης
  142.  
  143. resTiEcho.println(responseTimes); // εγγραφη των χρονων αποκρισης σε αρχειο
  144.  
  145.  
  146. message = new String(rxbuffer,0,q.getLength());
  147. packetCounter++; // αριθμηση των πακετων που λαμβανουμε
  148. System.out.println(message +" "+ "packet No:"+packetCounter);
  149.  
  150. }
  151. catch (Exception x) {
  152. System.out.println(x);
  153. }
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. r.close();
  161.  
  162. }
  163. // συναρτηση για την ληψη θερμοκρασιων απο τους αισθητηρες
  164. public void temperatures (PrintWriter temp, String code,int clp,int sp) throws IOException {
  165.  
  166. int clientPort = clp;
  167.  
  168. DatagramSocket r = new DatagramSocket(clientPort);
  169. r.setSoTimeout(1200);
  170.  
  171. byte[] rxbuffer = new byte[55];
  172.  
  173. DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
  174.  
  175. String message = new String(rxbuffer,0,q.getLength());
  176.  
  177. Requests (code,sp); // κληση της Request για να γινει αιτημα του client προς τον server
  178.  
  179. try {
  180.  
  181. r.receive(q);
  182.  
  183. message = new String(rxbuffer,0,q.getLength());
  184. packetCounter++;
  185. System.out.println(message +" "+ "packet No:"+packetCounter);
  186.  
  187. }
  188. catch (Exception x) {
  189. System.out.println(x);
  190. }
  191.  
  192.  
  193.  
  194. String temps = new String(rxbuffer,43,3);
  195.  
  196. temp.println(temps); //εγγραψη των θερμοκρασιων σε αρχειο
  197.  
  198.  
  199.  
  200.  
  201.  
  202. r.close();
  203.  
  204.  
  205.  
  206. }
  207.  
  208. public void image(FileOutputStream ima,String code,int clientPort,int sp) throws IOException {
  209. DatagramSocket r = new DatagramSocket(clientPort);
  210.  
  211.  
  212.  
  213. byte[] rxbuffer = new byte[128];
  214.  
  215. DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
  216.  
  217.  
  218.  
  219.  
  220. boolean flag = true;
  221.  
  222.  
  223.  
  224.  
  225. while(flag) {
  226.  
  227. Requests (code,sp); // κληση της Request για να γινει αιτημα του client προς τον server
  228. r.receive(q);
  229. for(int i=0;i<128;i++) {
  230.  
  231. if(i<127) {
  232.  
  233.  
  234. if(rxbuffer[i]==(byte)0xFF &&rxbuffer[i+1]==(byte)0xD9 ) {
  235.  
  236. flag=false;
  237.  
  238.  
  239.  
  240.  
  241. }
  242.  
  243. }
  244. System.out.println((int)rxbuffer[i]);
  245. ima.write(rxbuffer[i]);
  246.  
  247. }
  248.  
  249.  
  250. }
  251. r.close();
  252.  
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement