summaryrefslogtreecommitdiff
path: root/cpp/src/Yellow/Service.cpp
blob: 69be947d1e58da338f0c3c2154ecac09e5ad9163 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
// **********************************************************************
//
// Copyright (c) 2001
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Yellow/AdminI.h>
#include <Yellow/QueryI.h>

#include <IceBox/IceBox.h>

#if defined(_WIN32)
#   define YELLOW_SERVICE_API __declspec(dllexport)
#else
#   define YELLOW_SERVICE_API /**/
#endif

using namespace std;
using namespace Ice;
using namespace Yellow;
using namespace Freeze;

namespace Yellow
{

class YELLOW_SERVICE_API ServiceI : public ::IceBox::FreezeService
{
public:

    ServiceI();
    virtual ~ServiceI();

    virtual void start(const string&,
		       const CommunicatorPtr&,
		       const StringSeq&,
		       const ::Freeze::DBEnvironmentPtr&);

    virtual void stop();

private:

    ObjectAdapterPtr _queryAdapter;
    ObjectAdapterPtr _adminAdapter;
};

} // End namespace Yellow

extern "C"
{

//
// Factory function
//
YELLOW_SERVICE_API ::IceBox::FreezeService*
create(Ice::CommunicatorPtr communicator)
{
    return new Yellow::ServiceI;
}

}

Yellow::ServiceI::ServiceI()
{
}

Yellow::ServiceI::~ServiceI()
{
}

void
Yellow::ServiceI::start(const string& name,
			const CommunicatorPtr& communicator,
			const StringSeq& args,
			const DBEnvironmentPtr& dbEnv)
{
    //
    // The Admin & Query interface implementations each have their own
    // map. This is safe because a) no iterators are used on the
    // database, and b) internally the database is synchronized.
    //
    DBPtr dbYellow = dbEnv->openDB("yellow", true);

    //
    // With respect to the adapters I want to support three use-cases:
    //
    // 1) admin is not permitted.
    // 2) both admin & query is permitted on the same endpoint
    // 3) query & admin are permitted, but on seperate endpoitns
    //
    // TODO: At present #2 isn't supported.
    //
    _queryAdapter = communicator->createObjectAdapter(name + ".Query");

    string adminEndpoints = communicator->getProperties()->getProperty(name + ".Admin.Endpoints");
    if(!adminEndpoints.empty())
    {
	_adminAdapter = communicator->createObjectAdapter(name + ".Admin");
	ObjectPtr admin = new AdminI(dbYellow);
	_adminAdapter->add(admin, stringToIdentity(name + "/Admin"));
    }

    ObjectPtr query = new QueryI(dbYellow);
    _queryAdapter->add(query, stringToIdentity(name + "/Query"));

    if(_adminAdapter)
    {
	_adminAdapter->activate();
    }
    _queryAdapter->activate();
}

void
Yellow::ServiceI::stop()
{
    if(_adminAdapter)
    {
	_adminAdapter->deactivate();
    }
    _queryAdapter->deactivate();
}