Advertisement
Guest User

hi

a guest
Feb 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.89 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package hashcodevideostreaming;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.Collections;
  11. import java.util.Comparator;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author karimation
  17.  */
  18. public class HashCodeVideoStreaming {
  19.  
  20.     public static int nbVideos;
  21.     public static int nbEndPoints;
  22.     public static int nbRequests;
  23.     public static int nbCache;
  24.     public static int CapaciteCache;
  25.     public static int sizeVideos[];
  26.     public static ArrayList<EndPoint> arrayEndPoint = new ArrayList<>();
  27.     public static ArrayList<ReqeustVideo> arrayRequestVideo = new ArrayList<>();
  28.     public static ArrayList<VideoToCache> arrayVideoCache = new ArrayList<>();
  29.  
  30.     public static int latenceCacheIDEndPoints[];
  31.  
  32.     public static void main(String[] args) {
  33.  
  34.         Scanner sc = new Scanner(System.in);
  35.         nbVideos = sc.nextInt();
  36.         nbEndPoints = sc.nextInt();
  37.         nbRequests = sc.nextInt();
  38.         nbCache = sc.nextInt();
  39.         CapaciteCache = sc.nextInt();
  40.         // size of each videos
  41.         sizeVideos = new int[nbVideos];
  42.         for (int i = 0; i < nbVideos; i++) {
  43.             sizeVideos[i] = sc.nextInt();
  44.         }
  45.  
  46.         // latence of each endPoint with datacenter
  47.         for (int i = 0; i < nbEndPoints; i++) {
  48.             int latenceWithDC = sc.nextInt();
  49.             int nbCacheConnected = sc.nextInt();
  50.             arrayEndPoint.add(new EndPoint(i, latenceWithDC, nbCacheConnected));
  51.             int arrayCacheToEndPoint[] = new int[nbCacheConnected];
  52.             for (int j = 0; j < nbCacheConnected; j++) {
  53.                 arrayEndPoint.get(i).latenceWithEachCache.add(new IdCacheClient(sc.nextInt(), sc.nextInt()));
  54.             }
  55.  
  56.         }
  57.  
  58.         //read Reaquets
  59.         for (int i = 0; i < nbRequests; i++) {
  60.             int idVideo = sc.nextInt();
  61.             int endPoint = sc.nextInt();
  62.             int nbReqVideo = sc.nextInt();
  63.             arrayEndPoint.get(endPoint).nbVideoInCache.add(new ReqeustVideo(idVideo, nbReqVideo));
  64.             // nemchi lel endpoint w n7ot fiha el video w nb request
  65.         }
  66.         //afficheEndPoint
  67.         System.out.println("afficheEndPoint");
  68.         System.out.println(arrayEndPoint.toString());
  69.         //afficheRequest
  70.         System.out.println("AfficheRequestVideo");
  71.         System.out.println(arrayRequestVideo.toString());
  72.         // traitement sur chaque cache
  73.         // pour chaque cache chnekhdho les clients mte3o w naamlo sorting
  74.         for (int i = 0; i < nbCache; i++) {
  75.             //cache x
  76.             arrayVideoCache.clear();
  77.  
  78.             for (int j = 0; j < nbEndPoints; j++) {
  79.                 // if endPoint connected to this cache
  80.                 for (int k = 0; k < arrayEndPoint.get(j).latenceWithEachCache.size(); k++) {
  81.                     if (arrayEndPoint.get(j).latenceWithEachCache.get(k).idServerCache == i) {
  82.                         VideoToCache vid = new VideoToCache(nbVideos, nbRequests, nbEndPoints, CapaciteCache, i);
  83.                         // parcours tous les videos
  84.                         for (int l = 0; l < arrayEndPoint.get(j).nbVideoInCache.size(); l++) {
  85.                             //    public VideoToCache(int idVideo, int nbRequest, int latenceWithDC, int latenceWithCache, int IdEnd)
  86.  
  87.                             System.out.println("lat: "+arrayEndPoint.get(j).latenceWithEachCache.get(l).latenceServerCache);
  88.                             VideoToCache v = new VideoToCache(arrayEndPoint.get(j).nbVideoInCache.get(l).idVideo,
  89.                                     arrayEndPoint.get(j).nbVideoInCache.get(l).numberRequest, arrayEndPoint.get(j).latenceWithDataCenter,
  90.                                     arrayEndPoint.get(j).latenceWithEachCache.get(l).latenceServerCache, j);
  91.                             arrayVideoCache.add(v);
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.             //affiche video in cache
  97.             System.out.println("VideoInCache");
  98.             System.out.println(arrayVideoCache.size());
  99.             System.out.println("avantSort");
  100.             for (int j = 0; j < arrayVideoCache.size(); j++) {
  101.                 System.out.println("idVideo: " + arrayVideoCache.get(j).idVideo);
  102.                 System.out.println("latcache: "+arrayVideoCache.get(j).latenceWithCache);
  103.                 System.out.println("latDC: "+arrayVideoCache.get(j).latenceWithCache);
  104.             }
  105.             Collections.sort(arrayVideoCache, new EndComparator());//
  106.             System.out.println("Apres");
  107.             for (int j = 0; j < arrayVideoCache.size(); j++) {
  108.                 System.out.println("idVideo: " + arrayVideoCache.get(j).idVideo);
  109.             }
  110.  
  111.             break;
  112.  
  113.         }
  114.     }
  115.  
  116.     static class EndComparator implements Comparator<VideoToCache> {
  117.  
  118.         @Override
  119.         public int compare(VideoToCache o1, VideoToCache o2) {
  120.             return o1.latenceWithCache < o2.latenceWithCache ? 1 : -1;
  121.         }
  122.     }
  123. }
  124.  
  125. class CacheDist {
  126.  
  127.     int sizeOfCache;
  128.     ArrayList listOfVideos = new ArrayList();
  129.  
  130.     public CacheDist(int sizeOfCache) {
  131.         this.sizeOfCache = sizeOfCache;
  132.     }
  133.  
  134. }
  135.  
  136. class ReqeustVideo {
  137.  
  138.     int idVideo;
  139.     int numberRequest;
  140.  
  141.     public ReqeustVideo(int idVideo, int numberReq) {
  142.         this.idVideo = idVideo;
  143.         this.numberRequest = numberReq;
  144.     }
  145.  
  146.     public int getIdVideo() {
  147.         return idVideo;
  148.     }
  149.  
  150.     public int getNumberRequest() {
  151.         return numberRequest;
  152.     }
  153.  
  154.     @Override
  155.     public int hashCode() {
  156.         int hash = 7;
  157.         return hash;
  158.     }
  159.  
  160.     @Override
  161.     public boolean equals(Object obj) {
  162.         if (this == obj) {
  163.             return true;
  164.         }
  165.         if (obj == null) {
  166.             return false;
  167.         }
  168.         if (getClass() != obj.getClass()) {
  169.             return false;
  170.         }
  171.         final ReqeustVideo other = (ReqeustVideo) obj;
  172.         if (this.idVideo != other.idVideo) {
  173.             return false;
  174.         }
  175.         if (this.numberRequest != other.numberRequest) {
  176.             return false;
  177.         }
  178.  
  179.         return true;
  180.     }
  181.  
  182.     @Override
  183.     public String toString() {
  184.         return "ReqeustVideo{" + "idVideo=" + idVideo + ", numberRequest=" + numberRequest + '}';
  185.     }
  186.  
  187. }
  188.  
  189. class EndPoint {
  190.  
  191.     int idEndPoint;
  192.     int latenceWithDataCenter;
  193.     int nbCacheConnected;
  194.     ArrayList<IdCacheClient> latenceWithEachCache = new ArrayList();
  195.     ArrayList<ReqeustVideo> nbVideoInCache = new ArrayList();
  196.  
  197.     public EndPoint(int idEndPoint, int latenceWithDataCenter, int nbCacheConnected) {
  198.         this.idEndPoint = idEndPoint;
  199.         this.latenceWithDataCenter = latenceWithDataCenter;
  200.         this.nbCacheConnected = nbCacheConnected;
  201.     }
  202.  
  203. }
  204.  
  205. class IdCacheClient {
  206.  
  207.     int idServerCache;
  208.     int latenceServerCache;
  209.  
  210.     public IdCacheClient(int idServerCache, int latenceServerCache) {
  211.         this.idServerCache = idServerCache;
  212.         this.latenceServerCache = latenceServerCache;
  213.     }
  214.  
  215. }
  216.  
  217. class VideoToCache {
  218.  
  219.     int idVideo;
  220.     int nbRequest;
  221.     int latenceWithDC;
  222.     int latenceWithCache;
  223.     int IdEndPointt;
  224.  
  225.     public VideoToCache(int idVideo, int nbRequest, int latenceWithDC, int latenceWithCache, int IdEnd) {
  226.         this.idVideo = idVideo;
  227.         this.nbRequest = nbRequest;
  228.         this.latenceWithDC = latenceWithDC * nbRequest;
  229.         this.latenceWithCache = latenceWithCache * nbRequest;
  230.         this.IdEndPointt = IdEnd;
  231.     }
  232.  
  233.     @Override
  234.     public String toString() {
  235.         return "VideoToCache{" + "idVideo=" + idVideo + ", nbRequest=" + nbRequest + ", latenceWithDC=" + latenceWithDC + ", latenceWithCache=" + latenceWithCache + ", IdEndPointt=" + IdEndPointt + '}';
  236.     }
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement