Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class userapplication {
- static int packetCounter = 0;
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- userapplication echopackets = new userapplication ();// αντικειμενο που θα μας βοηθησει στην κληση της συναρτησης echo
- // δημιουργια αρχειων
- PrintWriter responseTimesEcho = new PrintWriter("responseTimesEcho.txt", "UTF-8");
- PrintWriter responseTimesEchoWithoutDelay = new PrintWriter("responseTimesEchoWithoutDelay.txt", "UTF-8");
- PrintWriter temper = new PrintWriter("temper.txt", "UTF-8");
- FileOutputStream im = new FileOutputStream("M1.jpg");
- // κωδικοι Ιθακης
- String EchoCode = "E9260";
- String EchoCodeWithoutDelay ="E0000";
- String imageCode = "M8151FLOW=ON";
- String EchoCode2= EchoCode+ "T00";
- int clientPort = 48020;
- int serverPort = 38020;
- //μεταβλητες που θα βοηθησουν στο να τρεχει ενα κομματι κωδικα οσο χρονο επιθυμουμε
- long timeRun = System.currentTimeMillis();
- long endRun = timeRun + 30000;
- //βρογχος ωστε να τρεχει συγκεκριμενο χρονο ο κωδικας που βρισκεται μεσα σε αυτον
- while (System.currentTimeMillis() < endRun) {
- echopackets.echo(responseTimesEcho, EchoCode,clientPort, serverPort);//κληση της echo για 4 λεπτα
- }
- responseTimesEcho.close();
- timeRun = System.currentTimeMillis();
- endRun = timeRun + 30000;
- packetCounter = 0;
- //βρογχος ωστε να τρεχει συγκεκριμενο χρονο ο κωδικας που βρισκεται μεσα σε αυτον
- while (System.currentTimeMillis() < endRun) {
- echopackets.echo(responseTimesEchoWithoutDelay,EchoCodeWithoutDelay,clientPort,serverPort);//κληση της echo με code Ε0000 για να μην υπαρχει καθυστερηση απο τον server
- }
- responseTimesEchoWithoutDelay.close();
- // ληψη θερμοκρασιας απο τον αισθητηρα 00 με χρηση του επιθεματος Τ00
- echopackets.temperatures(temper,EchoCode2,clientPort,serverPort);//κληση της temperature με echocode που περιλαμβανει και κωδικο ΤΧΧ
- temper.close();
- //ληψη εικονας
- echopackets.image(im,imageCode, clientPort, serverPort);
- im.close();
- }
- //συναρτηση που υλοποιει ενα request του client προς τον Server Ithaki
- public void Requests (String c , int sp ) throws IOException {
- DatagramSocket s = new DatagramSocket();
- String packetInfo = c;
- int serverPort = sp;
- byte [] code = packetInfo.getBytes();
- byte[] hostIP = { (byte)155,(byte)207,(byte)18,(byte)208 };
- InetAddress hostAddress = InetAddress.getByAddress(hostIP);
- DatagramPacket p = new DatagramPacket(code,code.length, hostAddress,serverPort);
- s.send(p);
- s.close();
- }
- //συναρτηση echo οπου γινεται η ληψη του πακετου απο το server, , και χρονομετρουνται τα response times του συστηματος
- public void echo (PrintWriter resTiEcho, String code,int clp,int sp) throws IOException {
- int clientPort = clp;
- DatagramSocket r = new DatagramSocket(clientPort);
- r.setSoTimeout(1200);
- byte[] rxbuffer = new byte[32];
- DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
- String message = new String(rxbuffer,0,q.getLength());
- long startT;
- long endT;
- int responseTimes;
- Requests (code,sp); // κληση της Request για να γινει αιτημα του client προς τον server
- try {
- startT = System.currentTimeMillis();// χρονος εκκινησης του προγραμματος
- r.receive(q);
- endT = System.currentTimeMillis();
- responseTimes = (int)(endT - startT); // ευρεση χρονου αποκρισης
- resTiEcho.println(responseTimes); // εγγραφη των χρονων αποκρισης σε αρχειο
- message = new String(rxbuffer,0,q.getLength());
- packetCounter++; // αριθμηση των πακετων που λαμβανουμε
- System.out.println(message +" "+ "packet No:"+packetCounter);
- }
- catch (Exception x) {
- System.out.println(x);
- }
- r.close();
- }
- // συναρτηση για την ληψη θερμοκρασιων απο τους αισθητηρες
- public void temperatures (PrintWriter temp, String code,int clp,int sp) throws IOException {
- int clientPort = clp;
- DatagramSocket r = new DatagramSocket(clientPort);
- r.setSoTimeout(1200);
- byte[] rxbuffer = new byte[55];
- DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
- String message = new String(rxbuffer,0,q.getLength());
- Requests (code,sp); // κληση της Request για να γινει αιτημα του client προς τον server
- try {
- r.receive(q);
- message = new String(rxbuffer,0,q.getLength());
- packetCounter++;
- System.out.println(message +" "+ "packet No:"+packetCounter);
- }
- catch (Exception x) {
- System.out.println(x);
- }
- String temps = new String(rxbuffer,43,3);
- temp.println(temps); //εγγραψη των θερμοκρασιων σε αρχειο
- r.close();
- }
- public void image(FileOutputStream ima,String code,int clientPort,int sp) throws IOException {
- DatagramSocket r = new DatagramSocket(clientPort);
- byte[] rxbuffer = new byte[128];
- DatagramPacket q = new DatagramPacket(rxbuffer,rxbuffer.length);
- boolean flag = true;
- while(flag) {
- Requests (code,sp); // κληση της Request για να γινει αιτημα του client προς τον server
- r.receive(q);
- for(int i=0;i<128;i++) {
- if(i<127) {
- if(rxbuffer[i]==(byte)0xFF &&rxbuffer[i+1]==(byte)0xD9 ) {
- flag=false;
- }
- }
- System.out.println((int)rxbuffer[i]);
- ima.write(rxbuffer[i]);
- }
- }
- r.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement