Created
July 8, 2009 17:36
-
-
Save hakobe/143000 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import scala.actors.Actor | |
| import scala.actors.Actor._ | |
| import scala.collection.mutable.ListBuffer | |
| import scala.io.Source | |
| import java.net._ | |
| import java.io._ | |
| object Fetcher { | |
| var pathPrefix = "/tmp/" | |
| var urlPrefix = "" | |
| val actors :ListBuffer[Actor] = new ListBuffer() | |
| def fetch(urlString :String) = { | |
| val url = new URL(urlString) | |
| val inputSrc = Source.fromURL(url) // cool | |
| val content = inputSrc.mkString("") | |
| write(getName(url.getPath()), content) | |
| } | |
| def getName(path :String) = { | |
| val fragments = path.split("/") | |
| fragments.last | |
| } | |
| // ださい… | |
| def write(name :String, content :String) = { | |
| var writer :BufferedWriter = null; | |
| try { | |
| writer = new BufferedWriter(new FileWriter(pathPrefix + name)); | |
| writer.write(content, 0, content.length) | |
| } | |
| catch { | |
| case e :Exception => | |
| e.printStackTrace() | |
| } | |
| finally { | |
| writer.flush(); | |
| writer.close(); | |
| } | |
| } | |
| def prepare(urlString :String) = { | |
| val a = actor { | |
| react { case "go" => | |
| fetch(urlString) | |
| } | |
| } | |
| actors.append(a) | |
| } | |
| def start = { | |
| actors.foreach( a => a ! "go" ) | |
| } | |
| def main (args:Array[String]) = { | |
| pathPrefix = args(0) | |
| urlPrefix = args(1) | |
| val isAsync = args.length > 2 && args(2) == "async" | |
| var method : Int => Unit = | |
| if (isAsync) ({ x => prepare(urlPrefix + x) }) | |
| else ({ x => fetch(urlPrefix + x) }) | |
| (0 to 14).foreach( method ) | |
| start | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment