1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8using System; 9using System.Collections.Generic; 10using System.Text; 11using BerkeleyDB; 12 13namespace ex_lock { 14 class Program { 15 const int EXIT_FAILURE = 1; 16 const int EXIT_SUCCESS = 0; 17 18 static void Main(string[] args) { 19 DatabaseEntry data; 20 DatabaseEnvironment env; 21 List<Lock> lockList; 22 Lock lk; 23 LockMode mode; 24 string buff; 25 string home; 26 string progName; 27 int lockNo; 28 uint doUnLink; 29 uint locker; 30 uint maxLock; 31 32 try { 33 home = args[0]; 34 maxLock = uint.Parse(args[1]); 35 doUnLink = uint.Parse(args[2]); 36 } catch { 37 Usage(); 38 return; 39 } 40 41 data = new DatabaseEntry(); 42 lockList = new List<Lock>(); 43 progName = "ex_csharp_lock"; 44 45 /* Initialize the database environment. */ 46 if (DBInit(out env, home, progName, maxLock, doUnLink) == EXIT_FAILURE) 47 return; 48 49 /* 50 * Accept lock requests. 51 */ 52 try { 53 locker = env.CreateLockerID(); 54 } catch (Exception e) { 55 Console.WriteLine("{0}:{1}\n{2}", 56 e.Source, e.Message, e.StackTrace); 57 env.Close(); 58 return; 59 } 60 61 while (true) { 62 /* Choose getting or releasing lock. */ 63 Console.WriteLine("Operation get/release/quit: "); 64 buff = Console.ReadLine(); 65 if (buff == "get") { 66 /* Input the content to be locked. */ 67 Console.WriteLine("Input text string to lock"); 68 try { 69 buff = Console.ReadLine(); 70 DbtFromString(data, buff); 71 } catch { 72 Console.WriteLine("Input fails"); 73 continue; 74 } 75 76 /* 77 * Choose the locker's mode. More lock modes 78 * could be provided. Only support read/write 79 * mode here. 80 */ 81 while (true) { 82 Console.WriteLine("read or write"); 83 buff = Console.ReadLine(); 84 if (buff == "read" || buff == "write") 85 break; 86 } 87 if (buff == "write") 88 mode = LockMode.WRITE; 89 else 90 mode = LockMode.READ; 91 92 /* Get lock and add it to the list of locks. */ 93 try { 94 lk = env.GetLock(locker, false, data, mode); 95 } catch (Exception e) { 96 Console.WriteLine("{0}:{1}\n{2}", 97 e.Source, e.Message, e.StackTrace); 98 env.Close(); 99 return; 100 } 101 102 Console.WriteLine("Lock #{0} granted", 103 lockList.Count); 104 lockList.Add(lk); 105 } else if (buff == "release") { 106 /* 107 * Release a lock. 108 */ 109 while (true) { 110 /* Input lock number to release. */ 111 Console.WriteLine( 112 "Input lock number to release"); 113 buff = Console.ReadLine(); 114 try { 115 lockNo = int.Parse(buff); 116 if (lockNo > 0 && lockNo < 117 lockList.Count) 118 break; 119 else 120 Console.WriteLine( 121 "Lock number is out of range"); 122 } catch { 123 Console.WriteLine( 124 "Not a valid lock number"); 125 } 126 127 } 128 129 /* Release a lock and remove it from the list of locks. */ 130 try { 131 env.PutLock(lockList[lockNo]); 132 } catch (Exception e) { 133 Console.WriteLine("{0}:{1}\n{2}", 134 e.Source, e.Message, e.StackTrace); 135 env.Close(); 136 return; 137 } 138 139 lockList.Remove(lockList[lockNo]); 140 } else if (buff == "quit") { 141 break; 142 } 143 } 144 145 /*Free locker and close the environment. */ 146 env.FreeLockerID(locker); 147 env.Close(); 148 } 149 150 /* 151 * Create and open environment and database. 152 */ 153 public static int DBInit(out DatabaseEnvironment env, 154 string home, string progName, uint maxLock, 155 uint doUnLink) { 156 DatabaseEnvironmentConfig envConfig; 157 LockingConfig lkConfig; 158 159 /* Configure locking subsystem. */ 160 lkConfig = new LockingConfig(); 161 lkConfig.MaxLocks = maxLock; 162 163 /* Configure environment. */ 164 envConfig = new DatabaseEnvironmentConfig(); 165 envConfig.Create = true; 166 envConfig.ErrorPrefix = progName; 167 envConfig.LockSystemCfg = lkConfig; 168 envConfig.UseLocking = true; 169 170 /* 171 * Optionally remove the environment region and 172 * open the environment. 173 */ 174 try { 175 if (doUnLink == 1) 176 DatabaseEnvironment.Remove(home, true); 177 env = DatabaseEnvironment.Open(home, envConfig); 178 } catch (Exception e) { 179 Console.WriteLine("{0}:{1}\n{2}", 180 e.Source, e.Message, e.StackTrace); 181 env = null; 182 return EXIT_FAILURE; 183 } 184 185 /* 186 try 187 { 188 env = DatabaseEnvironment.Open(home, envConfig); 189 } 190 catch(Exception e) 191 { 192 Console.WriteLine("{0}:{1}\n{2}", 193 e.Source, e.Message, e.StackTrace); 194 env = null; 195 return ExConstants.EXIT_FAILURE; 196 } 197 */ 198 199 return EXIT_SUCCESS; 200 } 201 202 #region Utilities 203 /* Show the usage of the example. */ 204 public static void Usage() { 205 Console.WriteLine( 206 "Usage: [home] [max lock] [1 doUnlink | 0]"); 207 } 208 209 /* Get dbt from a string. */ 210 static void DbtFromString(DatabaseEntry dbt, string s) { 211 dbt.Data = System.Text.Encoding.ASCII.GetBytes(s); 212 } 213 #endregion Utilities 214 } 215} 216