Added documentation, and refactored it a bit.

This commit is contained in:
CodeForFame 2011-06-22 11:44:40 -05:00
parent dfbc8e8637
commit 742e31bd3e

View File

@ -14,6 +14,8 @@ import java.util.concurrent.Executors
/** /**
* This is for out-of-game alerts. * This is for out-of-game alerts.
*
* @author CodeForFame
*/ */
object AlertHandler extends Application { object AlertHandler extends Application {
@ -33,18 +35,24 @@ object AlertHandler extends Application {
override def run() { override def run() {
val meds = recip.data.filter(p => p._1 <= priority) val meds = recip.data.filter(p => p._1 <= priority)
for (m <- meds) { for (m <- meds) {
Medium.send(m._2, msg) Service.send(m._2, msg)
} }
} }
}) })
} }
/**
* Sends an alert to all users.
*/
def sendAlert(msg: String, priority: Int) { def sendAlert(msg: String, priority: Int) {
for (u <- users) for (u <- users)
sendAlert(msg, u, priority) sendAlert(msg, u, priority)
} }
def load { /**
* Loads the config file.
*/
private def load {
val config = XML.loadFile("alert-config.xml") val config = XML.loadFile("alert-config.xml")
val users1 = (config \\ "user") val users1 = (config \\ "user")
val list = new ListBuffer[User]; val list = new ListBuffer[User];
@ -54,13 +62,16 @@ object AlertHandler extends Application {
users = list.toList users = list.toList
} }
/**
* Parses the XML and creates a User from it.
*/
private def parseUser(u: Node) = { private def parseUser(u: Node) = {
val name = (u \ "name").text val name = (u \ "name").text
val credentials = { val credentials = {
val map = new HashMap[Int, Medium] val map = new HashMap[Int, Service]
val creds = u \ "email" val creds = u \ "email"
for (c <- creds) { for (c <- creds) {
map.put(Integer.parseInt((c \ "priority").text), new Medium("email", (c \ "address").text)) map.put(Integer.parseInt((c \ "priority").text), new Service("email", (c \ "address").text))
} }
map.toMap map.toMap
} }
@ -68,35 +79,64 @@ object AlertHandler extends Application {
} }
} }
private class User(name_ : String, data_ : Map[Int, Medium]) { /**
* This class contains information for the user, such as name, and preferences for Services.
*
* @author CodeForFame
*/
private class User(name_ : String, data_ : Map[Int, Service]) {
def name = name_ def name = name_
def data = data_ def data = data_
} }
private object Medium { /**
* The companion object for the Service class.
* This is where you 'register' services.
*
* @author CodeForFame
*/
private object Service {
var meds = new HashMap[String, (String, String) => Unit] var services = new HashMap[String, (String, String) => Unit]
{ {
meds += (("email", EMail.send _)) services += (("email", EMail.send _))
} }
def send(m: Medium, msg: String) { /**
val pf = meds.get(m.identifier).get * Sends a message via the specified service.
pf(msg, m.recip) */
def send(s: Service, msg: String) {
val pf = services.get(s.identifier).get
pf(msg, s.recip)
} }
} }
private class Medium(identifier_ : String, recip_ : String) { /**
* A class that is for defining a service.
*
* @author CodeForFame
*/
private class Service(identifier_ : String, recip_ : String) {
def identifier = identifier_ def identifier = identifier_
def recip = recip_ def recip = recip_
} }
private trait Protocol { /**
* Services should have this trait, you should override the send method.
*
* @author CodeForFame
*/
private trait ServiceTrait {
def send(msg: String, recip: String) def send(msg: String, recip: String)
} }
private object EMail extends Protocol { /**
* This Service sends an alert via e-mail.
*
* @author CodeForFame
*/
private object EMail extends ServiceTrait {
override def send(msg: String, recip: String) = { override def send(msg: String, recip: String) = {
val props = new Properties() val props = new Properties()
val config = XML.loadFile("alert-config.xml") \\ "credentials" val config = XML.loadFile("alert-config.xml") \\ "credentials"