diff --git a/src/org/ntlab/radishforandroidstudio/framework/network/CallBack.java b/src/org/ntlab/radishforandroidstudio/framework/network/CallBack.java deleted file mode 100644 index 1e9238b..0000000 --- a/src/org/ntlab/radishforandroidstudio/framework/network/CallBack.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.ntlab.radishforandroidstudio.framework.network; - - -public interface CallBack { - public void onResponse(String response); -} diff --git a/src/org/ntlab/radishforandroidstudio/framework/network/HttpAsyncConnection.java b/src/org/ntlab/radishforandroidstudio/framework/network/HttpAsyncConnection.java index 0df92c3..73c0d22 100644 --- a/src/org/ntlab/radishforandroidstudio/framework/network/HttpAsyncConnection.java +++ b/src/org/ntlab/radishforandroidstudio/framework/network/HttpAsyncConnection.java @@ -16,7 +16,6 @@ private String queryParams = ""; private String pathParams = ""; private String formParams = ""; - private CallBack callBack = null; private String method; private static String clientSessionID = null; @@ -41,10 +40,6 @@ setMethod("DELETE"); } - public void setCallBack(CallBack callBack) { - this.callBack = callBack; - } - private void setMethod(String method) { this.method = method; } diff --git a/src/org/ntlab/radishforandroidstudio/framework/network/HttpClient.java b/src/org/ntlab/radishforandroidstudio/framework/network/HttpClient.java deleted file mode 100644 index 1f21afe..0000000 --- a/src/org/ntlab/radishforandroidstudio/framework/network/HttpClient.java +++ /dev/null @@ -1,262 +0,0 @@ -package org.ntlab.radishforandroidstudio.framework.network; - -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.logging.Logger; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.LinkedBlockingQueue; - -/** - * * * @author ���� �a�P * - */ -public abstract class HttpClient { - - private URL url; - private static String clientSessionID; - // ���b�Z�[�WID���L�[�Ƃ��� - private Map HttpReceiverContainer; - private Queue HttpMessageContainer; - private String sendMsg; - private long latestSentMsgID; - private int maxConnection; - /** - * ���V�[�o�[�̐������E���ԁi�~���b�j - */ - private long receiverAliveTime; - - public HttpClient(String url) { - try { - this.url = new URL(url); - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - this.latestSentMsgID = -1L; - // �f�t�H���g�̍ő�R�l�N�V������ - this.maxConnection = 5; - // �f�t�H���g�̃��V�[�o�[�̐������ԁi�~���b�j - this.receiverAliveTime = 60000L; - this.HttpReceiverContainer = // �X���b�h�Z�[�t��Map - new ConcurrentHashMap(); - this.HttpMessageContainer = new LinkedBlockingQueue(); - } - - /** - * ���V�[�o�[��new���邽�߂����̃N���X - * - * @param http - * :���V�[�o�[�̃R���X�g���N�^�ɓn�� - * @param msgID - * :���V�[�o�[�̃R���X�g���N�^�ɓn�� - * @param client - * :���V�[�o�[�̃R���X�g���N�^�ɓn�� - * @return ���V�[�o�[�̃C���X�^���X - */ - protected abstract HttpReceiver createReceiver(HttpURLConnection http, String msgID, HttpClient client); - - /** - * �T�[�o�[�ւ̍ő�ڑ�����ݒ肷�� - * - * @param maxConnection - * :�T�[�o�ւ̍ő�ڑ����i�f�t�H���g��5�j - */ - public void setMaxConnection(int maxConnection) { - this.maxConnection = maxConnection; - } - - /** - * ���V�[�o�[�̐����”\���Ԃ�ݒ肷�� ���̎��Ԃ��߂���ƁA��M�𒆎~���܂� - * - * @param aliveTime - */ - public void setReceiverAliveTime(long aliveTime) { - this.receiverAliveTime = aliveTime; - } - - /** - * �T�[�o�[�ɑ���f�[�^�����b�Z�[�W�Ƃ��ăZ�b�g���� - * - * @param name - * :�p�����[�^�� - * @param value - * :�p�����[�^�̒l - */ - public void setParameter(String name, String value) { - if (this.sendMsg == null) { - this.sendMsg = name + "=" + value; - } else { - this.sendMsg += ("&" + name + "=" + value); - } - } - - /** - * �T�[�o�[�Ƀ��b�Z�[�W�𑗐M���� �i���b�Z�[�W��setParameter()�ō쐬�����j - * - * @return:���b�Z�[�WID - */ - public String send() { - try { - this.latestSentMsgID++; - if (this.latestSentMsgID == Long.MAX_VALUE) { - // long�����E�l�ɒB�����烊�Z�b�g - this.latestSentMsgID = 0L; - } - if (this.maxConnection < this.HttpReceiverContainer.size()) { - this.HttpMessageContainer.offer(new HttpMessage(Long.toString(this.latestSentMsgID), this.sendMsg)); - } else { - HttpReceiver r = this.createReceiver(this.sendMsgToServer(this.sendMsg), - Long.toString(this.latestSentMsgID), this); - r.start(); - this.HttpReceiverContainer.put(r.getMsgID(), r); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - // ������ - this.sendMsg = null; - // ���b�Z�[�WID ��Ԃ� - return Long.toString(this.latestSentMsgID); - } - - /** - * msgID�ɍ��v����Receiver��ArrayList����폜���� ��HttpReceiver�ȊO����̌Ăяo���֎~�� - * - * @param msgID - * :���b�Z�[�WID - */ - void removeTerminatedReceiver(String msgID) { - this.HttpReceiverContainer.remove(msgID); - try { - /* HttpMessageContainer�ɕۗ�����Ă��郁�b�Z�[�W���������� */ - HttpMessage httpMsg; - if ((httpMsg = this.HttpMessageContainer.poll()) != null) { - HttpReceiver r = this.createReceiver(this.sendMsgToServer(httpMsg.getMessage()), httpMsg.getMsgID(), - this); - r.start(); - this.HttpReceiverContainer.put(r.getMsgID(), r); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * �T�[�o�[����̕ԐM����M�������ǂ�����Ԃ� - * - * @param msgID - * :���b�Z�[�WID - * @return true :��M�ς� false :��M�� / - */ - public boolean hasReceived(String msgID) { // HttpMessageContainer���猟�� - int arraySize = this.HttpMessageContainer.size(); - HttpMessage[] arrayHttpMessage = this.HttpMessageContainer.toArray(new HttpMessage[arraySize]); - for (int i = 0; i < arraySize; i++) { - // �L���[��z��ɂ��āA�S�Ă̗v�f��msgID���`�F�b�N���Ă��� - if (arrayHttpMessage[i].getMsgID().equals(msgID)) { - return false; - } - } - /* - * HttpReceiverContainer���猟�� - */ - // �X���b�h�̊J�n���Ԃ��`�F�b�N���āA���Ԃ��o�������Ă�����j������ - HttpReceiver receiver = this.HttpReceiverContainer.get(msgID); - if (receiver != null) { - long nowTime = System.currentTimeMillis(); - long passedTime = nowTime - receiver.getStartTime(); - if (this.receiverAliveTime < (nowTime - receiver.getStartTime())) { - // ���V�[�o�[�����X�g����폜���� - this.removeTerminatedReceiver(msgID); - // ��M�𒆒f���� - receiver.stopReceive(); - // �X���b�h�����荞�ݏ�Ԃɂ��� - receiver.interrupt(); - // �x����\�� - Logger.getLogger(Logger.GLOBAL_LOGGER_NAME) - .warning("Connection time out: " + Long.toString(passedTime) + " (ms)"); - } - // HttpReceiver�̃��X�g�Ɋ܂܂�Ă�����false��Ԃ� - return false; - } - /* - * HttpMessageContainer��HttpReceiverContainer����A �w���msgID�����‚���Ȃ��ꍇ�́A ��M�����������Ƃ݂Ȃ� - * �i���݂��Ȃ�msgID����M���������Ƃ݂Ȃ����j - */ - return true; - } - - /** - * msgID�Ŏw�肵�����b�Z�[�W���A���Ă���܂ŁA�҂� - * - * @param msgID - * :���b�Z�[�WID - */ - public void waitForResponse(String msgID) { - try { - while (!this.hasReceived(msgID)) { - Thread.sleep(1L); - } - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public HttpReceiver getReceiver(String msgID) { - return this.HttpReceiverContainer.get(msgID); - } - - /** - * �T�[�o�[�Ƀ��b�Z�[�W�𑗐M���A��M��ԂɈڍs����HttpURLConnection��Ԃ� - * - * @param sendMsg - * :�T�[�o�[�ɑ��郁�b�Z�[�W - * @return ���b�Z�[�W���M�ς݂�HttpURLConncection - * @throws IOException - */ - private HttpURLConnection sendMsgToServer(String sendMsg) throws IOException { - /* - * HttpURLConnection�́A �C���X�^���X�����˃T�[�o�[�ւ̃��N�G�X�g�i�f�[�^���M�j�˃T�[�o�[����̃��X�|���X�i�f�[�^��M�j - * �Ƃ����T�C�N�����Ƃ�Ȃ��Ƃ����Ȃ��B - */ - HttpURLConnection http; - http = (HttpURLConnection) this.url.openConnection(); // �T�[�o�[�ɐڑ� - http.setRequestMethod("POST"); - http.setDoOutput(true); - // keepAlive���Ȃ��ݒ� - // http.setRequestProperty("Connection", "close"); - if (this.clientSessionID != null) { - // �Z�b�V����ID���ݒ肳��Ă�����t�����đ��M - http.setRequestProperty("Cookie", this.clientSessionID); - } - // ���b�Z�[�W���M - BufferedWriter writer = new BufferedWriter( - // ���{��̕��������h�~�̂��߃T�[�o�[�ɑ��M���镶���R�[�h���w�肷�� - new OutputStreamWriter(http.getOutputStream(), "UTF-8")); - if (sendMsg == null) { - // sendMsg��null�Ȃ�A�����Ȃ�������𑗂� - sendMsg = ""; - } - writer.write(sendMsg); - writer.flush(); - writer.close(); - /* - * �f�[�^�̑��M�̑O�ɁA�f�[�^�̎�M�͏o���Ȃ��̂ŁA�����ɋL�q���� - */ - if (this.clientSessionID == null) { - // �Z�b�V�����������Ƃ��́A�ݒ肵�Ă��� - this.clientSessionID = http.getHeaderField("Set-Cookie"); - } - return http; - } -} \ No newline at end of file diff --git a/src/org/ntlab/radishforandroidstudio/framework/network/HttpMessage.java b/src/org/ntlab/radishforandroidstudio/framework/network/HttpMessage.java deleted file mode 100644 index c35b0e7..0000000 --- a/src/org/ntlab/radishforandroidstudio/framework/network/HttpMessage.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.ntlab.radishforandroidstudio.framework.network; - -public class HttpMessage { - - private String msgID; - private String message; - - public HttpMessage(String msgID, String message) { - this.msgID = msgID; - this.message = message; - } - - public String getMsgID() { - return this.msgID; - } - - public String getMessage() { - return this.message; - } -} \ No newline at end of file diff --git a/src/org/ntlab/radishforandroidstudio/framework/network/HttpReceiver.java b/src/org/ntlab/radishforandroidstudio/framework/network/HttpReceiver.java deleted file mode 100644 index 29bf465..0000000 --- a/src/org/ntlab/radishforandroidstudio/framework/network/HttpReceiver.java +++ /dev/null @@ -1,143 +0,0 @@ -package org.ntlab.radishforandroidstudio.framework.network; - -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - -/** - * * HTTP�ɂ��T�[�o�[����̃��b�Z�[�W�̎�M�y�уt�@�C���̃_�E�����[�h���o����N���X * @author ���� �a�P * - */ -public abstract class HttpReceiver extends Thread { - - private final HttpURLConnection http; - private final String msgID; - private HttpClient httpClient; - /** - * ���̃X���b�h���J�n�������� - */ - private long taskStartTime; - - public HttpReceiver(HttpURLConnection http, String msgID, HttpClient client) { - this.http = http; - this.msgID = msgID; - this.httpClient = client; - // �X���b�h���J�n�����܂ł̃^�C�����O������̂ŁA�C���X�^���X�����܂ꂽ���Ԃ��L�^ - this.taskStartTime = System.currentTimeMillis(); - } - - public abstract void recieve(String s); - - public void run() { - // �J�n���Ԃ��L�^ - this.taskStartTime = System.currentTimeMillis(); - try { - // �T�[�o�[����̕ԐM����M���A���V�[�o�[�ɓn�� - String s = this.receiveFromServer(http); - recieve(s); - // System.out.println(s); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - // ���g���AHttpClient��List����폜 - this.httpClient.removeTerminatedReceiver(this.msgID); - } - - /** - * ���b�Z�[�WID���擾���� - * - * @return ���b�Z�[�WID - */ - public String getMsgID() { - return this.msgID; - } - - /** - * @return ���̃X���b�h���J�n�������� - */ - long getStartTime() { - return this.taskStartTime; - } - - /** - * �t�@�C���̃_�E�����[�h������ - * - * @param url - * �T�[�o�[��̃t�@�C����URL - * @param outputFilePath - * �����o����̃p�X - */ - protected void download(String url, String outputFilePath) { - try { - // �����̃p�X�����ɐ�΃p�X��File�̃C���X�^���X����� - File outputFile = (new File(outputFilePath)).getAbsoluteFile(); - // �t�@�C���ۑ���ɕK�v�ȃf�B���N�g�����쐬 - outputFile.getParentFile().mkdirs(); - System.out.println("download from " + url + "\n save to " + outputFile.getAbsolutePath()); - /* - * �ۑ�����t�@�C���� FileOutputStream ���\�z���܂��B - */ - FileOutputStream fos = new FileOutputStream(outputFile); - HttpURLConnection urlConnection = (HttpURLConnection) (new URL(url)).openConnection(); - urlConnection.connect(); - // �t�@�C���T�C�Y���擾 - int fileSize = urlConnection.getContentLength(); - int num; - // �t�@�C�������o�C�g�P�ʂŎ�M���邩��ݒ肷�� - byte buf[] = new byte[fileSize]; - /* DataInputStream���g�p���ăt�@�C���ɏ����o���܂��B */ - DataInputStream dis = new DataInputStream(urlConnection.getInputStream()); - while ((num = dis.read(buf)) != -1) { - fos.write(buf, 0, num); - } - dis.close(); - fos.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * �T�[�o�[����̃��b�Z�[�W����M���邽�߂̃��[�_�[ ���̃X���b�h�����~���ꂽ�ꍇ�AHttpClient���烊�[�_�[��close���閽�߂𑗂邽�� �����ɒu�� - */ - private BufferedReader reader; - - /** - * �T�[�o�[����̕ԐM����M���� - * - * @param http - * :���b�Z�[�W���T�[�o�[�ɑ��M�ς݂�HttpURLConnection - * @return �T�[�o�[����̕ԐM - * @throws IOException - */ - private String receiveFromServer(HttpURLConnection http) throws IOException { - this.reader = new BufferedReader( - // ���{��̕��������h�~�̂��߂ɂ����ŕ����R�[�h���w�肷�� - new InputStreamReader(http.getInputStream(), "UTF-8")); - StringBuffer buf = new StringBuffer(); - // readLine��null�ɂȂ�܂ő����� - for (String line = ""; line != null; line = this.reader.readLine()) { - // �V����line��A�� - buf.append(line); - } - this.reader.close(); - return buf.toString(); - } - - /** - * ��M�𒆎~���� - */ - void stopReceive() { - try { - this.reader.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } -} \ No newline at end of file