package eu.socialsensor.framework.client.search;

import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class MediaSearcher {

	public final Logger logger = Logger.getLogger(MediaSearcher.class);
	private String searcherHostname;
	public static final String CHANNEL = "searchRequestsChannel";
	
	
	public MediaSearcher(String searcherHostname) {
		this.searcherHostname = searcherHostname;
	}
	
	public void search(String message) {
		search(message, CHANNEL);
	}
	
	public void delete(String message) {
		delete(message, CHANNEL);
	}

	public void search(String message, String channel) {
		Jedis publisherJedis = null;
		try {
			JedisPoolConfig poolConfig = new JedisPoolConfig();
	        JedisPool jedisPool = new JedisPool(poolConfig, searcherHostname, 6379, 0);
	        
	        publisherJedis = jedisPool.getResource();
	        
			publisherJedis.publish(channel, message);
		}
		catch(Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		finally {
			if(publisherJedis != null)
				publisherJedis.disconnect();
		}
	}
	
	public void delete(String message, String channel) {
		
		Jedis publisherJedis = null;
		try {
			JedisPoolConfig poolConfig = new JedisPoolConfig();
	        JedisPool jedisPool = new JedisPool(poolConfig, searcherHostname, 6379, 0);
	        
	        publisherJedis = jedisPool.getResource();
	        
			publisherJedis.publish(channel, message);
		}
		catch(Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		finally {
			if(publisherJedis != null)
				publisherJedis.disconnect();
		}
	}
	
}

@@ -296,42 +296,8 @@
 s);%0A
-%09private String searcherHostname;%0A
 %09pub
@@ -357,16 +357,82 @@
 nel%22;%0A%09%0A
+%09private String searcherHostname;%0A%09%0A%09private JedisPool jedisPool;%0A
 %09%0A%09publi
@@ -518,16 +518,160 @@
 stname;%0A
+%09%09%0A%09%09JedisPoolConfig poolConfig = new JedisPoolConfig();%0A%09%09%0A        this.jedisPool = new JedisPool(poolConfig, this.searcherHostname, 6379, 0);%0A
 %09%7D%0A%09%0A%09pu
@@ -905,158 +905,8 @@
 y %7B%0A
-%09%09%09JedisPoolConfig poolConfig = new JedisPoolConfig();%0A%09        JedisPool jedisPool = new JedisPool(poolConfig, searcherHostname, 6379, 0);%0A%09        %0A
 %09   
@@ -948,34 +948,24 @@
 Resource();%0A
-%09        %0A
 %09%09%09publisher
@@ -989,32 +989,33 @@
 nnel, message);%0A
+%0A
 %09%09%7D%0A%09%09catch(Exce
@@ -1106,32 +1106,34 @@
 erJedis != null)
+ %7B
 %0A%09%09%09%09publisherJe
@@ -1142,32 +1142,83 @@
 s.disconnect();%0A
+%09%09%09%09jedisPool.returnResource(publisherJedis);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09%0A%09public
@@ -1310,158 +1310,8 @@
 y %7B%0A
-%09%09%09JedisPoolConfig poolConfig = new JedisPoolConfig();%0A%09        JedisPool jedisPool = new JedisPool(poolConfig, searcherHostname, 6379, 0);%0A%09        %0A
 %09   
@@ -1361,18 +1361,8 @@
 ();%0A
-%09        %0A
 %09%09%09p
@@ -1518,16 +1518,18 @@
 != null)
+ %7B
 %0A%09%09%09%09pub
@@ -1554,19 +1554,352 @@
 nect();%0A
+%09%09%09%09jedisPool.returnResource(publisherJedis);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09public static void main(String...args) %7B%0A%09%09MediaSearcher searcher = new MediaSearcher(%22160.40.51.18%22);%0A%09%09%0A%09%09for(int i=0; i%3C10; i++) %7B%0A%09%09%09searcher.search(%22test_%22+i, %22test%22);%0A%09%09%09try %7B%0A%09%09%09%09Thread.sleep(2000);%0A%09%09%09%7D catch (InterruptedException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A
-%09%0A
 %7D%0A

Fix Media Searcher Client