Limiting the Number of Users

LimitLogins.cs
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02using System; 03using System.IO; 04using Db4objects.Db4o; 05 06namespace 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 MAXIMUM_USERS = 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 < MAXIMUM_USERS; 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}

LimitLogins.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02Imports System 03Imports System.IO 04Imports Db4objects.Db4o 05 06Namespace 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 MAXIMUM_USERS 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 MAXIMUM_USERS - 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 47End Namespace