Ejemplo n.º 1
0
    def __init__(self, journalfile=None):
        self.journaller = Journaler(journalfile)
        self.sessions = {}

        # We load all sessions from the journal and add to our list
        for session in self.journaller.sessions():
            self.sessions[session.key] = session
Ejemplo n.º 2
0
def main():
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

    parser = argparse.ArgumentParser(
        description='Examine the contents of the store file.')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-l',
                       '--list',
                       dest='list',
                       action='store_true',
                       help='list available streams')
    group.add_argument('-s',
                       '--sessions',
                       dest='sessions',
                       nargs='+',
                       action='store',
                       metavar=('s1', 's2'),
                       help='session to examine')
    parser.add_argument('filename',
                        action='store',
                        help='filename of the store file')
    parser.add_argument('-d',
                        '--direction',
                        dest='direction',
                        choices=['in', 'out', 'both'],
                        action='store',
                        default="both",
                        help='filename of the store file')

    args = parser.parse_args()

    journal = Journaler(args.filename)

    if args.list is True:
        # list all sessions
        row_format = "{:^15}|" * 3
        separator_format = "{:->15}|" * 3
        for session in journal.sessions():
            print(
                row_format.format("Session Id", "TargetCompId",
                                  "SenderCompId"))
            print(separator_format.format("", "", ""))
            print(
                row_format.format(session.key, session.targetCompId,
                                  session.senderCompId))
        print(separator_format.format("", "", ""))
    else:
        # list all messages in that stream
        direction = None if args.direction == 'both' else MessageDirection.INBOUND if args.direction == "in" else MessageDirection.OUTBOUND
        for (seqNo, msg, msgDirection,
             session) in journal.getAllMsgs(args.sessions, direction):
            d = "---->" if msgDirection == MessageDirection.OUTBOUND.value else "<----"
            print("{:>3} {:^5} [{:>5}] {}".format(session, d, seqNo, msg))
Ejemplo n.º 3
0
class FIXEngine(object):
    def __init__(self, journalfile=None):
        self.journaller = Journaler(journalfile)
        self.sessions = {}

        # We load all sessions from the journal and add to our list
        for session in self.journaller.sessions():
            self.sessions[session.key] = session

    def validateSession(self, targetCompId, senderCompId):
        # this make any session we receive valid
        return True

    def shouldResendMessage(self, session, msg):
        # we should resend all application messages
        return True

    def createSession(self, targetCompId, senderCompId):
        if self.findSessionByCompIds(targetCompId, senderCompId) is None:
            session = self.journaller.createSession(targetCompId, senderCompId)
            self.sessions[session.key] = session
        else:
            raise RuntimeError("Failed to add session with duplicate key")
        return session

    def getSession(self, identifier):
        try:
            return self.sessions[identifier]
        except KeyError:
            return None

    def findSessionByCompIds(self, targetCompId, senderCompId):
        sessions = [
            x for x in self.sessions.values() if x.targetCompId == targetCompId
            and x.senderCompId == senderCompId
        ]
        if sessions is not None and len(sessions) != 0:
            return sessions[0]
        return None

    def getOrCreateSessionFromCompIds(self, targetCompId, senderCompId):
        session = self.findSessionByCompIds(targetCompId, senderCompId)
        if session is None:
            if self.validateSession(targetCompId, senderCompId):
                session = self.createSession(targetCompId, senderCompId)

        return session
Ejemplo n.º 4
0
    def testAddExtractMsg(self):
        journal = Journaler()

        msg = FIXMessage("AB")
        msg.setField("45", "dgd")
        msg.setField("32", "aaaa")
        msg.setField("323", "bbbb")

        rptgrp1 = FIXContext()
        rptgrp1.setField("611", "aaa")
        rptgrp1.setField("612", "bbb")
        rptgrp1.setField("613", "ccc")

        msg.addRepeatingGroup("444", rptgrp1, 0)
        session = FIXSession(1, "S1", "T1")
        for i in range(0, 5):
            msg.setField("34", str(i))
            journal.persistMsg(msg, session, MessageDirection.OUTBOUND)

        msg = journal.recoverMsg(session, MessageDirection.OUTBOUND, 1)