Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Punto 1 Terminado #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Lab1_ARSW_Castellanos_Naranjo_Torres.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
package edu.eci.arsw.blacklistvalidator;

import edu.eci.arsw.spamkeywordsdatasource.HostBlacklistsDataSourceFacade;
import edu.eci.arsw.threads.SearchTask;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -18,7 +22,8 @@
public class HostBlackListsValidator {

private static final int BLACK_LIST_ALARM_COUNT=5;

private List<SearchTask> searchThreads;

/**
* Check the given host's IP address in all the available black lists,
* and report it as NOT Trustworthy when such IP was reported in at least
Expand All @@ -29,42 +34,47 @@ public class HostBlackListsValidator {
* @param ipaddress suspicious host's IP address.
* @return Blacklists numbers where the given host's IP address was found.
*/
public List<Integer> checkHost(String ipaddress){

LinkedList<Integer> blackListOcurrences=new LinkedList<>();

int ocurrencesCount=0;

HostBlacklistsDataSourceFacade skds=HostBlacklistsDataSourceFacade.getInstance();

int checkedListsCount=0;

for (int i=0;i<skds.getRegisteredServersCount() && ocurrencesCount<BLACK_LIST_ALARM_COUNT;i++){
checkedListsCount++;

if (skds.isInBlackListServer(i, ipaddress)){

blackListOcurrences.add(i);

ocurrencesCount++;
public List<Integer> checkHost(String ipaddress, int threads){
HostBlacklistsDataSourceFacade skds = HostBlacklistsDataSourceFacade.getInstance();
LinkedList<Integer> mergedBlackListOcurrences = new LinkedList<>();
searchThreads = new ArrayList<>();

int totalLists = skds.getRegisteredServersCount();
int baseSize = totalLists / threads;
int remainder = totalLists % threads;
int startIndex = 0;

for(int i = 0; i < threads; i++) {
int segmentSize = (i < remainder) ? baseSize + 1 : baseSize;
int endIndex = startIndex + segmentSize - 1;

SearchTask thread = new SearchTask(startIndex, endIndex, ipaddress);
searchThreads.add(thread);
thread.start();

startIndex = endIndex + 1;
}
// Esperar a que todos los hilos terminen su ejecución
for(SearchTask thread : searchThreads) {
try {
thread.join();
} catch (InterruptedException e) {
LOG.log(Level.SEVERE, "Thread interrupted", e);
}
}

if (ocurrencesCount>=BLACK_LIST_ALARM_COUNT){
skds.reportAsNotTrustworthy(ipaddress);
int checkedListsCount = 0;

for(SearchTask thread : searchThreads){
mergedBlackListOcurrences.addAll(thread.getBlacklistOccurrences());
checkedListsCount += thread.getCheckedCount();
}
else{
if (mergedBlackListOcurrences.size() >= BLACK_LIST_ALARM_COUNT){
skds.reportAsNotTrustworthy(ipaddress);
} else {
skds.reportAsTrustworthy(ipaddress);
}

}
LOG.log(Level.INFO, "Checked Black Lists:{0} of {1}", new Object[]{checkedListsCount, skds.getRegisteredServersCount()});

return blackListOcurrences;
return mergedBlackListOcurrences;
}


private static final Logger LOG = Logger.getLogger(HostBlackListsValidator.class.getName());



}
}
28 changes: 17 additions & 11 deletions src/main/java/edu/eci/arsw/blacklistvalidator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@

import java.util.List;

/**
*
* @author hcadavid
*/
public class Main {

public static void main(String a[]){
HostBlackListsValidator hblv=new HostBlackListsValidator();
List<Integer> blackListOcurrences=hblv.checkHost("200.24.34.55");
System.out.println("The host was found in the following blacklists:"+blackListOcurrences);

public static void main(String[] args) {
int cores = Runtime.getRuntime().availableProcessors();
//runExperiment("202.24.34.55", 1);
//runExperiment("202.24.34.55", cores);
//runExperiment("202.24.34.55", cores * 2);
//runExperiment("202.24.34.55", 50);
runExperiment("202.24.34.55", 100);
}

}

private static void runExperiment(String ipaddress, int threads) {
HostBlackListsValidator hblv = new HostBlackListsValidator();
long startTime = System.currentTimeMillis();
List<Integer> blackListOcurrences = hblv.checkHost(ipaddress, threads);
long endTime = System.currentTimeMillis();
System.out.println("Execution time with " + threads + " threads: " + (endTime - startTime) + "ms");
System.out.println("The host was found in the following blacklists:" + blackListOcurrences);
}
}
29 changes: 27 additions & 2 deletions src/main/java/edu/eci/arsw/threads/CountThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@
*/
package edu.eci.arsw.threads;

import java.lang.*;

/**
*
* @author hcadavid
*/
public class CountThread {

public class CountThread implements Runnable {
public int LowLimit;
public int UpperLimit;

public CountThread(int LowLimit, int UpperLimit) {
this.LowLimit = LowLimit;
this.UpperLimit = UpperLimit;
}

public void run() {
if(UpperLimit < LowLimit){
int newA = UpperLimit;
UpperLimit = LowLimit;
UpperLimit = newA;
}
for (int i = LowLimit + 1; i < UpperLimit; i++) {
System.out.println(i);
}
}

public static void main(String[] args) {
Thread thread = new Thread(new CountThread(10, 15));
thread.start();
}

}
19 changes: 15 additions & 4 deletions src/main/java/edu/eci/arsw/threads/CountThreadsMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@
*/
package edu.eci.arsw.threads;

import java.lang.*;

/**
*
* @author hcadavid
*/
public class CountThreadsMain {
public static void main(String a[]){
public class CountThreadsMain implements Runnable{

public void run(){

}

public static void main(String[] args){
Thread thread1 = new Thread(new CountThread(0, 99));
Thread thread2 = new Thread(new CountThread(99, 199));
Thread thread3 = new Thread(new CountThread(200, 299));
thread1.run();
thread2.run();
thread3.run();
}

}
46 changes: 46 additions & 0 deletions src/main/java/edu/eci/arsw/threads/SearchTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.eci.arsw.threads;

import edu.eci.arsw.spamkeywordsdatasource.HostBlacklistsDataSourceFacade;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class SearchTask extends Thread {
private final int startRange;
private final int endRange;
private final String ipAddress;
private final List<Integer> blacklistOccurrences;
private static final int ALARM_THRESHOLD = 5;
private int checkedCount;

public SearchTask(int startRange, int endRange, String ipAddress) {
this.startRange = startRange;
this.endRange = endRange;
this.ipAddress = ipAddress;
this.blacklistOccurrences = new LinkedList<>();
}

@Override
public void run() {
int occurrenceCounter = 0;
HostBlacklistsDataSourceFacade blacklistDataSource = HostBlacklistsDataSourceFacade.getInstance();
checkedCount = 0;

for (int i = startRange; i <= endRange && occurrenceCounter < ALARM_THRESHOLD; i++) {
checkedCount++;
if (blacklistDataSource.isInBlackListServer(i, ipAddress)) {
blacklistOccurrences.add(i);
occurrenceCounter++;
}
}
}

public List<Integer> getBlacklistOccurrences() {
return blacklistOccurrences;
}

public int getCheckedCount() {
return checkedCount;
}
}
15 changes: 4 additions & 11 deletions src/test/java/edu/eci/arsw/math/PiCalcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.junit.Test;
import static org.junit.Assert.*;




/**
*
* @author hcadavid
Expand Down Expand Up @@ -37,16 +40,6 @@ public void piGenTest() throws Exception {
0x4, 0x5, 0x2, 0x8, 0x2, 0x1, 0xE, 0x6,
0x3, 0x8, 0xD, 0x0, 0x1, 0x3, 0x7, 0x7,};

for (int start = 0; start < expected.length; start++) {
for (int count = 0; count < expected.length - start; count++) {
byte[] digits = PiDigits.getDigits(start, count);
assertEquals(count, digits.length);

for (int i = 0; i < digits.length; i++) {
assertEquals(expected[start + i], digits[i]);
}
}
}
}

}
}