Skip to content

Commit

Permalink
♻️ Split example REPL into sub module
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlm committed Nov 15, 2023
1 parent 87c117b commit 484e288
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 224 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cargo r --example demo_basic_ffi -- example/settings.ini
Run rust full binding example:

```sh
cargo r --example fix_getting_started -- example/server.ini
cargo r --example fix_repl -- acceptor example/server.ini
cargo r --example fix_repl -- initiator example/client.ini
```
Expand Down
4 changes: 2 additions & 2 deletions example/client.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ FileLogPath=log
FileStorePath=store

[SESSION]
BeginString=FIX.4.1
BeginString=FIX.4.4
TargetCompID=SERVER1
StartTime=00:01:00
EndTime=23:59:00
HeartBtInt=20
SocketConnectPort=7071
SocketConnectHost=127.0.0.1
DataDictionary=libquickfix/spec/FIX41.xml
DataDictionary=libquickfix/spec/FIX44.xml
4 changes: 2 additions & 2 deletions example/server.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ FileLogPath=log
FileStorePath=store

[SESSION]
BeginString=FIX.4.1
BeginString=FIX.4.4
TargetCompID=CLIENT1
StartTime=00:01:00
EndTime=23:59:00
HeartBtInt=20
SocketAcceptPort=7071
DataDictionary=libquickfix/spec/FIX41.xml
DataDictionary=libquickfix/spec/FIX44.xml
220 changes: 0 additions & 220 deletions quickfix/examples/fix_repl.rs

This file was deleted.

84 changes: 84 additions & 0 deletions quickfix/examples/fix_repl/command_exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::io::{self, stdin, stdout, BufRead, StdinLock, Write};

use colored::Colorize;
use quickfix::{send_to_target, ConnectionHandler};

use crate::command_parser::ShellCommand;

pub struct FixShell<'a> {
stdin: StdinLock<'a>,
last_command: String,
}

impl FixShell<'_> {
pub fn new() -> Self {
Self {
stdin: stdin().lock(),
last_command: String::with_capacity(1024),
}
}

fn read_user_input(&mut self) -> io::Result<()> {
let mut stdout = stdout().lock();
write!(stdout, "{}> ", "FIX".blue())?;
stdout.flush()?;
drop(stdout);

self.last_command.clear();
self.stdin.read_line(&mut self.last_command)?;

Ok(())
}

fn exec_command<C: ConnectionHandler>(
&mut self,
command: ShellCommand,
connection_handler: &mut C,
) {
match command {
ShellCommand::Help => {
println!("Available commands:");
println!("- status : Print connection handler status");
println!("- start : Start connection handler");
println!("- block : Block connection handler");
println!("- poll : Poll connection handler");
println!("- stop : Stop connection handler");
println!("- send_to K1=V1|K2=V2|… sender target : Create new FIX message");
}
ShellCommand::Start => println!("RESULT: {:?}", connection_handler.start()),
ShellCommand::Stop => println!("RESULT: {:?}", connection_handler.stop()),
ShellCommand::Status => println!(
"Connection handler status: logged_on={:?}, stopped={:?}",
connection_handler.is_logged_on(),
connection_handler.is_stopped(),
),
ShellCommand::Block => println!("RESULT: {:?}", connection_handler.block()),
ShellCommand::Poll => println!("RESULT: {:?}", connection_handler.poll()),
ShellCommand::SendMessage(msg, session_id) => {
println!("Sending {msg:?} to {session_id:?}");
println!("SEND_RESULT: {:?}", send_to_target(msg, &session_id));
}
ShellCommand::NoOperation | ShellCommand::Quit => {}
}
}

pub fn repl<C: ConnectionHandler>(&mut self, connection_handler: &mut C) {
println!(">> Type 'help' or '?' for more information, 'quit' or 'q' to exit.");

loop {
self.read_user_input().expect("I/O error");

// Handle CTRL-D
if self.last_command.is_empty() {
println!("CTRL-D");
break;
}
// Handle other commands
match self.last_command.parse::<ShellCommand>() {
Ok(ShellCommand::Quit) => break,
Ok(cmd) => self.exec_command(cmd, connection_handler),
Err(err) => eprintln!("Error when running command: {}", err.to_string().red()),
}
}
}
}
Loading

0 comments on commit 484e288

Please sign in to comment.