mirror of
https://github.com/Febbweiss/logstash-forwarder-java.git
synced 2026-03-04 22:25:39 +00:00
Added sendEvents method.
This commit is contained in:
20
src/main/java/info/fetter/logstashforwarder/Event.java
Normal file
20
src/main/java/info/fetter/logstashforwarder/Event.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
package info.fetter.logstashforwarder.protocol;
|
package info.fetter.logstashforwarder.protocol;
|
||||||
|
|
||||||
|
import info.fetter.logstashforwarder.Event;
|
||||||
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.KeyManagementException;
|
import java.net.ProtocolException;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.security.KeyStoreException;
|
import java.util.ArrayList;
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.cert.CertificateException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.zip.Deflater;
|
import java.util.zip.Deflater;
|
||||||
@@ -36,10 +35,11 @@ public class LumberjackClient {
|
|||||||
private DataInputStream input;
|
private DataInputStream input;
|
||||||
private int sequence = 1;
|
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.server = server;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
|
||||||
|
try {
|
||||||
keyStore = KeyStore.getInstance("JKS");
|
keyStore = KeyStore.getInstance("JKS");
|
||||||
keyStore.load(new FileInputStream(keyStorePath), null);
|
keyStore.load(new FileInputStream(keyStorePath), null);
|
||||||
|
|
||||||
@@ -56,6 +56,11 @@ public class LumberjackClient {
|
|||||||
|
|
||||||
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||||
input = new DataInputStream(socket.getInputStream());
|
input = new DataInputStream(socket.getInputStream());
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw e;
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int sendWindowSizeFrame(int size) throws IOException {
|
public int sendWindowSizeFrame(int size) throws IOException {
|
||||||
@@ -120,19 +125,31 @@ public class LumberjackClient {
|
|||||||
return 6 + compressor.getTotalOut();
|
return 6 + compressor.getTotalOut();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readAckFrame() throws IOException {
|
public int readAckFrame() throws ProtocolException, IOException {
|
||||||
byte protocolVersion = input.readByte();
|
byte protocolVersion = input.readByte();
|
||||||
if(protocolVersion != PROTOCOL_VERSION) {
|
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();
|
byte frameType = input.readByte();
|
||||||
if(frameType != FRAME_ACK) {
|
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();
|
int sequenceNumber = input.readInt();
|
||||||
return sequenceNumber;
|
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 {
|
public void close() throws IOException {
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user