01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
using System; 03
using System.IO; 04
using Db4objects.Db4o; 05
06
namespace Db4objects.Db4odoc.Semaphores 07
{ 08
/** 09
* This class demonstrates the use of semaphores to limit the 10
* number of logins to a server. 11
*/ 12
public class LimitLogins 13
{ 14
15
readonly static string Host = "localhost"; 16
readonly static int Port = 4455; 17
readonly static string User = "db4o"; 18
readonly static string Password= "db4o"; 19
20
readonly static int MaximumUsers = 10; 21
22
public static IObjectContainer Login() 23
{ 24
25
IObjectContainer objectContainer; 26
try 27
{ 28
objectContainer = Db4oFactory.OpenClient(Host, Port, User, Password); 29
} 30
catch (IOException e) 31
{ 32
return null; 33
} 34
35
bool allowedToLogin = false; 36
37
for (int i = 0; i < MaximumUsers; i++) 38
{ 39
if(objectContainer.Ext().SetSemaphore("max_user_check_" + i, 0)) 40
{ 41
allowedToLogin = true; 42
break; 43
} 44
} 45
46
if(! allowedToLogin) 47
{ 48
objectContainer.Close(); 49
return null; 50
} 51
52
return objectContainer; 53
} 54
} 55
}
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02
Imports System 03
Imports System.IO 04
Imports Db4objects.Db4o 05
06
Namespace Db4objects.Db4odoc.Semaphores 07
' 08
' This class demonstrates the use of semaphores to limit the 09
' number of logins to a server. 10
' 11
Public Class LimitLogins 12
13
Shared ReadOnly Host As String = "localhost" 14
Shared ReadOnly Port As Integer = 4455 15
Shared ReadOnly User As String = "db4o" 16
Shared ReadOnly Password As String = "db4o" 17
18
Shared ReadOnly MaximumUsers As Integer = 10 19
20
Public Shared Function Login() As IObjectContainer 21
22
Dim objectContainer As IObjectContainer 23
Try 24
objectContainer = Db4oFactory.OpenClient(Host, Port, User, Password) 25
Catch e As IOException 26
Return Nothing 27
End Try 28
29
Dim allowedToLogin As Boolean = False 30
31
Dim i As Integer 32
For i = 0 To MaximumUsers - 1 Step i + 1 33
If objectContainer.Ext().SetSemaphore("max_user_check_" + i.ToString(), 0) Then 34
allowedToLogin = True 35
Exit For 36
End If 37
Next 38
39
If Not allowedToLogin Then 40
objectContainer.Close() 41
Return Nothing 42
End If 43
44
Return objectContainer 45
End Function 46
End Class 47
End Namespace