C# Simulation of Intent Locking Protocols
来源:互联网 发布:淘宝莱蔻怎么那么便宜 编辑:程序博客网 时间:2024/06/14 12:01
The Main.cs code:
using System;using System.Threading;using System.IO;using System.Collections.Generic;using System.Linq;namespace NS_intentlocker{ #region "All Enums" public enum Status { Eligible, Running, Committed, Faulty } public enum BasesetNames { A, B, C, D, E, F, G, H, I, J } public enum BasesetTypes { S, X, IS, IX, SIX } public enum operations { R, A, S, M, D } #endregion #region "Custom class for list" //[Serializable] public class collections { public int numbers { get; set; } public string operation_code { get; set; } public bool locks { get; set; } public collections(int numbers, string operation_code, bool locks = false) { this.numbers = numbers; this.operation_code = operation_code; this.locks = locks; } }public class transcations{ public int ID { get; set; } public Status status{get;set;} public int extra_time_ticks {get; set;} public int operation_lines {get; set;} public int intent_locks { get; set; } public string BasesetName { get; set; } public string BasesetType { get; set; } public List<collections> cols { get; set; } //public int count { get; set; } public transcations() { } } #endregion //########################################################################################## //########################## Main Operation Class ######################################### //##########################################################################################public class Intent_Locker_implement{ static void Main(string[] args) { List<transcations> tsc = new List<transcations>(); // declare the list by transcation type #region "Read the data from script and save into the list" //**************************** Read the data from script and save into the list *************************************** List<string> tmp = new List<string>(); try { using (StreamReader sr = new StreamReader("five.txt")) // here for change different txt file name { String line; //int test = 0; int ids = 0; while ((line = sr.ReadLine()) != null) { if ((line = sr.ReadLine()) != "") // filter & skip the empty line { string[] items = line.Split(new Char[] { ' ', ',', '.', ':' }); // split to the element var t1 = new transcations(); // declare a new object t1.ID = ids; ++ids; t1.status = Status.Eligible; // the default status set as Eligible t1.extra_time_ticks = int.Parse(items[0]); t1.operation_lines = int.Parse(items[1]); t1.intent_locks = int.Parse(items[2]); t1.BasesetName = items[3]; t1.BasesetType = items[4]; //********************* get the operation line record ************************** List<collections> operationList = new List<collections>(); // create the list of collections for (int i = 0; i < int.Parse(items[1]); i++) { line = sr.ReadLine(); string[] items2 = line.Split(new Char[] { ' ', ',', '.', ':' }); collections cols = new collections(int.Parse(items2[0]), items2[1]); //cols.numbers = int.Parse(items2[0]); //cols.operation_code = items2[1]; operationList.Add(cols); } t1.cols = operationList; //****************************************************************************** tsc.Add(t1); // add the transcation to the list tsc } } } } catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } #endregion #region "testing part" //Console.WriteLine(tsc[1].cols[1].operation_code); // test read file successful :) //Console.WriteLine(tsc.Count()); //Console.WriteLine(tsc[0].status); #endregion #region "start the Simulation of Intent Locking Protocols" //***************** Simulation of Intent Locking Protocols *********************************** int TranscationCount = tsc.Count(); // get the count of the transcations int[] rollback = new int[TranscationCount]; // declare the arrays for save the count of rollback or completed int[] completed = new int[TranscationCount]; for (int n = 0; n < TranscationCount; n++) { rollback[n] = 0; completed[n] = 0; } // start the tick for (int ticks = 0; ticks < 1000; ticks++) { bool[] _array = new bool[1000]; // declear an array of boolean, used for save operation status for (int n = 0; n < TranscationCount; n++) { if (ticks >= tsc[n].extra_time_ticks && ticks < tsc[n].cols.Count() + tsc[n].extra_time_ticks) { tsc[n].status = Status.Running; // turn on the transcations // turn on the operation //Console.WriteLine(n + "/" + ticks); //if (tsc[n].cols[ticks - tsc[n].extra_time_ticks].operation_code != "R") //{ if (_array[tsc[n].cols[ticks - tsc[n].extra_time_ticks].numbers] == true) // if is true, that means this operation is using at moment, rollback! { //tsc[n].rollback = tsc[n].rollback++; // the rollback count of this transcation will be increase one rollback[n] = rollback[n]+1; } else { _array[tsc[n].cols[ticks - tsc[n].extra_time_ticks].numbers] = true; //otherwise set it as true //tsc[n].completed = tsc[n].completed++; // the complete count will increase one completed[n] = completed[n]+1; } //} // } ////****************** Verify the transcation committed or faulty *************************************** if (ticks >= tsc[n].extra_time_ticks + tsc[n].cols.Count() && rollback[n] == 0) { tsc[n].status = Status.Committed; //if rollback is 0, set status as commited } else if (rollback[n] > 0) { tsc[n].status = Status.Faulty; // if have any rollback, set status as faulty } ////****************************************************************************************************** } //************** output the result for each ticks ***************************** Console.WriteLine("*********************************************"); Console.WriteLine("Current ticks is: " + ticks); for (int n = 0; n < TranscationCount; n++) { Console.WriteLine("Transcation[" + (n + 1) + "] status is :" + tsc[n].status); Console.WriteLine("The Number of Rollback is:" + rollback[n]); Console.WriteLine("The Number of Completed is:" + completed[n]); } } #endregion #region "Finally output" //************** Finally output ***************************** // ouput on the screen Console.WriteLine("########################################"); Console.WriteLine("####### Summary Output #########"); ///Console.WriteLine("Current ticks is: " + ticks); for (int n = 0; n < TranscationCount; n++) { Console.WriteLine("Transcation[" + (n + 1) + "] status is :" + tsc[n].status); Console.WriteLine("The Number of Rollback is:" + rollback[n]); Console.WriteLine("The Number of Completed is:" + completed[n]); } // write in the log file using (StreamWriter sw = new StreamWriter("five.log")) { sw.WriteLine("########################################"); sw.WriteLine("####### Summary Output #########"); ///Console.WriteLine("Current ticks is: " + ticks); for (int n = 0; n < TranscationCount; n++) { sw.WriteLine("Transcation[" + (n + 1) + "] status is :" + tsc[n].status); sw.WriteLine("The Number of Rollback is:" + rollback[n]); sw.WriteLine("The Number of Completed is:" + completed[n]); } } #endregion }}}
The Text script for read:
2 8 1 A SIX
044 R
045 R
046 R
047 R
047 A
046 S
045 S
044 A
3 8 1 A SIX
024 R
034 R
044 R
054 R
054 A
044 A
034 A
911 R
- C# Simulation of Intent Locking Protocols
- Java: Simulation of printStackTr…
- Protocols
- RFC3117 - On the Design of Application Protocols
- A drop of performance testing- more protocols
- Protocols in different layers of a network
- Functions of Protocols and Transmission Speed
- HTTP/Socket .etc protocols implements in C#
- Hotkey of locking screen on OS X
- Several workaround in mips simulation of SkyEye
- 【DataStructure】One of queue usage: Simulation System
- List of discrete event simulation software
- C# 4.0 Features Event Object Locking
- <The Design Philosophy of the DARPA Internet Protocols>读后感
- 简译The Design Philosophy of the DARPA Internet Protocols
- On the coexistence of transport protocols in data centers
- 6 ways of doing locking in .NET (Pessimistic and optimistic)
- Transformer入门教程(九)(TR0787)The locking of cube
- 245. Judge not from appearence. 人不可貌相,海水不可斗量
- 为Magento提速
- 最近计划
- 【项目分析】手机新闻爆料中心的构想
- 数据路查询视图
- C# Simulation of Intent Locking Protocols
- Android类参考---Fragment(六)
- C++反汇编笔记1
- POJ 2516 最小费用流
- abap 判断当前用户是否有某事物码权限
- page189
- php分页类代码 Digg Style Paginator Class
- ANR处理及Log查看
- JS校验身份证号码(15位和18位)