Merge lp:~mandel/ubuntuone-windows-installer/implement_db_port_manager into lp:ubuntuone-windows-installer/beta

Proposed by Manuel de la Peña
Status: Merged
Approved by: Vincenzo Di Somma
Approved revision: 49
Merged at revision: 66
Proposed branch: lp:~mandel/ubuntuone-windows-installer/implement_db_port_manager
Merge into: lp:ubuntuone-windows-installer/beta
Prerequisite: lp:~mandel/ubuntuone-windows-installer/implement_dotnet_ipc_events
Diff against target: 469 lines (+303/-4)
8 files modified
.bzrignore (+1/-0)
src/Canonical.UbuntuOne.ProcessDispatcher.Tests/App.config (+50/-0)
src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Canonical.UbuntuOne.ProcessDispatcher.Tests.csproj (+20/-0)
src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Net/PortManagerFixture.cs (+15/-0)
src/Canonical.UbuntuOne.ProcessDispatcher.Tests/SetUpClass.cs (+43/-0)
src/Canonical.UbuntuOne.ProcessDispatcher/App.config (+9/-2)
src/Canonical.UbuntuOne.ProcessDispatcher/Canonical.UbuntuOne.ProcessDispatcher.csproj (+8/-0)
src/Canonical.UbuntuOne.ProcessDispatcher/Net/PortManager.cs (+157/-2)
To merge this branch: bzr merge lp:~mandel/ubuntuone-windows-installer/implement_db_port_manager
Reviewer Review Type Date Requested Status
Vincenzo Di Somma (community) Approve
Rick McBride (community) Approve
Review via email: mp+32972@code.launchpad.net

Description of the change

Provides an implementation of the port manager that stores the relation between a user and a port in a SQLite db. This allows to ensure that the same port is always used when assigned so that the system admin can allow exceptions in the Windows firewall (either the OS one or a 3 party one)

To post a comment you must log in.
Revision history for this message
Rick McBride (rmcbride) wrote :

Looks good, tests pass. +1

review: Approve
Revision history for this message
Vincenzo Di Somma (vds) wrote :

