removed server; better randomrefill
This commit is contained in:
parent
25c82acfd9
commit
d3fa21cbc0
|
@ -23,7 +23,6 @@ public class DB implements Runnable {
|
|||
private Logger log = Logger.getLogger(DB.class.getName());
|
||||
private ArrayList<String> randombuffer = new ArrayList<>(100);
|
||||
private Random rand = new Random();
|
||||
private Server serv = new Server(this);
|
||||
private Thread randomrefill = null;
|
||||
private int dbsize = 0;
|
||||
|
||||
|
@ -60,7 +59,6 @@ public class DB implements Runnable {
|
|||
log.info("Database is set up!");
|
||||
}
|
||||
|
||||
serv.start();
|
||||
refillbuffer();
|
||||
|
||||
//get db size
|
||||
|
@ -303,7 +301,6 @@ public class DB implements Runnable {
|
|||
* Stops the randomnes-Server and disconnect
|
||||
*/
|
||||
public void stop() {
|
||||
serv.stop();
|
||||
try {
|
||||
if(con != null) {
|
||||
if(!con.isClosed()) {
|
||||
|
@ -322,23 +319,18 @@ public class DB implements Runnable {
|
|||
@Override
|
||||
public void run() {
|
||||
log.info("Started Refilling.");
|
||||
ResultSet count = query("SELECT `" + db + "`.`getLimit`() as 'l';");
|
||||
if(count != null) {
|
||||
try {
|
||||
if(count.next()) {
|
||||
int max = count.getInt("l");
|
||||
ResultSet set = query("SELECT `id` FROM `videos` LIMIT " + rand.nextInt(max) + ",100;");
|
||||
if(set != null) {
|
||||
while(set.next()) {
|
||||
randombuffer.add(set.getString(1));
|
||||
}
|
||||
log.info("refilled randombuffer to " + randombuffer.size() + " videos.");
|
||||
ResultSet set = query("SELECT `id` FROM `videos` ORDER BY rand() LIMIT 100;");
|
||||
if(set != null) {
|
||||
while(set.next()) {
|
||||
randombuffer.add(set.getString(1));
|
||||
}
|
||||
log.info("refilled randombuffer to " + randombuffer.size() + " videos.");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.warn("error getting a random video", e);
|
||||
}
|
||||
}
|
||||
|
||||
if(randombuffer.isEmpty()) {
|
||||
log.error("Unable to retrieve RandomVideos");
|
||||
}
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
package de.mrbesen.youtubecrawler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class Server implements Runnable {
|
||||
|
||||
private ServerSocket ssoc;
|
||||
private Thread t;
|
||||
private boolean run = false;
|
||||
private Logger log = Logger.getLogger(this.getClass().getName());
|
||||
private DB db;
|
||||
|
||||
public Server(DB d) {
|
||||
db = d;
|
||||
}
|
||||
|
||||
void start() {
|
||||
run = true;
|
||||
t = new Thread(this, "Server");
|
||||
t.start();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
run = false;
|
||||
t.interrupt();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
final int port = 2419;
|
||||
ssoc = new ServerSocket(port, -1);
|
||||
//ssoc.bind(new InetSocketAddress(, port));
|
||||
ssoc.setSoTimeout(5);
|
||||
log.info("opened Server at port " + port);
|
||||
while(run) {
|
||||
try {
|
||||
Socket client = ssoc.accept();
|
||||
//if(client.getInetAddress().isLoopbackAddress()) {
|
||||
PrintWriter out = new PrintWriter(client.getOutputStream());
|
||||
out.println(db.getRandom());
|
||||
out.flush();
|
||||
out.close();
|
||||
/*} else {
|
||||
log.info("client connected: " + client.getInetAddress().toString());
|
||||
}*/
|
||||
client.close();
|
||||
} catch(SocketTimeoutException ignored) {}
|
||||
}
|
||||
ssoc.close();
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue