/// <summary> /// The ResetSession method completely resets an instance /// of a session that is stored in the database. /// </summary> /// <param name="sessionId"> /// The session id of the session to be reset. /// </param> public void ResetSession(string sessionId) { // REC: Ensure that the session is not locked: string sessionPath = Path.Combine(_rootPath, sessionId); string sessionLock = Path.Combine(sessionPath, "Locked.txt"); if (File.Exists(sessionLock)) { string error = string.Format("Session \"{0}\" is locked.", sessionId); throw new InvalidOperationException(error); } // REC: Remove the session's persisted session info: string sessionFile = Path.Combine(sessionPath, "Session.xml"); if (File.Exists(sessionFile)) { File.Delete(sessionFile); } // REC: Remove the session's index file: string sessionIdxFile = Path.Combine(sessionPath, "index.xml"); if (File.Exists(sessionIdxFile)) { File.Delete(sessionIdxFile); } string sessionMsgFile = Path.Combine(sessionPath, "messages.dat"); if (File.Exists(sessionMsgFile)) { File.Delete(sessionMsgFile); } // REC: Create a new session record for the session: XmlFixDatabaseRecord xmlEntry = new XmlFixDatabaseRecord(); xmlEntry.TxSequence = 1; xmlEntry.RxSequence = 1; string[] compIDs = sessionId.Split(new char[] { '-' }); xmlEntry.SenderCompID = compIDs[0]; xmlEntry.TargetCompID = compIDs[1]; // REC: Write the record out to the session file: FileStream fs = new FileStream(sessionFile, FileMode.Create, FileAccess.Write, FileShare.None); XmlSerializer xs = new XmlSerializer(typeof(XmlFixDatabaseRecord)); xs.Serialize(fs, xmlEntry); fs.Close(); }
/// <summary> /// This implementation of the ReleaseSession method is /// invoked to simultaneously release any locks that are /// currently being held on a session and update the info /// for the session in the database. /// </summary> /// <param name="sessionId"> /// The session id of the session being released. /// </param> /// <param name="record"> /// The updated record for the session being released. /// </param> public void ReleaseSession(string sessionId, VfxFixDatabaseRecord record) { string sessionPath = Path.Combine(_rootPath, sessionId); string sessionFile = Path.Combine(sessionPath, "Session.xml"); // REC: Write the updated session details out to // the session record file: XmlFixDatabaseRecord xmlRecord = new XmlFixDatabaseRecord(); xmlRecord.TxSequence = record.TxSequence; xmlRecord.RxSequence = record.RxSequence; FileStream fsSession = new FileStream(sessionFile, FileMode.Create, FileAccess.Write, FileShare.None); XmlSerializer xsSession = new XmlSerializer(typeof(XmlFixDatabaseRecord)); xsSession.Serialize(fsSession, xmlRecord); fsSession.Close(); // REC: Close the index and message database files since // they are no longer needed: SessionDetails sessionDetails = _mapSessions[sessionId]; sessionDetails.IdxWriter.Close(); sessionDetails.MsgWriter.Close(); // REC: Delete the session's lock file: string lockPath = Path.Combine(_rootPath, sessionId); string lockFile = Path.Combine(lockPath, "Locked.txt"); if (File.Exists(lockFile)) { File.Delete(lockFile); } // REC: Remove the session details from the map: _mapSessions.Remove(sessionId); }
/// <summary> /// The AcquireSession method is invoked to request exclusive /// access to a session's information in the database. /// </summary> /// <param name="sessionId"> /// The session's unique identifier. /// </param> /// <returns> /// The persisted information that is maintained for /// the specified session identifier. /// </returns> public VfxFixDatabaseRecord AcquireSession(string sessionId) { VfxFixDatabaseRecord result = new VfxFixDatabaseRecord(); // REC: Determine if the session's file path exists: string sessionPath = Path.Combine(_rootPath, sessionId); if (!Directory.Exists(sessionPath)) { Directory.CreateDirectory(sessionPath); } // REC: Attempt to open and load the database entry // for the specified session: string sessionFile = Path.Combine(sessionPath, "Session.xml"); if (!File.Exists(sessionFile)) { ResetSession(sessionId); } // REC: Attempt to read the session's details from // the persisted information in the session file: FileStream fsSession = new FileStream(sessionFile, FileMode.Open, FileAccess.Read, FileShare.None); XmlSerializer xsSession = new XmlSerializer(typeof(XmlFixDatabaseRecord)); XmlFixDatabaseRecord sessionRecord = xsSession.Deserialize(fsSession) as XmlFixDatabaseRecord; result.TxSequence = sessionRecord.TxSequence; result.RxSequence = sessionRecord.RxSequence; fsSession.Close(); // REC: Create the session's lock file: string lockFile = Path.Combine(sessionPath, "Locked.txt"); FileStream fs = new FileStream(lockFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None); fs.Close(); // REC: Create a new instance of the SessionDetails class // that will be used to maintain the session's information: SessionDetails sessionDetails = new SessionDetails(); // REC: Attempt to open the index file for the session: string idxFile = Path.Combine(sessionPath, "Index.xml"); FileStream idxStream = new FileStream(idxFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); // REC: Maintain a reference to the index file stream // in the session's details record: sessionDetails.IdxWriter = new StreamWriter(idxStream); // REC: Read all of the index entries from the index file // and store them in the session's details structure: StreamReader idxReader = new StreamReader(idxStream); while (idxReader.EndOfStream == false) { string idxLine = idxReader.ReadLine(); string[] idxTokens = idxLine.Split(new char[] { ':' }); IndexEntry idxEntry = new IndexEntry(); idxEntry.msgOffset = int.Parse(idxTokens[1]); idxEntry.msgLength = int.Parse(idxTokens[2]); idxEntry.msgSequence = int.Parse(idxTokens[0]); sessionDetails.Index.Add(idxEntry); } // REC: Attempt to open the message file for the session: string msgFile = Path.Combine(sessionPath, "Messages.txt"); FileStream msgStream = new FileStream(msgFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); // REC: Maintain a reference to the message file stream // in the session's details record: sessionDetails.MsgWriter = new StreamWriter(msgStream); // REC: Add the session details record to the internal // map, keyed by the session identifier: _mapSessions.Add(sessionId, sessionDetails); // REC: Return the session's specifics to the caller: return(result); }