Created
February 23, 2014 18:31
-
-
Save Ribesg/9175237 to your computer and use it in GitHub Desktop.
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
package fr.ribesg.alix.network; | |
import fr.ribesg.alix.Tools; | |
import fr.ribesg.alix.api.Server; | |
import org.apache.log4j.Logger; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
/** | |
* This class handles receiving packets. | |
* TODO: All the docz | |
* | |
* @author Ribesg | |
*/ | |
public class SocketReceiver implements Runnable { | |
private static final Logger LOGGER = Logger.getLogger(SocketReceiver.class.getName()); | |
private final BufferedReader reader; | |
private final Server server; | |
private final InternalMessageHandler handler; | |
private boolean stopAsked; | |
private boolean stopped; | |
/* package */ SocketReceiver(final Server server, final BufferedReader reader) { | |
this.reader = reader; | |
this.server = server; | |
this.handler = new InternalMessageHandler(server.getClient()); | |
this.stopAsked = false; | |
this.stopped = true; | |
} | |
@Override | |
public void run() { | |
this.stopped = false; | |
String mes; | |
while (!stopAsked) { | |
Tools.pause(SocketHandler.CHECK_DELAY); | |
try { | |
while ((mes = this.reader.readLine()) != null) { | |
LOGGER.debug("RECEIVED MESSAGE: '" + mes + "'"); | |
handler.handleMessage(server, mes); | |
} | |
} catch (final IOException e) { | |
LOGGER.error("Failed to read Message", e); | |
} | |
LOGGER.debug("Receiver Loop Tick"); | |
} | |
this.kill(); | |
} | |
/* package */ void askStop() { | |
this.stopAsked = true; | |
} | |
/* package */ boolean isStopped() { | |
return this.stopped; | |
} | |
/* package */ void kill() { | |
try { | |
this.reader.close(); | |
} catch (final IOException e) { | |
LOGGER.error("Failed to close Reader stream", e); | |
} | |
this.stopped = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment