summaryrefslogtreecommitdiff
path: root/cs/demo/Ice/session/SessionI.cs
blob: 83fb2d189486d50a2eb5377bbed249394de0bc0b (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using Demo;
using System;
using System.Collections;

public class SessionI : SessionDisp_
{
    public SessionI(string name)
    {
        _name = name;
        _timestamp = System.DateTime.Now;
        _nextId = 0;
        _destroy = false;
        _objs = new ArrayList();

        Console.Out.WriteLine("The session " + _name + " is now created.");
    }

    public override HelloPrx createHello(Ice.Current c)
    {
        lock(this)
        {
            if(_destroy)
            {
                throw new Ice.ObjectNotExistException();
            }
            
            HelloPrx hello = HelloPrxHelper.uncheckedCast(c.adapter.addWithUUID(new HelloI(_name, _nextId++)));
            _objs.Add(hello);
            return hello;
        }
    }

    public override void refresh(Ice.Current c)
    {
        lock(this)
        {
            if(_destroy)
            {
                throw new Ice.ObjectNotExistException();
            }
            
            _timestamp = System.DateTime.Now;
        }
    }

    public override string getName(Ice.Current c)
    {
        lock(this)
        {
            if(_destroy)
            {
                throw new Ice.ObjectNotExistException();
            }
            
            return _name;
        }
    }

    public override void destroy(Ice.Current c)
    {
        lock(this)
        {
            if(_destroy)
            {
                throw new Ice.ObjectNotExistException();
            }
            
            _destroy = true;
            
            Console.Out.WriteLine("The session " + _name +  " is now destroyed.");
            try
            {
                c.adapter.remove(c.id);
                foreach(HelloPrx p in _objs)
                {
                    c.adapter.remove(p.ice_getIdentity());
                }
            }
            catch(Ice.ObjectAdapterDeactivatedException)
            {
                // This method is called on shutdown of the server, in which
                // case this exception is expected.
            }
        }

        _objs.Clear();
    }

    public System.DateTime timestamp() 
    {
        lock(this)
        {
            if(_destroy)
            {
                throw new Ice.ObjectNotExistException();
            }
            return _timestamp;
        }
    }

    private string _name;
    private System.DateTime _timestamp; // The last time the session was refreshed.
    private int _nextId; // The per-session id of the next hello object. This is used for tracing purposes.
    private ArrayList _objs; // List of per-session allocated hello objects.
    private bool _destroy;
}