Same for me!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '.bzrignore'
--- .bzrignore 2010-08-10 12:42:54 +0000
+++ .bzrignore 2010-08-18 10:05:56 +0000
@@ -27,3 +27,4 @@
27src/UbuntuOneClient.Tests/bin27src/UbuntuOneClient.Tests/bin
28src/UbuntuOneClient.Tests/obj28src/UbuntuOneClient.Tests/obj
29*.ReSharper29*.ReSharper
30test-results
3031
=== added directory 'lib/SQLite'
=== added file 'lib/SQLite/System.Data.SQLite.DLL'
31Binary files lib/SQLite/System.Data.SQLite.DLL 1970-01-01 00:00:00 +0000 and lib/SQLite/System.Data.SQLite.DLL 2010-08-18 10:05:56 +0000 differ32Binary files lib/SQLite/System.Data.SQLite.DLL 1970-01-01 00:00:00 +0000 and lib/SQLite/System.Data.SQLite.DLL 2010-08-18 10:05:56 +0000 differ
=== added file 'src/Canonical.UbuntuOne.ProcessDispatcher.Tests/App.config'
--- src/Canonical.UbuntuOne.ProcessDispatcher.Tests/App.config 1970-01-01 00:00:00 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher.Tests/App.config 2010-08-18 10:05:56 +0000
@@ -0,0 +1,50 @@
1<?xml version="1.0"?>
2<configuration>
3 <configSections>
4 <sectionGroup name="spring">
5 <section name="parsers"
6 type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
7 <section name="context"
8 type="Spring.Context.Support.ContextHandler, Spring.Core"/>
9 <section name="objects"
10 type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
11 </sectionGroup>
12 <sectionGroup name="common">
13 <section name="logging"
14 type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
15 </sectionGroup>
16 </configSections>
17
18 <!-- logging configuration -->
19 <common>
20 <logging>
21 <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
22 <!-- choices are INLINE, FILE, FILE-WATCH, EXTERNAL-->
23 <!-- otherwise BasicConfigurer.Configure is used -->
24 <!-- log4net configuration file is specified with key configFile-->
25 <arg key="configType"
26 value="FILE" />
27 <arg key="configFile"
28 value="~/Config/log4net.config" />
29 </factoryAdapter>
30 </logging>
31 </common>
32
33 <!-- Spring configuration -->
34 <spring>
35 <parsers>
36 <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
37 </parsers>
38 <context>
39 <resource uri="config://spring/objects"/>
40 </context>
41 <objects xmlns="http://www.springframework.net"
42 xmlns:db="http://www.springframework.net/database">
43 <!-- most internal objects definition is in the embeded objects.xml file here you will just find the db configuration -->
44 <db:provider id="DbProvider"
45 provider="SQLite-1.0.65"
46 connectionString="Data Source=UbuntuOne.db;Version=3;FailIfMissing=False;Compress=True"/>
47 </objects>
48 </spring>
49 <startup/>
50</configuration>
051
=== modified file 'src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Canonical.UbuntuOne.ProcessDispatcher.Tests.csproj'
--- src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Canonical.UbuntuOne.ProcessDispatcher.Tests.csproj 2010-08-18 10:05:56 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Canonical.UbuntuOne.ProcessDispatcher.Tests.csproj 2010-08-18 10:05:56 +0000
@@ -65,6 +65,7 @@
65 <Compile Include="Protobuf\SyncDaemonMessageFactoryFixture.cs" />65 <Compile Include="Protobuf\SyncDaemonMessageFactoryFixture.cs" />
66 <Compile Include="Protobuf\SyncFolderMessageFactoryFixture.cs" />66 <Compile Include="Protobuf\SyncFolderMessageFactoryFixture.cs" />
67 <Compile Include="Protobuf\SyncShareMessageFactoryFixture.cs" />67 <Compile Include="Protobuf\SyncShareMessageFactoryFixture.cs" />
68 <Compile Include="SetUpClass.cs" />
68 <Compile Include="SyncConfigurationServiceFixture.cs" />69 <Compile Include="SyncConfigurationServiceFixture.cs" />
69 <Compile Include="SyncDaemonServiceFixture.cs" />70 <Compile Include="SyncDaemonServiceFixture.cs" />
70 <Compile Include="SyncFoldersServiceFixture.cs" />71 <Compile Include="SyncFoldersServiceFixture.cs" />
@@ -81,6 +82,10 @@
81 </ProjectReference>82 </ProjectReference>
82 </ItemGroup>83 </ItemGroup>
83 <ItemGroup>84 <ItemGroup>
85 <Reference Include="Common.Logging.Log4Net, Version=1.2.0.2, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
86 <SpecificVersion>False</SpecificVersion>
87 <HintPath>..\..\lib\Spring.Net\Common.Logging.Log4Net.dll</HintPath>
88 </Reference>
84 <Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">89 <Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
85 <SpecificVersion>False</SpecificVersion>90 <SpecificVersion>False</SpecificVersion>
86 <HintPath>..\..\lib\log4net\log4net.dll</HintPath>91 <HintPath>..\..\lib\log4net\log4net.dll</HintPath>
@@ -97,11 +102,23 @@
97 <SpecificVersion>False</SpecificVersion>102 <SpecificVersion>False</SpecificVersion>
98 <HintPath>..\..\lib\RhinoMocks\Rhino.Mocks.dll</HintPath>103 <HintPath>..\..\lib\RhinoMocks\Rhino.Mocks.dll</HintPath>
99 </Reference>104 </Reference>
105 <Reference Include="Spring.Core, Version=1.3.0.20349, Culture=neutral, PublicKeyToken=65e474d141e25e07, processorArchitecture=MSIL">
106 <SpecificVersion>False</SpecificVersion>
107 <HintPath>..\..\lib\Spring.Net\Spring.Core.dll</HintPath>
108 </Reference>
109 <Reference Include="Spring.Data, Version=1.3.0.20349, Culture=neutral, PublicKeyToken=65e474d141e25e07, processorArchitecture=MSIL">
110 <SpecificVersion>False</SpecificVersion>
111 <HintPath>..\..\lib\Spring.Net\Spring.Data.dll</HintPath>
112 </Reference>
100 <Reference Include="System" />113 <Reference Include="System" />
101 <Reference Include="System.Core">114 <Reference Include="System.Core">
102 <RequiredTargetFramework>3.5</RequiredTargetFramework>115 <RequiredTargetFramework>3.5</RequiredTargetFramework>
103 </Reference>116 </Reference>
104 <Reference Include="System.Data" />117 <Reference Include="System.Data" />
118 <Reference Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
119 <SpecificVersion>False</SpecificVersion>
120 <HintPath>..\..\lib\SQLite\System.Data.SQLite.DLL</HintPath>
121 </Reference>
105 <Reference Include="System.Xml" />122 <Reference Include="System.Xml" />
106 </ItemGroup>123 </ItemGroup>
107 <ItemGroup>124 <ItemGroup>
@@ -121,6 +138,9 @@
121 <Install>false</Install>138 <Install>false</Install>
122 </BootstrapperPackage>139 </BootstrapperPackage>
123 </ItemGroup>140 </ItemGroup>
141 <ItemGroup>
142 <None Include="App.config" />
143 </ItemGroup>
124 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />144 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
125 <ProjectExtensions>145 <ProjectExtensions>
126 <MonoDevelop>146 <MonoDevelop>
127147
=== modified file 'src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Net/PortManagerFixture.cs'
--- src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Net/PortManagerFixture.cs 2010-08-12 07:59:15 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher.Tests/Net/PortManagerFixture.cs 2010-08-18 10:05:56 +0000
@@ -18,9 +18,12 @@
18 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>18 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>
19 */19 */
20using Canonical.UbuntuOne.Common;20using Canonical.UbuntuOne.Common;
21using Canonical.UbuntuOne.Common.Container;
21using Canonical.UbuntuOne.ProcessDispatcher.Net;22using Canonical.UbuntuOne.ProcessDispatcher.Net;
22using NUnit.Framework;23using NUnit.Framework;
23using Rhino.Mocks;24using Rhino.Mocks;
25using Spring.Data.Common;
26using Spring.Data.Core;
2427
25namespace Canonical.UbuntuOne.ProcessDispatcher.Tests.Net28namespace Canonical.UbuntuOne.ProcessDispatcher.Tests.Net
26{29{
@@ -33,6 +36,7 @@
33 private IAuthentication _auth;36 private IAuthentication _auth;
34 private PortManager _portManager;37 private PortManager _portManager;
35 private MockRepository _mocks;38 private MockRepository _mocks;
39 private IDbProvider _dbProvider;
3640
37 #endregion41 #endregion
3842
@@ -44,15 +48,26 @@
44 _mocks = new MockRepository();48 _mocks = new MockRepository();
45 _portFinder = _mocks.StrictMock<IPortFinder>();49 _portFinder = _mocks.StrictMock<IPortFinder>();
46 _auth = _mocks.StrictMock<IAuthentication>();50 _auth = _mocks.StrictMock<IAuthentication>();
51 _dbProvider = ObjectsContainer.GetImplementationOf<IDbProvider>();
47 _portManager = PortManager.Instance;52 _portManager = PortManager.Instance;
48 _portManager.Authentication = _auth;53 _portManager.Authentication = _auth;
49 _portManager.PortFinder = _portFinder;54 _portManager.PortFinder = _portFinder;
55 _portManager.DbProvider = _dbProvider;
50 }56 }
5157
52 [TearDown]58 [TearDown]
53 public void TearDown()59 public void TearDown()
54 {60 {
55 _portManager.Clear();61 _portManager.Clear();
62 // Clear the db
63 var template = new AdoTemplate(_dbProvider);
64 template.Execute(
65 command =>
66 {
67 command.CommandText = string.Format("DELETE FROM {0};", PortManager.UserPortsTableName);
68 return command.ExecuteScalar();
69 }
70 );
56 }71 }
5772
58 #endregion73 #endregion
5974
=== added file 'src/Canonical.UbuntuOne.ProcessDispatcher.Tests/SetUpClass.cs'
--- src/Canonical.UbuntuOne.ProcessDispatcher.Tests/SetUpClass.cs 1970-01-01 00:00:00 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher.Tests/SetUpClass.cs 2010-08-18 10:05:56 +0000
@@ -0,0 +1,43 @@
1/*
2 * Copyright 2010 Canonical Ltd.
3 *
4 * This file is part of UbuntuOne on Windows.
5 *
6 * UbuntuOne on Windows is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version
8 * as published by the Free Software Foundation.
9 *
10 * Ubuntu One on Windows is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with UbuntuOne for Windows. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>
19 */
20using Canonical.UbuntuOne.Common.Container;
21using NUnit.Framework;
22
23namespace Canonical.UbuntuOne.ProcessDispatcher.Tests
24{
25 /// <summary>
26 /// Set up class ran before any test to be able to use the IoC from xml
27 /// </summary>
28 [SetUpFixture]
29 public class SetUpClass
30 {
31 [SetUp]
32 public void Setup()
33 {
34 ObjectsContainer.Initialize(new SpringContainer());
35 }
36
37 [TearDown]
38 public void TearDown()
39 {
40 ObjectsContainer.Clear();
41 }
42 }
43}
044
=== modified file 'src/Canonical.UbuntuOne.ProcessDispatcher/App.config'
--- src/Canonical.UbuntuOne.ProcessDispatcher/App.config 2010-07-26 15:55:00 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher/App.config 2010-08-18 10:05:56 +0000
@@ -32,11 +32,18 @@
32 32
33 <!-- Spring configuration -->33 <!-- Spring configuration -->
34 <spring>34 <spring>
35 <parsers>
36 <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
37 </parsers>
35 <context>38 <context>
36 <resource uri="assembly://Canonical.UbuntuOne.ProcessDispatcher/Canonical.UbuntuOne.ProcessDispatcher/objects.xml" />39 <resource uri="assembly://Canonical.UbuntuOne.ProcessDispatcher/Canonical.UbuntuOne.ProcessDispatcher/objects.xml" />
37 </context>40 </context>
38 <objects xmlns="http://www.springframework.net">41 <objects xmlns="http://www.springframework.net"
39 <!-- most internal objects definition is in the embeded objects.xml file -->42 xmlns:db="http://www.springframework.net/database">
43 <!-- most internal objects definition is in the embeded objects.xml file here you will just find the db configuration -->
44 <db:provider id="DbProvider"
45 provider="SQLite-1.0.65"
46 connectionString="Data Source=UbuntuOne.db;Version=3;FailIfMissing=False;Compress=True"/>
40 </objects>47 </objects>
41 </spring>48 </spring>
4249
4350
=== modified file 'src/Canonical.UbuntuOne.ProcessDispatcher/Canonical.UbuntuOne.ProcessDispatcher.csproj'
--- src/Canonical.UbuntuOne.ProcessDispatcher/Canonical.UbuntuOne.ProcessDispatcher.csproj 2010-08-18 10:05:56 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher/Canonical.UbuntuOne.ProcessDispatcher.csproj 2010-08-18 10:05:56 +0000
@@ -127,6 +127,14 @@
127 <SpecificVersion>False</SpecificVersion>127 <SpecificVersion>False</SpecificVersion>
128 <HintPath>..\..\lib\log4net\log4net.dll</HintPath>128 <HintPath>..\..\lib\log4net\log4net.dll</HintPath>
129 </Reference>129 </Reference>
130 <Reference Include="Spring.Core, Version=1.3.0.20349, Culture=neutral, PublicKeyToken=65e474d141e25e07, processorArchitecture=MSIL">
131 <SpecificVersion>False</SpecificVersion>
132 <HintPath>..\..\lib\Spring.Net\Spring.Core.dll</HintPath>
133 </Reference>
134 <Reference Include="Spring.Data, Version=1.3.0.20349, Culture=neutral, PublicKeyToken=65e474d141e25e07, processorArchitecture=MSIL">
135 <SpecificVersion>False</SpecificVersion>
136 <HintPath>..\..\lib\Spring.Net\Spring.Data.dll</HintPath>
137 </Reference>
130 <Reference Include="Spring.Services, Version=1.3.0.20214, Culture=neutral, PublicKeyToken=65e474d141e25e07, processorArchitecture=MSIL">138 <Reference Include="Spring.Services, Version=1.3.0.20214, Culture=neutral, PublicKeyToken=65e474d141e25e07, processorArchitecture=MSIL">
131 <SpecificVersion>False</SpecificVersion>139 <SpecificVersion>False</SpecificVersion>
132 <HintPath>..\..\lib\Spring.Net\Spring.Services.dll</HintPath>140 <HintPath>..\..\lib\Spring.Net\Spring.Services.dll</HintPath>
133141
=== modified file 'src/Canonical.UbuntuOne.ProcessDispatcher/Net/PortManager.cs'
--- src/Canonical.UbuntuOne.ProcessDispatcher/Net/PortManager.cs 2010-08-12 07:59:15 +0000
+++ src/Canonical.UbuntuOne.ProcessDispatcher/Net/PortManager.cs 2010-08-18 10:05:56 +0000
@@ -17,14 +17,19 @@
17 * 17 *
18 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>18 * Authors: Manuel de la Peña <manuel.delapena@canonical.com>
19 */19 */
20using System;
20using System.Collections.Generic;21using System.Collections.Generic;
22using System.Data;
21using Canonical.UbuntuOne.Common;23using Canonical.UbuntuOne.Common;
24using Spring.Data.Common;
25using Spring.Data.Core;
2226
23namespace Canonical.UbuntuOne.ProcessDispatcher.Net27namespace Canonical.UbuntuOne.ProcessDispatcher.Net
24{28{
25 /// <summary>29 /// <summary>
26 /// Implementation of the IPortManager that keeps track of the ports assigned to the different users and ensures that30 /// Implementation of the IPortManager that keeps track of the ports assigned to the different users and ensures that
27 /// there are correctly managed.31 /// there are correctly managed. The port manager create a sqlite db that will be used to ensure that a user
32 /// allw
28 /// </summary>33 /// </summary>
29 internal class PortManager : IPortManager34 internal class PortManager : IPortManager
30 {35 {
@@ -67,6 +72,8 @@
6772
68 private readonly object _dictionaryLock = new object();73 private readonly object _dictionaryLock = new object();
69 private readonly Dictionary<string, UserPorts> _ports;74 private readonly Dictionary<string, UserPorts> _ports;
75 internal const string UserPortsTableName = "UserPorts";
76 private bool _usetTableExists;
7077
71 #endregion78 #endregion
7279
@@ -82,6 +89,12 @@
82 /// </summary>89 /// </summary>
83 internal IPortFinder PortFinder { get; set; }90 internal IPortFinder PortFinder { get; set; }
8491
92 /// <summary>
93 /// Gets and sets the provider that the port manager uses to store the relation between a user and the
94 /// ports he use in the application.
95 /// </summary>
96 internal IDbProvider DbProvider { get; set; }
97
85 #endregion98 #endregion
8699
87 #region Helpers100 #region Helpers
@@ -93,11 +106,153 @@
93 {106 {
94 lock (_dictionaryLock)107 lock (_dictionaryLock)
95 {108 {
96 var userPorts = new UserPorts(PortFinder.GetFreePort(), PortFinder.GetFreePort());109 // we check if the table exists, this can happe either the first time we start in the system
110 // and the table does not exists or when we reboot and we lost the info of the flag. Ofcourse the
111 // the table could have been drop at some point
112 if(!_usetTableExists)
113 {
114 var reallyExists = UserPortsTableExists();
115 if(!reallyExists)
116 {
117 CreateUserPortsTable();
118 }
119 _usetTableExists = true;
120 }
121 int dotNetPort;
122 int pythonPort;
123 // we try to get the data from the db
124 if(!TryGetUserPortFromDb(Authentication.Username, out pythonPort, out dotNetPort))
125 {
126 // we cannot get the ports, therefore we generate them.
127 pythonPort = PortFinder.GetFreePort();
128 dotNetPort = PortFinder.GetFreePort();
129 // store the user ports in the db for the next time
130 InserUserPortsInDb(Authentication.Username, pythonPort, dotNetPort);
131 }
132 var userPorts = new UserPorts(pythonPort, dotNetPort);
97 _ports.Add(Authentication.Username, userPorts);133 _ports.Add(Authentication.Username, userPorts);
98 }134 }
99 }135 }
100136
137
138 /// <summary>
139 /// Helper method that returns if the table that stores the user name and ports relation is
140 /// present in the db.
141 /// </summary>
142 /// <returns>A boolean value stating if the UserPorts table is present in the db.</returns>
143 private bool UserPortsTableExists()
144 {
145 // query the sqlite schema using and ado template
146 var template = new AdoTemplate(DbProvider);
147 var result = template.Execute(
148 command =>
149 {
150 command.CommandText = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=:tableName;";
151 var parameter = command.CreateParameter();
152 parameter.ParameterName = ":tableName";
153 parameter.Value = UserPortsTableName;
154 command.Parameters.Add(parameter);
155 var count = command.ExecuteScalar();
156 return (long)count != 0;
157 }
158 );
159
160 return (bool)result;
161 }
162
163 /// <summary>
164 /// Helper method that will create the user porst table in the db when needed.
165 /// </summary>
166 private void CreateUserPortsTable()
167 {
168 // create the table using an ado template.
169 var template = new AdoTemplate(DbProvider);
170 template.ExecuteNonQuery( CommandType.Text,
171 String.Format("CREATE TABLE IF NOT EXISTS {0}('Username' TEXT PRIMARY KEY, 'PythonPort' INT, 'DotNetPort' INT);",
172 UserPortsTableName));
173 }
174
175 /// <summary>
176 /// Helper method that adds a new pair of ports for a user in the db.
177 /// </summary>
178 /// <param name="username">The name of the user whose ports we are going to insert.</param>
179 /// <param name="pythonPort">The port to be used to talk to python.</param>
180 /// <param name="dotNetPort">The port to be used to talk to dotNet.</param>
181 private void InserUserPortsInDb(string username, int pythonPort, int dotNetPort)
182 {
183 var template = new AdoTemplate(DbProvider);
184 template.Execute(
185 command =>
186 {
187 command.CommandText = String.Format(
188 "INSERT INTO {0}(Username, PythonPort, DotNetPort) VALUES (:username, :pythonPort, :dotNetPort)",
189 UserPortsTableName);
190 var usernameParam = command.CreateParameter();
191 usernameParam.ParameterName = ":username";
192 usernameParam.Value = username;
193 command.Parameters.Add(usernameParam);
194
195 var pythonParam = command.CreateParameter();
196 pythonParam.ParameterName = ":pythonPort";
197 pythonParam.Value = pythonPort;
198 command.Parameters.Add(pythonParam);
199
200 var dotNetParam = command.CreateParameter();
201 dotNetParam.ParameterName = ":dotNetPort";
202 dotNetParam.Value = dotNetPort;
203 command.Parameters.Add(dotNetParam);
204
205 return command.ExecuteScalar();
206 }
207 );
208 }
209
210 /// <summary>
211 /// Helper method that allows to retrieve the ports of a user from the db. If the data cannot be retrieved from
212 /// the db.
213 /// </summary>
214 /// <param name="username">The username whose ports we want to retrieve.</param>
215 /// <param name="pythonPort">Out parameter used to return the port used to talk with python.</param>
216 /// <param name="dotNetPort">Out parameter used to return the port used to talk with dot net.</param>
217 /// <returns>A boolean flag that indicates if the data could have been retrieved from the db.</returns>
218 private bool TryGetUserPortFromDb(string username, out int pythonPort, out int dotNetPort)
219 {
220 // try to get the ports from the db, if it is not possible we return false
221 var template = new AdoTemplate(DbProvider);
222 try
223 {
224 var userPorts = (UserPorts)template.Execute(
225 command =>
226 {
227 command.CommandText =
228 String.Format("SELECT PythonPort, DotNetPort FROM {0} WHERE Username=:username;",
229 UserPortsTableName);
230 var parameter = command.CreateParameter();
231 parameter.ParameterName = ":username";
232 parameter.Value = username;
233 command.Parameters.Add(parameter);
234 var reader = command.ExecuteReader();
235 while (reader.Read())
236 {
237 return new UserPorts(System.Convert.ToInt32((long) reader["PythonPort"]),
238 System.Convert.ToInt32((long) reader["DotNetPort"]));
239 }
240 return new UserPorts(-1, -1);
241 }
242 );
243 pythonPort = userPorts.PythonPort;
244 dotNetPort = userPorts.DotNetPort;
245 return false;
246 }
247 catch(Exception)
248 {
249 // an exception will be thrown if we cannot get the data from the db, therefore we return false
250 pythonPort = -1;
251 dotNetPort = -1;
252 return false;
253 }
254 }
255
101 #endregion256 #endregion
102257
103 #region Constructors258 #region Constructors

Subscribers

People subscribed via source and target branches

to all changes: