Skip to content

Instantly share code, notes, and snippets.

@dsdstudio
Created August 20, 2013 11:19
Show Gist options
  • Save dsdstudio/6280192 to your computer and use it in GitHub Desktop.
Save dsdstudio/6280192 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* XingAPI Wrapper
* @author : bhkim
* @since : 2013-08-13 오전 2:18
*/
public class Main {
static ByteBuffer mkRequestPkt(int cmd, int gubuncode, String shcode) {
ByteBuffer buffer = ByteBuffer.allocate(15)
.order(ByteOrder.LITTLE_ENDIAN)
.putInt(cmd)
.putInt(gubuncode);
if (shcode != null) {
// C++ 에서는 EOF 문자를 넣어줘야 ZeroTerminated String 으로 인식할수있다.
buffer.put(shcode.getBytes());
buffer.put((byte) 0x00);
}
return buffer;
}
static class DataT9945 {
public Integer gubuncode;
public final String hname;
public final String shcode;
public final String expcode;
public final char etfchk;
public final String filter;
public DataT9945(byte[] buf) {
if (buf == null || buf.length != 64)
throw new IllegalArgumentException("Invalid Argument ");
ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
String s = null;
byte[] tempArr;
try {
tempArr = new byte[40];
byteBuffer.get(tempArr);
s = new String(tempArr, "EUC_KR").trim();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
this.hname = s;
tempArr = new byte[6];
byteBuffer.get(tempArr);
this.shcode = new String(tempArr).trim();
tempArr = new byte[12];
byteBuffer.get(tempArr);
this.expcode = new String(tempArr).trim();
this.etfchk = (char) byteBuffer.get();
tempArr = new byte[5];
byteBuffer.get(tempArr);
this.filter = new String(tempArr).trim();
/*
System.out.println(hname + " "
+ shcode + " "
+ expcode + " "
+ etfchk + " "
+ filter + " ");
*/
}
}
/**
* HeaderProtocol Domain Class
*/
static class HeaderProtocol {
private final short responseType;
private final short errorCode;
private final String trcode;
private final String msg;
public Boolean isError() {
return responseType == 1;
}
public String getTrCode() {
return this.trcode;
}
public String getMsg() {
return this.msg;
}
public HeaderProtocol(InputStream socketInputStream) {
byte[] buf = new byte[60];
byte[] tmpArr;
try {
int readed = socketInputStream.read(buf);
ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
this.responseType = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).getShort();
this.errorCode = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).getShort();
tmpArr = new byte[6];
byteBuffer.order(ByteOrder.LITTLE_ENDIAN).get(tmpArr);
this.trcode = new String(tmpArr).trim();
tmpArr = new byte[50];
byteBuffer.order(ByteOrder.LITTLE_ENDIAN).get(tmpArr);
this.msg = new String(tmpArr, "EUC_KR");
// System.out.println("available : " + socketInputStream.available() + " readed : " + readed);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* 종목리스트 요청
*
* @param socket
* @param gubunCode
* @throws IOException
*/
static List<DataT9945> getStockList(Socket socket, Integer gubunCode) throws IOException {
List<DataT9945> list = new ArrayList<>();
OutputStream os = socket.getOutputStream();
ByteBuffer requestPkt = mkRequestPkt(1, gubunCode, null);
os.write(requestPkt.array());
try (InputStream is = socket.getInputStream()) {
// HEADER pkt 처리
HeaderProtocol protocol = new HeaderProtocol(is);
if (protocol.isError()) {
System.out.println("ERROR => " + protocol.getMsg());
return Collections.emptyList();
}
int read = 0;
byte buf[] = new byte[64];
while ((read = is.read(buf)) != -1) {
DataT9945 data = new DataT9945(buf);
data.gubuncode = gubunCode;
list.add(data);
}
return list;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* t1101
*
* @param socket
* @param gubuncode
* @param shcode
* @throws IOException
*/
static void requestStock(Socket socket, Integer gubuncode, String shcode) throws IOException {
OutputStream os = socket.getOutputStream();
os.write(mkRequestPkt(2, gubuncode, shcode).array());
try (InputStream is = socket.getInputStream()) {
// HEADER pkt 처리
HeaderProtocol protocol = new HeaderProtocol(is);
if (protocol.isError()) {
System.out.println("ERROR => " + protocol.getMsg());
return;
}
int read = 0;
byte buf[] = new byte[950];
while ((read = is.read(buf)) != -1) {
ByteBuffer b = ByteBuffer.wrap(buf);
byte[] tempArr;
tempArr = new byte[20];
b.order(ByteOrder.LITTLE_ENDIAN).get(tempArr);
String hname = new String(tempArr, "EUC_KR").trim();
tempArr = new byte[8];
b.order(ByteOrder.LITTLE_ENDIAN).get(tempArr);
String price = new String(tempArr, "EUC_KR").trim();
System.out.println(hname + " " + price);
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class Monitor implements Runnable {
int count = 0;
private final Socket socket;
public Monitor(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
InputStream is = this.socket.getInputStream();
int read = 0;
byte[] buf = new byte[223];
System.out.println("Running");
while ((read = is.read(buf)) != -1) {
byte[] tmp = new byte[6];
System.arraycopy(buf, 216, tmp, 0, 6);
String shcode = new String(tmp).trim();
tmp = new byte[8];
System.arraycopy(buf, 25, tmp, 0, 8);
String price = new String(tmp).trim();
tmp = new byte[8];
System.arraycopy(buf, 84, tmp, 0, 8);
String cvolume = new String(tmp);
if (shcode.equals("003520")) {
System.out.println("R: 영진약품 " + shcode + " C:" + price + " Q:" + cvolume + " A:" + count);
}
count++;
}
System.out.println("finished");
} catch (IOException e) {
e.printStackTrace();
}
}
}
static void adviseReal(Socket socket, Integer gubuncode, String shcode) throws IOException {
OutputStream os = socket.getOutputStream();
os.write(mkRequestPkt(3, gubuncode, shcode).array());
try (InputStream is = socket.getInputStream()) {
// HEADER pkt 처리
HeaderProtocol protocol = new HeaderProtocol(is);
if (protocol.isError()) {
System.out.println("ERROR => " + protocol.getMsg());
return;
}
byte buf[] = new byte[1024];
int read = 0;
while ((read = is.read(buf)) != -1) {
System.out.println(new String(ByteBuffer.wrap(buf, 10, 50).order(ByteOrder.LITTLE_ENDIAN).array(), "EUC_KR").trim() + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
static final Integer KOSPI = 1;
static final Integer KOSDAQ = 2;
static final int RequestPort = 10001;
static final int RealtimeRequestPort = 10000;
static final ExecutorService service = Executors.newSingleThreadExecutor();
public static void main(String[] args) throws IOException {
List<DataT9945> list = getStockList(new Socket("127.0.0.1", RequestPort), KOSPI);
for (int i = 0, n = list.size(); i < n; i++) {
DataT9945 d = list.get(i);
adviseReal(new Socket("127.0.0.1", RequestPort), KOSPI, d.shcode);
}
list = getStockList(new Socket("127.0.0.1", RequestPort), KOSDAQ);
for (DataT9945 d : list) {
if (d.shcode.length() != 6)
System.out.println(d.shcode + " " + d.hname);
adviseReal(new Socket("127.0.0.1", RequestPort), KOSDAQ, d.shcode);
}
Monitor monitor = new Monitor(new Socket("127.0.0.1", RealtimeRequestPort));
service.submit(monitor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment