Skip to content

Commit

Permalink
refactor core to decouple log repository and merge delivery and ports
Browse files Browse the repository at this point in the history
packages
  • Loading branch information
RichardWarburton committed Jan 3, 2015
1 parent b8703ab commit e8f3e11
Show file tree
Hide file tree
Showing 60 changed files with 343 additions and 332 deletions.
Expand Up @@ -21,51 +21,65 @@
**/
package com.insightfullogic.honest_profiler.core;

import com.insightfullogic.honest_profiler.core.collector.LogCollector;
import com.insightfullogic.honest_profiler.core.sources.VirtualMachine;
import com.insightfullogic.honest_profiler.core.store.LogRepository;
import com.insightfullogic.honest_profiler.core.store.LogSaver;
import com.insightfullogic.honest_profiler.core.parser.LogParser;
import com.insightfullogic.honest_profiler.core.parser.LogParser.LogState;
import com.insightfullogic.honest_profiler.core.sources.LogSource;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;

import static org.slf4j.LoggerFactory.getLogger;

// TODO: decouple saving from parsing/processing (Possibly remove this class)
public class Conductor {

private final LogRepository logRepository;
private static final long POLL_INTERVAL = 10;

public Conductor(LogRepository logRepository) {
this.logRepository = logRepository;
}
private final Logger logger;
private final LogSource source;
private final LogParser parser;
private final boolean continuous;

public DataConsumer pipeData(VirtualMachine machine, ProfileListener listener) {
return pipe(machine, listener, false);
}
public Conductor(
final Logger logger,
final LogSource source,
final LogParser parser,
final boolean continuous) {

public void pipeFile(File file, VirtualMachine machine, ProfileListener listener) throws IOException {
final LogConsumer logConsumer = new LogConsumer(getLogger(LogConsumer.class), file, pipe(machine, listener, true), true);
new ThreadedAgent(getLogger(ThreadedAgent.class), logConsumer::run).start();
this.logger = logger;
this.source = source;
this.parser = parser;
this.continuous = continuous;
}

public void consumeFile(File file, VirtualMachine machine, ProfileListener listener) throws IOException {
LogConsumer consumer = new LogConsumer(getLogger(LogConsumer.class), file, pipe(machine, listener, false), false);
while (consumer.run())
;
}
public boolean run() throws IOException {
try {
final LogState logState = parser.readRecord(source.read());
switch (logState) {
case END_OF_LOG:
source.close();
return false;
case NOTHING_READ:
if (continuous) {
sleep();
} else {
parser.endOfLog();
}
return continuous;
case READ_RECORD:
return true;
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
parser.endOfLog();
}

private DataConsumer pipe(VirtualMachine machine, ProfileListener listener, boolean continuous) {
LogSaver saver = logRepository.onNewLog(machine);
return false;
}

if (continuous) {
ProfileUpdateModerator moderator = new ProfileUpdateModerator(getLogger(ProfileUpdateModerator.class), listener);
moderator.start();
listener = moderator;
private void sleep() {
try {
Thread.sleep(POLL_INTERVAL);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}

LogCollector collector = new LogCollector(listener, continuous);
return new DataConsumer(getLogger(DataConsumer.class), machine, saver, collector);
}

}

This file was deleted.

This file was deleted.

@@ -0,0 +1,57 @@
/**
* Copyright (c) 2014 Richard Warburton (richard.warburton@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**/
package com.insightfullogic.honest_profiler.core;

import com.insightfullogic.honest_profiler.core.collector.LogCollector;
import com.insightfullogic.honest_profiler.core.parser.LogParser;
import com.insightfullogic.honest_profiler.core.sources.LogSource;

import java.io.IOException;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Application Service for monitoring logs
*/
public class Monitor {

public void pipeFile(final LogSource logSource, final ProfileListener listener) throws IOException {
ProfileUpdateModerator moderator = new ProfileUpdateModerator(getLogger(ProfileUpdateModerator.class), listener);
moderator.start();

final Conductor conductor = pipe(logSource, moderator, true);
new ThreadedAgent(getLogger(ThreadedAgent.class), conductor::run).start();
}

public void consumeFile(final LogSource logSource, final ProfileListener listener) throws IOException {
Conductor consumer = pipe(logSource, listener, false);
while (consumer.run())
;
}

private Conductor pipe(final LogSource logSource, final ProfileListener listener, final boolean continuous) throws IOException {
LogCollector collector = new LogCollector(listener, continuous);
LogParser parser = new LogParser(getLogger(LogParser.class), collector);
return new Conductor(getLogger(Conductor.class), logSource, parser, continuous);
}

}
Expand Up @@ -48,7 +48,7 @@ public LogParser(final Logger logger, final LogEventListener listener) {

public LogState readRecord(ByteBuffer input) throws IOException {
if (!input.hasRemaining()) {
listener.endOfLog();
endOfLog();
return END_OF_LOG;
}

Expand All @@ -73,10 +73,14 @@ public LogState readRecord(ByteBuffer input) throws IOException {
logger.error(e.getMessage(), e);
}

listener.endOfLog();
endOfLog();
return END_OF_LOG;
}

public void endOfLog() {
listener.endOfLog();
}

private void readNewMethod(ByteBuffer input) throws IOException {
Method newMethod = new Method(input.getLong(), readString(input), readString(input), readString(input));
newMethod.accept(listener);
Expand Down
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2014 Richard Warburton (richard.warburton@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**/
package com.insightfullogic.honest_profiler.core.sources;

import java.io.IOException;
import java.nio.ByteBuffer;

public interface LogSource extends AutoCloseable {

public ByteBuffer read();

@Override
public void close() throws IOException;

}

0 comments on commit e8f3e11

Please sign in to comment.