Last active
August 13, 2016 22:20
-
-
Save tinkerstudent/9987b6bed8cb521a7e6cd49d230a1fec 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
package com.tinkeracademy.projects; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.io.Reader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.ScheduledFuture; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.StatusLine; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.message.BasicNameValuePair; | |
public class ChatService implements Runnable { | |
public static final String URL = "http://tinkercloud-1273.appspot.com/chat?channel=summer2016"; | |
public File chatFile = null; | |
public File userFile = null; | |
public ScheduledFuture<?> futureTask; | |
public ArrayList<String> pendingChats = null; | |
public enum FileType { | |
USER, | |
CHAT | |
} | |
public enum ChatStatus { | |
YES, | |
NO, | |
ERROR | |
} | |
public ChatStatus init() { | |
if (createFileIfRequired("TinkerAcademyChat.tff", FileType.CHAT) == ChatStatus.ERROR) { | |
return ChatStatus.ERROR; | |
} | |
if (createFileIfRequired("TinkerAcademyUser.tff", FileType.USER) == ChatStatus.ERROR) { | |
return ChatStatus.ERROR; | |
}; | |
pendingChats = new ArrayList<String>(); | |
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | |
futureTask = scheduler.scheduleAtFixedRate(this, 3, 3, TimeUnit.SECONDS); | |
return ChatStatus.YES; | |
} | |
public void run() { | |
if (pendingChats.isEmpty()) { | |
getFromServer(); | |
} else { | |
String line = pendingChats.remove(0); | |
postToServer(line); | |
} | |
} | |
/** | |
* Posts a single line of chat to the server | |
* | |
* @param line | |
* @return Chat Status | |
*/ | |
public ChatStatus postToServer(String line) | |
{ | |
HttpClient client = null; | |
HttpResponse response = null; | |
try { | |
HttpPost post = new HttpPost(URL); | |
List<NameValuePair> params = new ArrayList<NameValuePair>(); | |
params.add( new BasicNameValuePair( "chat", line )); | |
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity( params ); | |
post.setEntity(paramEntity); | |
client = HttpClients.createDefault(); | |
response = client.execute(post); | |
StatusLine statusLine = response.getStatusLine(); | |
int statusCode = statusLine.getStatusCode(); | |
if (statusCode == 200) { | |
ChatStatus status = processResponse(response); | |
return status; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
try { | |
if (client != null) { | |
if (client instanceof CloseableHttpClient) { | |
((CloseableHttpClient) client).close(); | |
} | |
} | |
if (response != null) { | |
if (response instanceof CloseableHttpResponse) { | |
((CloseableHttpResponse) response).close(); | |
} | |
} | |
} catch(IOException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
return ChatStatus.ERROR; | |
} | |
public ChatStatus processResponse(HttpResponse response) { | |
InputStreamReader reader = null; | |
try { | |
InputStream input = response.getEntity().getContent(); | |
reader = new InputStreamReader(input); | |
ArrayList<String> lines = new ArrayList<String>(); | |
ChatStatus status = readLinesFromReader(reader, lines); | |
if (status == ChatStatus.YES) { | |
return writeLines(chatFile, lines); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ChatStatus.ERROR; | |
} | |
public ChatStatus getFromServer() | |
{ | |
HttpClient client = null; | |
HttpResponse response = null; | |
try { | |
HttpGet get = new HttpGet(URL); | |
client = HttpClients.createDefault(); | |
response = client.execute(get); | |
StatusLine statusLine = response.getStatusLine(); | |
int statusCode = statusLine.getStatusCode(); | |
if (statusCode == 200) { | |
return processResponse(response); | |
} | |
} catch(Exception e) { | |
e.printStackTrace(); | |
try { | |
if (client != null) { | |
if (client instanceof CloseableHttpClient) { | |
((CloseableHttpClient) client).close(); | |
} | |
} | |
if (response != null) { | |
if (response instanceof CloseableHttpResponse) { | |
((CloseableHttpResponse) response).close(); | |
} | |
} | |
} catch(IOException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
return ChatStatus.ERROR; | |
} | |
public ChatStatus readLines(File file, ArrayList<String> lines) { | |
FileReader fileReader = null; | |
try { | |
fileReader = new FileReader(file); | |
return readLinesFromReader(fileReader, lines); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (fileReader != null) | |
fileReader.close(); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return ChatStatus.ERROR; | |
} | |
public ChatStatus readLinesFromReader(Reader reader, ArrayList<String> lines) { | |
BufferedReader bufReader = null; | |
try { | |
bufReader = new BufferedReader(reader); | |
String line = bufReader.readLine(); | |
while (line != null) { | |
lines.add(line); | |
line = bufReader.readLine(); | |
} | |
return ChatStatus.YES; | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (bufReader != null) | |
bufReader.close(); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return ChatStatus.ERROR; | |
} | |
public ChatStatus writeLines(File file, ArrayList<String> lines) { | |
for (String line : lines) { | |
ChatStatus status = writeLine(file, line); | |
if (status == ChatStatus.ERROR) { | |
return status; | |
} | |
} | |
return ChatStatus.YES; | |
} | |
public ChatStatus writeLine(File file, String line) { | |
FileWriter fileWriter = null; | |
PrintWriter printWriter = null; | |
try { | |
fileWriter = new FileWriter(file, true); | |
printWriter = new PrintWriter(fileWriter); | |
printWriter.println(line); | |
return ChatStatus.YES; | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (fileWriter != null) { | |
fileWriter.close(); | |
} | |
if (printWriter != null) { | |
printWriter.close(); | |
} | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return ChatStatus.ERROR; | |
} | |
public ChatStatus createFileIfRequired(String name, FileType fileType) { | |
String homeDirectory = System.getProperty("user.home"); | |
File file = new File(homeDirectory, name); | |
try { | |
if (!file.exists()) { | |
if (!file.createNewFile()) { | |
return ChatStatus.ERROR; | |
} | |
} | |
if (fileType == FileType.USER) { | |
userFile = file; | |
} else if (fileType == FileType.CHAT) { | |
chatFile = file; | |
} else { | |
return ChatStatus.ERROR; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return ChatStatus.ERROR; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment