Added sendEvents method.

This commit is contained in:
didfet
2015-03-09 22:50:54 +01:00
parent 0fa6300843
commit b54e3d9f25
2 changed files with 74 additions and 37 deletions

View File

@@ -0,0 +1,20 @@
package info.fetter.logstashforwarder;
import java.util.HashMap;
import java.util.Map;
public class Event {
private Map<String,byte[]> keyValues = new HashMap<String,byte[]>(10);
public void addField(String key, byte[] value) {
keyValues.put(key, value);
}
public void addField(String key, String value) {
keyValues.put(key, value.getBytes());
}
public Map<String,byte[]> getKeyValues() {
return keyValues;
}
}

View File

@@ -1,17 +1,16 @@
package info.fetter.logstashforwarder.protocol;
import info.fetter.logstashforwarder.Event;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.net.ProtocolException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.Deflater;
@@ -36,26 +35,32 @@ public class LumberjackClient {
private DataInputStream input;
private int sequence = 1;
public LumberjackClient(String keyStorePath, String server, int port) throws NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, KeyStoreException, KeyManagementException {
public LumberjackClient(String keyStorePath, String server, int port) throws IOException {
this.server = server;
this.port = port;
keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keyStorePath), null);
try {
keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keyStorePath), null);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
tmf.init(keyStore);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
SocketFactory socketFactory = context.getSocketFactory();
socket = (SSLSocket)socketFactory.createSocket(this.server, this.port);
socket.setUseClientMode(true);
socket.startHandshake();
SocketFactory socketFactory = context.getSocketFactory();
socket = (SSLSocket)socketFactory.createSocket(this.server, this.port);
socket.setUseClientMode(true);
socket.startHandshake();
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
input = new DataInputStream(socket.getInputStream());
} catch(IOException e) {
throw e;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
public int sendWindowSizeFrame(int size) throws IOException {
@@ -120,19 +125,31 @@ public class LumberjackClient {
return 6 + compressor.getTotalOut();
}
public int readAckFrame() throws IOException {
public int readAckFrame() throws ProtocolException, IOException {
byte protocolVersion = input.readByte();
if(protocolVersion != PROTOCOL_VERSION) {
throw new IOException("Protocol version should be 1, received " + protocolVersion);
throw new ProtocolException("Protocol version should be 1, received " + protocolVersion);
}
byte frameType = input.readByte();
if(frameType != FRAME_ACK) {
throw new IOException("Frame type should be Ack, received " + frameType);
throw new ProtocolException("Frame type should be Ack, received " + frameType);
}
int sequenceNumber = input.readInt();
return sequenceNumber;
}
public int sendEvents(List<Event> eventList) throws IOException {
int beginSequence = sequence;
int numberOfEvents = eventList.size();
List<Map<String,byte[]>> keyValuesList = new ArrayList<Map<String,byte[]>>(numberOfEvents);
for(Event event : eventList) {
keyValuesList.add(event.getKeyValues());
}
sendCompressedFrame(keyValuesList);
while(readAckFrame() < sequence) {}
return sequence - beginSequence;
}
public void close() throws IOException {
socket.close();
}