Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hashcodevideostreaming;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- /**
- *
- * @author karimation
- */
- public class HashCodeVideoStreaming {
- public static int nbVideos;
- public static int nbEndPoints;
- public static int nbRequests;
- public static int nbCache;
- public static int CapaciteCache;
- public static int sizeVideos[];
- public static ArrayList<EndPoint> arrayEndPoint = new ArrayList<>();
- public static ArrayList<ReqeustVideo> arrayRequestVideo = new ArrayList<>();
- public static ArrayList<VideoToCache> arrayVideoCache = new ArrayList<>();
- public static int latenceCacheIDEndPoints[];
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- nbVideos = sc.nextInt();
- nbEndPoints = sc.nextInt();
- nbRequests = sc.nextInt();
- nbCache = sc.nextInt();
- CapaciteCache = sc.nextInt();
- // size of each videos
- sizeVideos = new int[nbVideos];
- for (int i = 0; i < nbVideos; i++) {
- sizeVideos[i] = sc.nextInt();
- }
- // latence of each endPoint with datacenter
- for (int i = 0; i < nbEndPoints; i++) {
- int latenceWithDC = sc.nextInt();
- int nbCacheConnected = sc.nextInt();
- arrayEndPoint.add(new EndPoint(i, latenceWithDC, nbCacheConnected));
- int arrayCacheToEndPoint[] = new int[nbCacheConnected];
- for (int j = 0; j < nbCacheConnected; j++) {
- arrayEndPoint.get(i).latenceWithEachCache.add(new IdCacheClient(sc.nextInt(), sc.nextInt()));
- }
- }
- //read Reaquets
- for (int i = 0; i < nbRequests; i++) {
- int idVideo = sc.nextInt();
- int endPoint = sc.nextInt();
- int nbReqVideo = sc.nextInt();
- arrayEndPoint.get(endPoint).nbVideoInCache.add(new ReqeustVideo(idVideo, nbReqVideo));
- // nemchi lel endpoint w n7ot fiha el video w nb request
- }
- //afficheEndPoint
- System.out.println("afficheEndPoint");
- System.out.println(arrayEndPoint.toString());
- //afficheRequest
- System.out.println("AfficheRequestVideo");
- System.out.println(arrayRequestVideo.toString());
- // traitement sur chaque cache
- // pour chaque cache chnekhdho les clients mte3o w naamlo sorting
- for (int i = 0; i < nbCache; i++) {
- //cache x
- arrayVideoCache.clear();
- for (int j = 0; j < nbEndPoints; j++) {
- // if endPoint connected to this cache
- for (int k = 0; k < arrayEndPoint.get(j).latenceWithEachCache.size(); k++) {
- if (arrayEndPoint.get(j).latenceWithEachCache.get(k).idServerCache == i) {
- VideoToCache vid = new VideoToCache(nbVideos, nbRequests, nbEndPoints, CapaciteCache, i);
- // parcours tous les videos
- for (int l = 0; l < arrayEndPoint.get(j).nbVideoInCache.size(); l++) {
- // public VideoToCache(int idVideo, int nbRequest, int latenceWithDC, int latenceWithCache, int IdEnd)
- System.out.println("lat: "+arrayEndPoint.get(j).latenceWithEachCache.get(l).latenceServerCache);
- VideoToCache v = new VideoToCache(arrayEndPoint.get(j).nbVideoInCache.get(l).idVideo,
- arrayEndPoint.get(j).nbVideoInCache.get(l).numberRequest, arrayEndPoint.get(j).latenceWithDataCenter,
- arrayEndPoint.get(j).latenceWithEachCache.get(l).latenceServerCache, j);
- arrayVideoCache.add(v);
- }
- }
- }
- }
- //affiche video in cache
- System.out.println("VideoInCache");
- System.out.println(arrayVideoCache.size());
- System.out.println("avantSort");
- for (int j = 0; j < arrayVideoCache.size(); j++) {
- System.out.println("idVideo: " + arrayVideoCache.get(j).idVideo);
- System.out.println("latcache: "+arrayVideoCache.get(j).latenceWithCache);
- System.out.println("latDC: "+arrayVideoCache.get(j).latenceWithCache);
- }
- Collections.sort(arrayVideoCache, new EndComparator());//
- System.out.println("Apres");
- for (int j = 0; j < arrayVideoCache.size(); j++) {
- System.out.println("idVideo: " + arrayVideoCache.get(j).idVideo);
- }
- break;
- }
- }
- static class EndComparator implements Comparator<VideoToCache> {
- @Override
- public int compare(VideoToCache o1, VideoToCache o2) {
- return o1.latenceWithCache < o2.latenceWithCache ? 1 : -1;
- }
- }
- }
- class CacheDist {
- int sizeOfCache;
- ArrayList listOfVideos = new ArrayList();
- public CacheDist(int sizeOfCache) {
- this.sizeOfCache = sizeOfCache;
- }
- }
- class ReqeustVideo {
- int idVideo;
- int numberRequest;
- public ReqeustVideo(int idVideo, int numberReq) {
- this.idVideo = idVideo;
- this.numberRequest = numberReq;
- }
- public int getIdVideo() {
- return idVideo;
- }
- public int getNumberRequest() {
- return numberRequest;
- }
- @Override
- public int hashCode() {
- int hash = 7;
- return hash;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final ReqeustVideo other = (ReqeustVideo) obj;
- if (this.idVideo != other.idVideo) {
- return false;
- }
- if (this.numberRequest != other.numberRequest) {
- return false;
- }
- return true;
- }
- @Override
- public String toString() {
- return "ReqeustVideo{" + "idVideo=" + idVideo + ", numberRequest=" + numberRequest + '}';
- }
- }
- class EndPoint {
- int idEndPoint;
- int latenceWithDataCenter;
- int nbCacheConnected;
- ArrayList<IdCacheClient> latenceWithEachCache = new ArrayList();
- ArrayList<ReqeustVideo> nbVideoInCache = new ArrayList();
- public EndPoint(int idEndPoint, int latenceWithDataCenter, int nbCacheConnected) {
- this.idEndPoint = idEndPoint;
- this.latenceWithDataCenter = latenceWithDataCenter;
- this.nbCacheConnected = nbCacheConnected;
- }
- }
- class IdCacheClient {
- int idServerCache;
- int latenceServerCache;
- public IdCacheClient(int idServerCache, int latenceServerCache) {
- this.idServerCache = idServerCache;
- this.latenceServerCache = latenceServerCache;
- }
- }
- class VideoToCache {
- int idVideo;
- int nbRequest;
- int latenceWithDC;
- int latenceWithCache;
- int IdEndPointt;
- public VideoToCache(int idVideo, int nbRequest, int latenceWithDC, int latenceWithCache, int IdEnd) {
- this.idVideo = idVideo;
- this.nbRequest = nbRequest;
- this.latenceWithDC = latenceWithDC * nbRequest;
- this.latenceWithCache = latenceWithCache * nbRequest;
- this.IdEndPointt = IdEnd;
- }
- @Override
- public String toString() {
- return "VideoToCache{" + "idVideo=" + idVideo + ", nbRequest=" + nbRequest + ", latenceWithDC=" + latenceWithDC + ", latenceWithCache=" + latenceWithCache + ", IdEndPointt=" + IdEndPointt + '}';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement