-
Notifications
You must be signed in to change notification settings - Fork 8
/
ClientServerExample.cs
204 lines (181 loc) · 7.65 KB
/
ClientServerExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.IO;
using Db4objects.Db4o;
using Db4objects.Db4o.CS;
using Db4objects.Db4o.CS.Config;
namespace Db4odoc.Tutorial.F1.Chapter6
{
public class ClientServerExample : Util
{
readonly static string YapFileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"formula1.yap");
readonly static int ServerPort = 0xdb40;
readonly static string ServerUser = "user";
readonly static string ServerPassword = "password";
public static void Main(string[] args)
{
File.Delete(YapFileName);
AccessLocalServer();
File.Delete(YapFileName);
using (IObjectContainer db = Db4oEmbedded.OpenFile(YapFileName))
{
SetFirstCar(db);
SetSecondCar(db);
}
IServerConfiguration config = Db4oClientServer.NewServerConfiguration();
config.Common.ObjectClass(typeof(Car)).UpdateDepth(3);
using (IObjectServer server = Db4oClientServer.OpenServer(config,
YapFileName, 0))
{
QueryLocalServer(server);
DemonstrateLocalReadCommitted(server);
DemonstrateLocalRollback(server);
server.Dispose();
}
AccessRemoteServer();
config = Db4oClientServer.NewServerConfiguration();
config.TimeoutServerSocket = 1000 * 10;
using (IObjectServer server =
Db4oClientServer.OpenServer(config,
YapFileName, ServerPort))
{
server.GrantAccess(ServerUser, ServerPassword);
QueryRemoteServer(ServerPort, ServerUser, ServerPassword);
DemonstrateRemoteReadCommitted(ServerPort, ServerUser, ServerPassword);
DemonstrateRemoteRollback(ServerPort, ServerUser, ServerPassword);
PrintMsg();
}
}
public static void SetFirstCar(IObjectContainer db)
{
Pilot pilot = new Pilot("Rubens Barrichello", 99);
Car car = new Car("BMW");
car.Pilot = pilot;
db.Store(car);
}
public static void SetSecondCar(IObjectContainer db)
{
Pilot pilot = new Pilot("Michael Schumacher", 100);
Car car = new Car("Ferrari");
car.Pilot = pilot;
db.Store(car);
}
public static void AccessLocalServer()
{
using (IObjectServer server = Db4oClientServer.OpenServer(YapFileName, 0))
{
using (IObjectContainer client = server.OpenClient())
{
// Do something with this client, or open more clients
}
}
}
public static void QueryLocalServer(IObjectServer server)
{
using (IObjectContainer client = server.OpenClient())
{
ListResult(client.QueryByExample(new Car(null)));
}
}
public static void DemonstrateLocalReadCommitted(IObjectServer server)
{
using (IObjectContainer client1 = server.OpenClient(),
client2 = server.OpenClient())
{
Pilot pilot = new Pilot("David Coulthard", 98);
var result = client1.QueryByExample(new Car("BMW"));
Car car = (Car) result.Next();
car.Pilot = pilot;
client1.Store(car);
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
client1.Commit();
ListResult(client1.QueryByExample(typeof(Car)));
ListRefreshedResult(client2, client2.QueryByExample(typeof(Car)), 2);
}
}
public static void DemonstrateLocalRollback(IObjectServer server)
{
using (IObjectContainer client1 = server.OpenClient(),
client2 = server.OpenClient())
{
var result = client1.QueryByExample(new Car("BMW"));
Car car = result.Next();
car.Pilot = new Pilot("Someone else", 0);
client1.Store(car);
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
client1.Rollback();
client1.Ext().Refresh(car, 2);
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
}
}
public static void AccessRemoteServer()
{
var config = Db4oClientServer.NewServerConfiguration();
config.TimeoutServerSocket = 1000 * 10;
using (IObjectServer server = Db4oClientServer.OpenServer(config, YapFileName, ServerPort))
{
server.GrantAccess(ServerUser, ServerPassword);
using (IObjectContainer client =
Db4oClientServer.OpenClient("localhost", ServerPort, ServerUser, ServerPassword))
{
// Do something with this client, or open more clients
}
PrintMsg();
// Environment.Exit(0);
}
}
private static void PrintMsg()
{
System.Console.WriteLine(@"
When PORT > 0,
Client-Server, Some Systems Server.Close() will wait long time until timeout.
about the Network Timeout setting,
you can call 'Environment.Exit(0)' instead of close()");
}
public static void QueryRemoteServer(int port, string user, string password)
{
using (IObjectContainer client = Db4oClientServer.OpenClient("localhost", port, user, password))
{
ListResult(client.QueryByExample(new Car(null)));
}
}
public static void DemonstrateRemoteReadCommitted(int port, string user, string password)
{
using (IObjectContainer client1 = Db4oClientServer.OpenClient("localhost", port, user, password),
client2 = Db4oClientServer.OpenClient("localhost", port, user, password))
{
Pilot pilot = new Pilot("Jenson Button", 97);
var result = client1.QueryByExample(new Car(null));
Car car = result.Next();
car.Pilot = pilot;
client1.Store(car);
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
client1.Commit();
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
}
}
public static void DemonstrateRemoteRollback(int port, string user, string password)
{
using (IObjectContainer client1 = Db4oClientServer.OpenClient("localhost", port, user, password),
client2 = Db4oClientServer.OpenClient("localhost", port, user, password))
{
var result = client1.QueryByExample(new Car(null));
Car car = result.Next();
car.Pilot = new Pilot("Someone else", 0);
client1.Store(car);
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
client1.Rollback();
client1.Ext().Refresh(car, 2);
ListResult(client1.QueryByExample(new Car(null)));
ListResult(client2.QueryByExample(new Car(null)));
}
}
}
}