@@ -1,122 +1,122 | |||
|
1 | 1 | using Implab.Parallels; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Collections.Generic; |
|
4 | 4 | using System.Linq; |
|
5 | 5 | using System.Text; |
|
6 | 6 | using System.Threading; |
|
7 | 7 | using System.Threading.Tasks; |
|
8 | 8 | using System.Windows.Forms; |
|
9 | 9 | |
|
10 | 10 | namespace Implab.Diagnostics.Interactive |
|
11 | 11 | { |
|
12 | 12 | public class InteractiveListener: TextListenerBase |
|
13 | 13 | { |
|
14 | 14 | TraceForm m_form; |
|
15 | 15 | |
|
16 | 16 | SynchronizationContext m_syncGuiThread; |
|
17 | 17 | readonly Promise<object> m_guiStarted = new Promise<object>(); |
|
18 | 18 | |
|
19 | 19 | readonly IPromise m_guiFinished; |
|
20 | readonly IPromise m_workerFinished = new Promise<object>(); | |
|
20 | // readonly IPromise m_workerFinished = new Promise<object>(); | |
|
21 | 21 | |
|
22 | 22 | readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>(); |
|
23 | 23 | readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false); |
|
24 | 24 | |
|
25 | 25 | int m_queueLength; |
|
26 | 26 | bool m_exitPending; |
|
27 | 27 | |
|
28 | 28 | readonly object m_pauseLock = new object(); |
|
29 | 29 | bool m_paused; |
|
30 | 30 | readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true); |
|
31 | 31 | |
|
32 | 32 | public InteractiveListener(bool global) : base(global) { |
|
33 | 33 | m_guiFinished = AsyncPool.InvokeNewThread(GuiThread); |
|
34 | m_workerFinished = AsyncPool.InvokeNewThread(QueueThread); | |
|
34 | /*m_workerFinished = */AsyncPool.InvokeNewThread(QueueThread); | |
|
35 | 35 | |
|
36 | 36 | m_guiStarted.Join(); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | void GuiThread() { |
|
40 | 40 | m_form = new TraceForm(); // will create SynchronizationContext |
|
41 | 41 | |
|
42 | 42 | m_form.PauseEvents += (s,a) => Pause(); |
|
43 | 43 | m_form.ResumeEvents += (s, a) => Resume(); |
|
44 | 44 | |
|
45 | 45 | m_syncGuiThread = SynchronizationContext.Current; |
|
46 | 46 | m_guiStarted.Resolve(); |
|
47 | 47 | Application.Run(); |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | void QueueThread() { |
|
51 | 51 | while (!m_exitPending) { |
|
52 | 52 | if (m_paused) |
|
53 | 53 | m_pauseEvent.WaitOne(); |
|
54 | 54 | |
|
55 | 55 | TraceViewItem item; |
|
56 | 56 | if (m_queue.TryDequeue(out item)) { |
|
57 | 57 | Interlocked.Decrement(ref m_queueLength); |
|
58 | 58 | |
|
59 | 59 | m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null); |
|
60 | 60 | } else { |
|
61 | 61 | m_queueEvent.WaitOne(); |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | public void Pause() { |
|
67 | 67 | // for consistency we need to set this properties atomically |
|
68 | 68 | lock (m_pauseLock) { |
|
69 | 69 | m_pauseEvent.Reset(); |
|
70 | 70 | m_paused = true; |
|
71 | 71 | } |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | public void Resume() { |
|
75 | 75 | // for consistency we need to set this properties atomically |
|
76 | 76 | lock (m_pauseLock) { |
|
77 | 77 | m_paused = false; |
|
78 | 78 | m_pauseEvent.Set(); |
|
79 | 79 | } |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | void Enqueue(TraceViewItem item) { |
|
83 | 83 | m_queue.Enqueue(item); |
|
84 | 84 | if (Interlocked.Increment(ref m_queueLength) == 1) |
|
85 | 85 | m_queueEvent.Set(); |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | public void ShowForm() { |
|
89 | 89 | m_syncGuiThread.Post(x => m_form.Show(), null); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | public void HideForm() { |
|
93 | 93 | m_syncGuiThread.Post(x => m_form.Hide(), null); |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | void Terminate() { |
|
97 | 97 | m_exitPending = true; |
|
98 | 98 | Resume(); |
|
99 | 99 | m_syncGuiThread.Post(x => Application.ExitThread(), null); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | protected override void Dispose(bool disposing) { |
|
103 | 103 | if (disposing) { |
|
104 | 104 | Terminate(); |
|
105 | 105 | m_guiFinished.Join(); |
|
106 | 106 | } |
|
107 | 107 | base.Dispose(disposing); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | protected override void WriteEntry(TraceContext context, EventText text, string channel) { |
|
111 | 111 | var item = new TraceViewItem { |
|
112 | 112 | Indent = text.indent, |
|
113 | 113 | Message = text.content, |
|
114 | 114 | Thread = context.ThreadId, |
|
115 | 115 | Channel = channel, |
|
116 | 116 | Timestamp = Environment.TickCount |
|
117 | 117 | }; |
|
118 | 118 | |
|
119 | 119 | Enqueue(item); |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | } |
@@ -1,198 +1,217 | |||
|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
3 | 3 | <PropertyGroup> |
|
4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
5 | 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
6 | 6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
7 | 7 | <OutputType>Library</OutputType> |
|
8 | 8 | <RootNamespace>Implab</RootNamespace> |
|
9 | 9 | <AssemblyName>Implab</AssemblyName> |
|
10 | 10 | </PropertyGroup> |
|
11 | 11 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
12 | 12 | <DebugSymbols>true</DebugSymbols> |
|
13 | 13 | <DebugType>full</DebugType> |
|
14 | 14 | <Optimize>false</Optimize> |
|
15 | 15 | <OutputPath>bin\Debug</OutputPath> |
|
16 | 16 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
17 | 17 | <ErrorReport>prompt</ErrorReport> |
|
18 | 18 | <WarningLevel>4</WarningLevel> |
|
19 | 19 | <ConsolePause>false</ConsolePause> |
|
20 | 20 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
21 | 21 | </PropertyGroup> |
|
22 | 22 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
23 | 23 | <DebugType>full</DebugType> |
|
24 | 24 | <Optimize>true</Optimize> |
|
25 | 25 | <OutputPath>bin\Release</OutputPath> |
|
26 | 26 | <ErrorReport>prompt</ErrorReport> |
|
27 | 27 | <WarningLevel>4</WarningLevel> |
|
28 | 28 | <ConsolePause>false</ConsolePause> |
|
29 | 29 | </PropertyGroup> |
|
30 | 30 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> |
|
31 | 31 | <DebugSymbols>true</DebugSymbols> |
|
32 | 32 | <DebugType>full</DebugType> |
|
33 | 33 | <Optimize>false</Optimize> |
|
34 | 34 | <OutputPath>bin\Debug</OutputPath> |
|
35 | 35 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> |
|
36 | 36 | <ErrorReport>prompt</ErrorReport> |
|
37 | 37 | <WarningLevel>4</WarningLevel> |
|
38 | 38 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
39 | 39 | <ConsolePause>false</ConsolePause> |
|
40 | 40 | </PropertyGroup> |
|
41 | 41 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> |
|
42 | 42 | <Optimize>true</Optimize> |
|
43 | 43 | <OutputPath>bin\Release</OutputPath> |
|
44 | 44 | <ErrorReport>prompt</ErrorReport> |
|
45 | 45 | <WarningLevel>4</WarningLevel> |
|
46 | 46 | <ConsolePause>false</ConsolePause> |
|
47 | 47 | <DefineConstants>NET_4_5</DefineConstants> |
|
48 | 48 | </PropertyGroup> |
|
49 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> | |
|
50 | <DebugSymbols>true</DebugSymbols> | |
|
51 | <DebugType>full</DebugType> | |
|
52 | <Optimize>false</Optimize> | |
|
53 | <OutputPath>bin\Debug</OutputPath> | |
|
54 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> | |
|
55 | <ErrorReport>prompt</ErrorReport> | |
|
56 | <WarningLevel>4</WarningLevel> | |
|
57 | <RunCodeAnalysis>true</RunCodeAnalysis> | |
|
58 | <ConsolePause>false</ConsolePause> | |
|
59 | </PropertyGroup> | |
|
60 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> | |
|
61 | <Optimize>true</Optimize> | |
|
62 | <OutputPath>bin\Release</OutputPath> | |
|
63 | <DefineConstants>NET_4_5;MONO;</DefineConstants> | |
|
64 | <ErrorReport>prompt</ErrorReport> | |
|
65 | <WarningLevel>4</WarningLevel> | |
|
66 | <ConsolePause>false</ConsolePause> | |
|
67 | </PropertyGroup> | |
|
49 | 68 | <ItemGroup> |
|
50 | 69 | <Reference Include="System" /> |
|
51 | 70 | <Reference Include="System.Xml" /> |
|
52 | 71 | </ItemGroup> |
|
53 | 72 | <ItemGroup> |
|
54 | 73 | <Compile Include="Component.cs" /> |
|
55 | 74 | <Compile Include="CustomEqualityComparer.cs" /> |
|
56 | 75 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
57 | 76 | <Compile Include="Diagnostics\EventText.cs" /> |
|
58 | 77 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> |
|
59 | 78 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
60 | 79 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
61 | 80 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
62 | 81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
63 | 82 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
64 | 83 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
65 | 84 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
66 | 85 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
67 | 86 | <Compile Include="Disposable.cs" /> |
|
68 | 87 | <Compile Include="ICancellable.cs" /> |
|
69 | 88 | <Compile Include="IProgressHandler.cs" /> |
|
70 | 89 | <Compile Include="IProgressNotifier.cs" /> |
|
71 | 90 | <Compile Include="IPromiseT.cs" /> |
|
72 | 91 | <Compile Include="IPromise.cs" /> |
|
73 | 92 | <Compile Include="IServiceLocator.cs" /> |
|
74 | 93 | <Compile Include="ITaskController.cs" /> |
|
75 | 94 | <Compile Include="JSON\JSONElementContext.cs" /> |
|
76 | 95 | <Compile Include="JSON\JSONElementType.cs" /> |
|
77 | 96 | <Compile Include="JSON\JSONGrammar.cs" /> |
|
78 | 97 | <Compile Include="JSON\JSONParser.cs" /> |
|
79 | 98 | <Compile Include="JSON\JSONScanner.cs" /> |
|
80 | 99 | <Compile Include="JSON\JsonTokenType.cs" /> |
|
81 | 100 | <Compile Include="JSON\JSONWriter.cs" /> |
|
82 | 101 | <Compile Include="JSON\JSONXmlReader.cs" /> |
|
83 | 102 | <Compile Include="JSON\JSONXmlReaderOptions.cs" /> |
|
84 | 103 | <Compile Include="JSON\StringTranslator.cs" /> |
|
85 | 104 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
86 | 105 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
87 | 106 | <Compile Include="Parallels\MTQueue.cs" /> |
|
88 | 107 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
89 | 108 | <Compile Include="Parsing\Alphabet.cs" /> |
|
90 | 109 | <Compile Include="Parsing\AlphabetBase.cs" /> |
|
91 | 110 | <Compile Include="Parsing\AltToken.cs" /> |
|
92 | 111 | <Compile Include="Parsing\BinaryToken.cs" /> |
|
93 | 112 | <Compile Include="Parsing\CatToken.cs" /> |
|
94 | 113 | <Compile Include="Parsing\CDFADefinition.cs" /> |
|
95 | 114 | <Compile Include="Parsing\DFABuilder.cs" /> |
|
96 | 115 | <Compile Include="Parsing\DFADefinitionBase.cs" /> |
|
97 | 116 | <Compile Include="Parsing\DFAStateDescriptor.cs" /> |
|
98 | 117 | <Compile Include="Parsing\DFAutomaton.cs" /> |
|
99 | 118 | <Compile Include="Parsing\EDFADefinition.cs" /> |
|
100 | 119 | <Compile Include="Parsing\EmptyToken.cs" /> |
|
101 | 120 | <Compile Include="Parsing\EndToken.cs" /> |
|
102 | 121 | <Compile Include="Parsing\EnumAlphabet.cs" /> |
|
103 | 122 | <Compile Include="Parsing\Grammar.cs" /> |
|
104 | 123 | <Compile Include="Parsing\IAlphabet.cs" /> |
|
105 | 124 | <Compile Include="Parsing\IDFADefinition.cs" /> |
|
106 | 125 | <Compile Include="Parsing\IVisitor.cs" /> |
|
107 | 126 | <Compile Include="Parsing\ParserException.cs" /> |
|
108 | 127 | <Compile Include="Parsing\Scanner.cs" /> |
|
109 | 128 | <Compile Include="Parsing\StarToken.cs" /> |
|
110 | 129 | <Compile Include="Parsing\SymbolToken.cs" /> |
|
111 | 130 | <Compile Include="Parsing\Token.cs" /> |
|
112 | 131 | <Compile Include="SafePool.cs" /> |
|
113 | 132 | <Compile Include="ServiceLocator.cs" /> |
|
114 | 133 | <Compile Include="TaskController.cs" /> |
|
115 | 134 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
116 | 135 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
117 | 136 | <Compile Include="Promise.cs" /> |
|
118 | 137 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
119 | 138 | <Compile Include="Safe.cs" /> |
|
120 | 139 | <Compile Include="ValueEventArgs.cs" /> |
|
121 | 140 | <Compile Include="PromiseExtensions.cs" /> |
|
122 | 141 | <Compile Include="TransientPromiseException.cs" /> |
|
123 | 142 | <Compile Include="SyncContextPromise.cs" /> |
|
124 | 143 | <Compile Include="ObjectPool.cs" /> |
|
125 | 144 | <Compile Include="ObjectPoolWrapper.cs" /> |
|
126 | 145 | </ItemGroup> |
|
127 | 146 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
128 | 147 | <ItemGroup /> |
|
129 | 148 | <ProjectExtensions> |
|
130 | 149 | <MonoDevelop> |
|
131 | 150 | <Properties> |
|
132 | 151 | <Policies> |
|
133 | 152 | <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" /> |
|
134 | 153 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> |
|
135 | 154 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> |
|
136 | 155 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> |
|
137 | 156 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> |
|
138 | 157 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> |
|
139 | 158 | <NameConventionPolicy> |
|
140 | 159 | <Rules> |
|
141 | 160 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
142 | 161 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
143 | 162 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
144 | 163 | <RequiredPrefixes> |
|
145 | 164 | <String>I</String> |
|
146 | 165 | </RequiredPrefixes> |
|
147 | 166 | </NamingRule> |
|
148 | 167 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
149 | 168 | <RequiredSuffixes> |
|
150 | 169 | <String>Attribute</String> |
|
151 | 170 | </RequiredSuffixes> |
|
152 | 171 | </NamingRule> |
|
153 | 172 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
154 | 173 | <RequiredSuffixes> |
|
155 | 174 | <String>EventArgs</String> |
|
156 | 175 | </RequiredSuffixes> |
|
157 | 176 | </NamingRule> |
|
158 | 177 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
159 | 178 | <RequiredSuffixes> |
|
160 | 179 | <String>Exception</String> |
|
161 | 180 | </RequiredSuffixes> |
|
162 | 181 | </NamingRule> |
|
163 | 182 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
164 | 183 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> |
|
165 | 184 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
166 | 185 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> |
|
167 | 186 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
168 | 187 | <RequiredPrefixes> |
|
169 | 188 | <String>m_</String> |
|
170 | 189 | </RequiredPrefixes> |
|
171 | 190 | </NamingRule> |
|
172 | 191 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> |
|
173 | 192 | <RequiredPrefixes> |
|
174 | 193 | <String>_</String> |
|
175 | 194 | </RequiredPrefixes> |
|
176 | 195 | </NamingRule> |
|
177 | 196 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
178 | 197 | <RequiredPrefixes> |
|
179 | 198 | <String>m_</String> |
|
180 | 199 | </RequiredPrefixes> |
|
181 | 200 | </NamingRule> |
|
182 | 201 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
183 | 202 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
184 | 203 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
185 | 204 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
186 | 205 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
187 | 206 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
188 | 207 | <RequiredPrefixes> |
|
189 | 208 | <String>T</String> |
|
190 | 209 | </RequiredPrefixes> |
|
191 | 210 | </NamingRule> |
|
192 | 211 | </Rules> |
|
193 | 212 | </NameConventionPolicy> |
|
194 | 213 | </Policies> |
|
195 | 214 | </Properties> |
|
196 | 215 | </MonoDevelop> |
|
197 | 216 | </ProjectExtensions> |
|
198 | 217 | </Project> No newline at end of file |
@@ -1,340 +1,343 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | using Implab.Parsing; |
|
3 | 3 | using System; |
|
4 | 4 | using System.Collections.Generic; |
|
5 | 5 | using System.Globalization; |
|
6 | 6 | using System.IO; |
|
7 | 7 | using System.Linq; |
|
8 | 8 | using System.Text; |
|
9 | 9 | using System.Threading.Tasks; |
|
10 | 10 | using System.Xml; |
|
11 | 11 | |
|
12 | 12 | namespace Implab.JSON { |
|
13 | 13 | public class JSONXmlReader : XmlReader { |
|
14 | 14 | |
|
15 | 15 | enum ValueContext { |
|
16 | 16 | Undefined, |
|
17 | 17 | ElementStart, |
|
18 | 18 | ElementValue, |
|
19 | 19 | ElementEnd, |
|
20 | 20 | ElementEmpty |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | struct LocalNameContext { |
|
24 | 24 | public string localName; |
|
25 | 25 | public bool isArray; |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | JSONParser m_parser; |
|
29 | 29 | ValueContext m_valueContext; |
|
30 | 30 | ReadState m_state = ReadState.Initial; |
|
31 | 31 | Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>(); |
|
32 | 32 | LocalNameContext m_localName; |
|
33 | 33 | int m_depthCorrection = 0; |
|
34 | 34 | |
|
35 | 35 | readonly string m_rootName; |
|
36 | 36 | readonly string m_prefix; |
|
37 | 37 | readonly string m_namespaceUri; |
|
38 | 38 | readonly bool m_flattenArrays; |
|
39 | 39 | readonly string m_arrayItemName; |
|
40 | 40 | readonly XmlNameTable m_nameTable; |
|
41 | 41 | |
|
42 | 42 | JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) { |
|
43 | 43 | m_parser = parser; |
|
44 | 44 | |
|
45 | 45 | if (options != null) { |
|
46 | 46 | m_prefix = options.NodesPrefix ?? String.Empty; |
|
47 | 47 | m_namespaceUri = options.NamespaceURI ?? String.Empty; |
|
48 | 48 | m_rootName = options.RootName ?? "json"; |
|
49 | 49 | m_flattenArrays = options.FlattenArrays; |
|
50 | 50 | m_arrayItemName = options.ArrayItemName ?? "item"; |
|
51 | 51 | m_nameTable = options.NameTable ?? new NameTable(); |
|
52 | 52 | } else { |
|
53 | 53 | m_prefix = String.Empty; |
|
54 | 54 | m_namespaceUri = String.Empty; |
|
55 | 55 | m_rootName = "json"; |
|
56 | 56 | m_flattenArrays = false; |
|
57 | 57 | m_arrayItemName = "item"; |
|
58 | 58 | m_nameTable = new NameTable(); |
|
59 | 59 | } |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | /// <summary> |
|
63 | 63 | /// Always 0, JSON doesn't support attributes |
|
64 | 64 | /// </summary> |
|
65 | 65 | public override int AttributeCount { |
|
66 | 66 | get { return 0; } |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | public override string BaseURI { |
|
70 | 70 | get { return String.Empty; } |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | public override int Depth { |
|
74 | 74 | get { |
|
75 | 75 | return m_localNameStack.Count + m_depthCorrection; |
|
76 | 76 | } |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | public override bool EOF { |
|
80 | 80 | get { return m_parser.EOF; } |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | /// <summary> |
|
84 | 84 | /// Always throws an exception |
|
85 | 85 | /// </summary> |
|
86 | 86 | /// <param name="i"></param> |
|
87 | 87 | /// <returns></returns> |
|
88 | 88 | public override string GetAttribute(int i) { |
|
89 | 89 | throw new ArgumentOutOfRangeException(); |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | /// <summary> |
|
93 | 93 | /// Always returns empty string |
|
94 | 94 | /// </summary> |
|
95 | 95 | /// <param name="name"></param> |
|
96 | 96 | /// <param name="namespaceURI"></param> |
|
97 | 97 | /// <returns></returns> |
|
98 | 98 | public override string GetAttribute(string name, string namespaceURI) { |
|
99 | 99 | return String.Empty; |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | /// <summary> |
|
103 | 103 | /// Always returns empty string |
|
104 | 104 | /// </summary> |
|
105 | 105 | /// <param name="name"></param> |
|
106 | 106 | /// <returns></returns> |
|
107 | 107 | public override string GetAttribute(string name) { |
|
108 | 108 | return String.Empty; |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | public override bool IsEmptyElement { |
|
112 | 112 | get { return m_parser.ElementType == JSONElementType.Value && m_valueContext == ValueContext.ElementEmpty; } |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | public override string LocalName { |
|
116 | 116 | get { return m_localName.localName; } |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | public override string LookupNamespace(string prefix) { |
|
120 | 120 | if (String.IsNullOrEmpty(prefix) || prefix == m_prefix) |
|
121 | 121 | return m_namespaceUri; |
|
122 | 122 | else |
|
123 | 123 | return String.Empty; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | public override bool MoveToAttribute(string name, string ns) { |
|
127 | 127 | return false; |
|
128 | 128 | } |
|
129 | 129 | |
|
130 | 130 | public override bool MoveToAttribute(string name) { |
|
131 | 131 | return false; |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | public override bool MoveToElement() { |
|
135 | 135 | return false; |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | public override bool MoveToFirstAttribute() { |
|
139 | 139 | return false; |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | public override bool MoveToNextAttribute() { |
|
143 | 143 | return false; |
|
144 | 144 | } |
|
145 | 145 | |
|
146 | 146 | public override XmlNameTable NameTable { |
|
147 | 147 | get { return m_nameTable; } |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | public override string NamespaceURI { |
|
151 | 151 | get { return m_namespaceUri; } |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | public override XmlNodeType NodeType { |
|
155 | 155 | get { |
|
156 | 156 | switch (m_parser.ElementType) { |
|
157 | 157 | case JSONElementType.BeginObject: |
|
158 | 158 | case JSONElementType.BeginArray: |
|
159 | 159 | return XmlNodeType.Element; |
|
160 | 160 | case JSONElementType.EndObject: |
|
161 | 161 | case JSONElementType.EndArray: |
|
162 | 162 | return XmlNodeType.EndElement; |
|
163 | 163 | case JSONElementType.Value: |
|
164 | 164 | switch (m_valueContext) { |
|
165 | 165 | case ValueContext.ElementStart: |
|
166 | 166 | case ValueContext.ElementEmpty: |
|
167 | 167 | return XmlNodeType.Element; |
|
168 | 168 | case ValueContext.ElementValue: |
|
169 | 169 | return XmlNodeType.Text; |
|
170 | 170 | case ValueContext.ElementEnd: |
|
171 | 171 | return XmlNodeType.EndElement; |
|
172 | 172 | default: |
|
173 | 173 | throw new InvalidOperationException(); |
|
174 | 174 | } |
|
175 | 175 | default: |
|
176 | 176 | throw new InvalidOperationException(); |
|
177 | 177 | } |
|
178 | 178 | } |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | public override string Prefix { |
|
182 | 182 | get { return m_prefix; } |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | public override bool Read() { |
|
186 | 186 | if (m_state != System.Xml.ReadState.Interactive && m_state != System.Xml.ReadState.Initial) |
|
187 | 187 | return false; |
|
188 | 188 | |
|
189 | 189 | if (m_state == ReadState.Initial) |
|
190 | 190 | m_state = System.Xml.ReadState.Interactive; |
|
191 | 191 | |
|
192 | 192 | try { |
|
193 | 193 | switch (m_parser.ElementType) { |
|
194 | 194 | case JSONElementType.Value: |
|
195 | 195 | switch (m_valueContext) { |
|
196 | 196 | case ValueContext.ElementStart: |
|
197 | 197 | SetLocalName(String.Empty); |
|
198 | 198 | m_valueContext = ValueContext.ElementValue; |
|
199 | 199 | return true; |
|
200 | 200 | case ValueContext.ElementValue: |
|
201 | 201 | RestoreLocalName(); |
|
202 | 202 | m_valueContext = ValueContext.ElementEnd; |
|
203 | 203 | return true; |
|
204 | 204 | case ValueContext.ElementEmpty: |
|
205 | 205 | case ValueContext.ElementEnd: |
|
206 | 206 | RestoreLocalName(); |
|
207 | 207 | break; |
|
208 | 208 | } |
|
209 | 209 | break; |
|
210 | 210 | case JSONElementType.EndArray: |
|
211 | 211 | case JSONElementType.EndObject: |
|
212 | 212 | RestoreLocalName(); |
|
213 | 213 | break; |
|
214 | 214 | } |
|
215 | 215 | string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName; |
|
216 | 216 | while (m_parser.Read()) { |
|
217 | 217 | if (!String.IsNullOrEmpty(m_parser.ElementName)) |
|
218 | 218 | itemName = m_parser.ElementName; |
|
219 | 219 | |
|
220 | 220 | switch (m_parser.ElementType) { |
|
221 | 221 | case JSONElementType.BeginArray: |
|
222 | 222 | if (m_flattenArrays && !m_localName.isArray) { |
|
223 | 223 | m_depthCorrection--; |
|
224 | 224 | SetLocalName(itemName, true); |
|
225 | 225 | continue; |
|
226 | 226 | } else { |
|
227 | 227 | SetLocalName(itemName, true); |
|
228 | 228 | } |
|
229 | 229 | break; |
|
230 | 230 | case JSONElementType.BeginObject: |
|
231 | 231 | SetLocalName(itemName); |
|
232 | 232 | break; |
|
233 | 233 | case JSONElementType.EndArray: |
|
234 | 234 | if (m_flattenArrays && !m_localNameStack.Peek().isArray) { |
|
235 | 235 | RestoreLocalName(); |
|
236 | 236 | m_depthCorrection++; |
|
237 | 237 | continue; |
|
238 | 238 | } |
|
239 | 239 | break; |
|
240 | 240 | case JSONElementType.EndObject: |
|
241 | 241 | break; |
|
242 | 242 | case JSONElementType.Value: |
|
243 | 243 | SetLocalName(itemName); |
|
244 | 244 | m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart; |
|
245 | 245 | break; |
|
246 | 246 | default: |
|
247 | 247 | break; |
|
248 | 248 | } |
|
249 | 249 | return true; |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | m_state = System.Xml.ReadState.EndOfFile; |
|
253 | 253 | return false; |
|
254 | 254 | } catch { |
|
255 | 255 | m_state = System.Xml.ReadState.Error; |
|
256 | 256 | throw; |
|
257 | 257 | } |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | public override bool ReadAttributeValue() { |
|
261 | 261 | return false; |
|
262 | 262 | } |
|
263 | 263 | |
|
264 | 264 | public override ReadState ReadState { |
|
265 | 265 | get { return m_state; } |
|
266 | 266 | } |
|
267 | 267 | |
|
268 | 268 | public override void ResolveEntity() { |
|
269 | 269 | // do nothing |
|
270 | 270 | } |
|
271 | 271 | |
|
272 | 272 | public override string Value { |
|
273 | 273 | get { |
|
274 | 274 | if (m_parser.ElementValue == null) |
|
275 | 275 | return String.Empty; |
|
276 | 276 | if (Convert.GetTypeCode(m_parser.ElementValue) == TypeCode.Double) |
|
277 | 277 | return ((double)m_parser.ElementValue).ToString(CultureInfo.InvariantCulture); |
|
278 | 278 | else |
|
279 | 279 | return m_parser.ElementValue.ToString(); |
|
280 | 280 | } |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | void SetLocalName(string name) { |
|
284 | 284 | m_localNameStack.Push(m_localName); |
|
285 | 285 | m_localName.localName = name; |
|
286 | 286 | m_localName.isArray = false; |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | void SetLocalName(string name, bool isArray) { |
|
290 | 290 | m_localNameStack.Push(m_localName); |
|
291 | 291 | m_localName.localName = name; |
|
292 | 292 | m_localName.isArray = isArray; |
|
293 | 293 | } |
|
294 | 294 | |
|
295 | 295 | void RestoreLocalName() { |
|
296 | 296 | m_localName = m_localNameStack.Pop(); |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | public override void Close() { |
|
300 | 300 | |
|
301 | 301 | } |
|
302 | 302 | |
|
303 | 303 | protected override void Dispose(bool disposing) { |
|
304 | #if MONO | |
|
305 | disposing = true; | |
|
306 | #endif | |
|
304 | 307 | if (disposing) { |
|
305 | 308 | m_parser.Dispose(); |
|
306 | 309 | } |
|
307 | 310 | base.Dispose(disposing); |
|
308 | 311 | } |
|
309 | 312 | |
|
310 | 313 | public static JSONXmlReader Create(string file, JSONXmlReaderOptions options) { |
|
311 | 314 | return Create(File.OpenText(file), options); |
|
312 | 315 | } |
|
313 | 316 | |
|
314 | 317 | /// <summary> |
|
315 | 318 | /// Creates the XmlReader for the specified text stream with JSON data. |
|
316 | 319 | /// </summary> |
|
317 | 320 | /// <param name="reader">Text reader.</param> |
|
318 | 321 | /// <param name="options">Options.</param> |
|
319 | 322 | /// <remarks> |
|
320 | 323 | /// The reader will be disposed when the XmlReader is disposed. |
|
321 | 324 | /// </remarks> |
|
322 | 325 | public static JSONXmlReader Create(TextReader reader, JSONXmlReaderOptions options) { |
|
323 | 326 | return new JSONXmlReader(new JSONParser(reader, true), options); |
|
324 | 327 | } |
|
325 | 328 | |
|
326 | 329 | /// <summary> |
|
327 | 330 | /// Creates the XmlReader for the specified stream with JSON data. |
|
328 | 331 | /// </summary> |
|
329 | 332 | /// <param name="stream">Stream.</param> |
|
330 | 333 | /// <param name="options">Options.</param> |
|
331 | 334 | /// <remarks> |
|
332 | 335 | /// The stream will be disposed when the XmlReader is disposed. |
|
333 | 336 | /// </remarks> |
|
334 | 337 | public static JSONXmlReader Create(Stream stream, JSONXmlReaderOptions options) { |
|
335 | 338 | Safe.ArgumentNotNull(stream, "stream"); |
|
336 | 339 | // HACK don't dispose StreaReader to keep stream opened |
|
337 | 340 | return Create(new StreamReader(stream), options); |
|
338 | 341 | } |
|
339 | 342 | } |
|
340 | 343 | } |
@@ -1,92 +1,95 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Implab.Parallels; |
|
3 | 3 | using System.Threading; |
|
4 | using System.Diagnostics; | |
|
5 | using System.Diagnostics.CodeAnalysis; | |
|
4 | 6 | |
|
5 | 7 | namespace Implab { |
|
6 | public class ObjectPool<T> : IDisposable { | |
|
7 | readonly Func<T> m_factory; | |
|
8 | readonly Action<T> m_cleanup; | |
|
8 | public abstract class ObjectPool<T> : IDisposable { | |
|
9 | 9 | readonly int m_size; |
|
10 | 10 | readonly MTQueue<T> m_queue = new MTQueue<T>(); |
|
11 | 11 | |
|
12 | [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")] | |
|
13 | static readonly bool _isValueType = typeof(T).IsValueType; | |
|
14 | ||
|
12 | 15 | bool m_disposed; |
|
13 | 16 | |
|
14 | 17 | int m_count; |
|
15 | 18 | |
|
16 | public ObjectPool(Func<T> factory, Action<T> cleanup, int size) { | |
|
17 | Safe.ArgumentNotNull(factory, "factory"); | |
|
18 | Safe.ArgumentInRange(size, 1, size, "size"); | |
|
19 | ||
|
20 | m_factory = factory; | |
|
21 | m_cleanup = cleanup; | |
|
19 | protected ObjectPool(int size) { | |
|
22 | 20 | m_size = size; |
|
23 | 21 | } |
|
24 | 22 | |
|
25 |
p |
|
|
26 | } | |
|
27 | ||
|
28 | public ObjectPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) { | |
|
23 | protected ObjectPool() : this(Environment.ProcessorCount+1) { | |
|
29 | 24 | } |
|
30 | 25 | |
|
31 | 26 | public ObjectPoolWrapper<T> AllocateAuto() { |
|
32 | 27 | |
|
33 | 28 | return new ObjectPoolWrapper<T>(Allocate(), this); |
|
34 | 29 | } |
|
35 | 30 | |
|
36 | 31 | public T Allocate() { |
|
37 | 32 | if (m_disposed) |
|
38 |
throw new ObjectDisposedException( |
|
|
33 | throw new ObjectDisposedException(ToString()); | |
|
39 | 34 | |
|
40 | 35 | T instance; |
|
41 | 36 | if (m_queue.TryDequeue(out instance)) { |
|
42 | 37 | Interlocked.Decrement(ref m_count); |
|
43 | 38 | } else { |
|
44 |
instance = |
|
|
39 | instance = CreateInstance(); | |
|
40 | Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType); | |
|
45 | 41 | } |
|
46 | 42 | return instance; |
|
47 | 43 | } |
|
48 | 44 | |
|
45 | protected abstract T CreateInstance(); | |
|
46 | ||
|
47 | protected virtual void CleanupInstance(T instance) { | |
|
48 | } | |
|
49 | ||
|
49 | 50 | public void Release(T instance) { |
|
51 | if ( Object.Equals(instance,default(T)) && !_isValueType) | |
|
52 | return; | |
|
53 | ||
|
50 | 54 | Thread.MemoryBarrier(); |
|
51 | 55 | if (m_count < m_size && !m_disposed) { |
|
52 | 56 | Interlocked.Increment(ref m_count); |
|
53 | 57 | |
|
54 |
|
|
|
55 | m_cleanup(instance); | |
|
58 | CleanupInstance(instance); | |
|
56 | 59 | |
|
57 | 60 | m_queue.Enqueue(instance); |
|
58 | 61 | |
|
59 | 62 | // пока элемент возвращался в кеш, была начата операция освобождения всего кеша |
|
60 | 63 | // и возможно уже законцена, в таком случае следует извлечь элемент обратно и |
|
61 | 64 | // освободить его. Если операция освобождения кеша еще не заврешилась, то будет |
|
62 | 65 | // изъят и освобожден произвольный элемен, что не повлияет на ход всего процесса. |
|
63 | 66 | if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable) |
|
64 | 67 | ((IDisposable)instance).Dispose() ; |
|
65 | 68 | |
|
66 | 69 | } else { |
|
67 | 70 | if (instance is IDisposable) |
|
68 | 71 | ((IDisposable)instance).Dispose(); |
|
69 | 72 | } |
|
70 | 73 | } |
|
71 | 74 | |
|
72 | 75 | protected virtual void Dispose(bool disposing) { |
|
73 | 76 | if (disposing) { |
|
74 | 77 | m_disposed = true; |
|
75 | 78 | T instance; |
|
76 | 79 | while (m_queue.TryDequeue(out instance)) |
|
77 | 80 | if (instance is IDisposable) |
|
78 | 81 | ((IDisposable)instance).Dispose(); |
|
79 | 82 | } |
|
80 | 83 | } |
|
81 | 84 | |
|
82 | 85 | #region IDisposable implementation |
|
83 | 86 | |
|
84 | 87 | public void Dispose() { |
|
85 | 88 | Dispose(true); |
|
86 | 89 | GC.SuppressFinalize(this); |
|
87 | 90 | } |
|
88 | 91 | |
|
89 | 92 | #endregion |
|
90 | 93 | } |
|
91 | 94 | } |
|
92 | 95 |
General Comments 0
You need to be logged in to leave comments.
Login now