This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from aanciaes/develop
Develop
- Loading branch information
Showing
13 changed files
with
622 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,25 @@ import com.typesafe.scalalogging.Logger | |
|
||
object Application extends App { | ||
|
||
val log = Logger("scala.slick") | ||
val log = Logger("phase1") | ||
val defaultProcess = "akka.tcp://[email protected]:2551" | ||
|
||
val config = ConfigFactory.load.getConfig("ApplicationConfig") | ||
val sys = ActorSystem("akkaSystem", config) | ||
val appActor = sys.actorOf(Props[appActor], "appActor") | ||
|
||
while (true) { | ||
|
||
val line = scala.io.StdIn.readLine() | ||
var words: Array[String] = line.split("\\s") | ||
|
||
|
||
words(0) match { | ||
case "gv" if (words.length == 2) => showGV(words(1)) | ||
case "pv" if (words.length == 2) => showPV(words(1)) | ||
case "ms" if (words.length == 2) => messagesStats(words(1)) | ||
case "write" if(words.length == 3) => write(words(1), words(2)) | ||
case "read" if(words.length == 2) => read(words(1)) | ||
//case "msall" if (words.length == 2) => messagesStatsAll() | ||
case "clear" => { | ||
for (i <- 1 to 20) | ||
|
@@ -42,6 +47,13 @@ object Application extends App { | |
appActor ! MessagesStats(process) | ||
} | ||
|
||
def write(dataId: String, data: String) ={ | ||
appActor ! Write(dataId, data) | ||
} | ||
|
||
def read(dataId: String) ={ | ||
appActor ! Read(dataId) | ||
} | ||
//def messagesStatsAll() = { | ||
|
||
//} | ||
|
@@ -60,6 +72,16 @@ object Application extends App { | |
process ! ShowPV | ||
} | ||
|
||
case Write(dataId, data) => { | ||
val process = sys.actorSelection(s"${defaultProcess}/user/globalView") | ||
process ! Write(dataId, data) | ||
} | ||
|
||
case Read(dataId) => { | ||
val process = sys.actorSelection(s"${defaultProcess}/user/globalView") | ||
process ! Read(dataId) | ||
} | ||
|
||
case MessagesStats(x) => { | ||
val process = sys.actorSelection(s"${x}/user/informationDissemination") | ||
process ! MessagesStats | ||
|
@@ -73,6 +95,12 @@ object Application extends App { | |
println ("-------------------------------------------------------------") | ||
} | ||
|
||
case replyStore: ReplyStoreAction => { | ||
println ("-------------------------------------------------------------") | ||
println (s"${replyStore.replyType} from ${replyStore.myself} with the DATA: ${replyStore.data}") | ||
println ("-------------------------------------------------------------") | ||
} | ||
|
||
case stats : ReplyMessagesStats => { | ||
println ("-------------------------------------------------------------") | ||
println(s"Messages from ${sender.path.address.toString}") | ||
|
@@ -93,6 +121,7 @@ object Application extends App { | |
println () | ||
println ("-------------------------------------------------------------") | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package app | ||
|
||
import scala.collection.mutable._ | ||
|
||
object FindProcess { | ||
|
||
def matchKeys(hashedDataId: Int, map: TreeMap[Int, _]): Int = { | ||
|
||
val hashID_2551: Int = math.abs(("akka.tcp://[email protected]:2551").reverse.hashCode % 1000) //474 in localhost | ||
var previousN = hashID_2551 | ||
|
||
var count = 1 | ||
|
||
for ((hash, _) <- map) { | ||
if (hash > hashedDataId) { | ||
|
||
if (hash == map.firstKey) { | ||
return map.lastKey | ||
} else { | ||
return previousN | ||
} | ||
} else { | ||
if (count == map.size) { | ||
return hash | ||
} | ||
count = count + 1 | ||
previousN = hash | ||
} | ||
} | ||
return 0 | ||
} | ||
} | ||
|
||
case class Operation (op : String, key : Int, data : String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
package layers | ||
|
||
import akka.actor.Actor | ||
import akka.actor.{Actor, ActorRef} | ||
import app._ | ||
import com.typesafe.scalalogging.Logger | ||
import scala.collection.mutable._ | ||
|
||
|
||
class GlobalView extends Actor { | ||
|
||
val log = Logger("scala.slick") | ||
val log = Logger("phase1") | ||
val log2 = Logger("phase2") | ||
val N_REPLICAS = 3 | ||
val hashID_2551: Int = math.abs(("akka.tcp://[email protected]:2551").reverse.hashCode % 1000) //474 in localhost | ||
|
||
var globalView: List[String] = List.empty | ||
var myself: String = "" | ||
var myHashedId: Int = 0 | ||
var hashedProcesses = TreeMap[Int, String]() | ||
|
||
|
||
override def receive = { | ||
|
||
case init: InitGlobView => { | ||
myself = init.selfAddress | ||
globalView = globalView :+ myself | ||
|
||
myHashedId = math.abs(init.selfAddress.reverse.hashCode % 1000) | ||
println("Unique Identifier: " + myHashedId) | ||
|
||
val process = context.actorSelection(s"${init.contactNode}/user/globalView") | ||
process ! ShowGV | ||
} | ||
|
||
case message: BroadcastMessage => { | ||
log.debug("Global view receive Broacast Message from: " + sender.path.address.toString) | ||
//log.debug("Global view receive Broacast Message from: " + sender.path.address.toString) | ||
message.messageType match { | ||
case "add" => { | ||
if (!message.node.equals(myself)) | ||
log.debug ("adding: " + message.node + " to global view") | ||
//log.debug("adding: " + message.node + " to global view") | ||
globalView = globalView :+ message.node | ||
} | ||
case "del" => { | ||
|
@@ -44,8 +55,95 @@ class GlobalView extends Actor { | |
//Since all global views are up to date, on init | ||
//Gets contact node global view and copies it to is own | ||
case reply: ReplyShowView => { | ||
for (n <- reply.nodes.filter(!_.equals(myself))) | ||
|
||
for (n <- reply.nodes.filter(!_.equals(myself))) { | ||
globalView = globalView :+ n | ||
} | ||
|
||
if(globalView.size >= N_REPLICAS){ | ||
for(p <- globalView){ | ||
val process = context.actorSelection(s"${p}/user/globalView") | ||
process ! InitReplication | ||
} | ||
} | ||
} | ||
|
||
|
||
case InitReplication => { | ||
updateHashedProcesses(globalView) | ||
|
||
val replicas : TreeMap[Int, String] = findReplicas() | ||
|
||
val process = context.actorSelection(s"${myself}/user/storage") | ||
process ! InitReplication(replicas, myself, myHashedId) | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
// - - - - - - - - STORAGE - - - - - - - - // | ||
|
||
case write: Write => { | ||
|
||
val hashedDataId = math.abs(write.dataId.reverse.hashCode % 1000) | ||
log2.debug("Received write with key: " + hashedDataId) | ||
|
||
val processId = FindProcess.matchKeys(hashedDataId, hashedProcesses) | ||
|
||
|
||
val process = context.actorSelection(s"${hashedProcesses.get(processId).get}/user/storage") | ||
process ! ForwardWrite(hashedDataId, write.data, sender) | ||
} | ||
|
||
|
||
case read: Read => { | ||
|
||
print("Received Read from application") | ||
val hashedDataId = math.abs(read.dataId.reverse.hashCode % 1000) | ||
|
||
val processId = FindProcess.matchKeys(hashedDataId, hashedProcesses) | ||
|
||
val process = context.actorSelection(s"${hashedProcesses.get(processId).get}/user/storage") | ||
process ! ForwardRead(hashedDataId, sender) | ||
} | ||
|
||
} | ||
|
||
|
||
// - - - - - - - - - - - - - - - - - - - - - - - // | ||
|
||
def updateHashedProcesses(globalView: List[String]) = { | ||
for (n <- globalView) { | ||
hashedProcesses.put(math.abs((n.reverse.hashCode % 1000)), n) | ||
} | ||
} | ||
|
||
def findReplicas() = { | ||
|
||
val replicas = TreeMap[Int, String]() | ||
|
||
var count = 0 | ||
var it = hashedProcesses.iterator | ||
var break = false | ||
while(true && !break){ | ||
val p = it.next() | ||
|
||
if(p._1 == myHashedId || count != 0){ | ||
replicas.put(p._1, p._2) | ||
count = count + 1 | ||
} | ||
|
||
if(count == 3){ | ||
break = true | ||
} | ||
|
||
if(!it.hasNext){ | ||
it = hashedProcesses.iterator | ||
} | ||
} | ||
replicas | ||
} | ||
|
||
} |
Oops, something went wrong.