From d67517b40560dba404cae57f26c7b6ab6e3b12f3 Mon Sep 17 00:00:00 2001 From: Marty Kraimer Date: Fri, 12 Apr 2013 08:06:48 -0400 Subject: [PATCH] interim commit --- documentation/pvDatabaseCPP.html | 629 +++++++++++------------ src/Makefile | 5 + src/database/exampleCounter.h | 74 +++ src/database/powerSupplyRecordTest.h | 8 +- src/database/pvDatabase.cpp | 34 +- src/database/pvDatabase.h | 320 +++++++++++- src/database/pvRecord.cpp | 40 +- src/pvAccess/channelLocal.cpp | 687 ++++++++++++++++++++++++++ src/pvAccess/channelProviderLocal.cpp | 222 +++++++++ src/pvAccess/channelProviderLocal.h | 164 ++++++ test/Makefile | 1 + test/server/Makefile | 17 + test/server/testExampleServerMain.cpp | 102 ++++ 13 files changed, 1910 insertions(+), 393 deletions(-) create mode 100644 src/database/exampleCounter.h create mode 100644 src/pvAccess/channelLocal.cpp create mode 100644 src/pvAccess/channelProviderLocal.cpp create mode 100644 src/pvAccess/channelProviderLocal.h create mode 100644 test/server/Makefile create mode 100644 test/server/testExampleServerMain.cpp diff --git a/documentation/pvDatabaseCPP.html b/documentation/pvDatabaseCPP.html index a61cfe3..5ee9213 100644 --- a/documentation/pvDatabaseCPP.html +++ b/documentation/pvDatabaseCPP.html @@ -38,7 +38,7 @@

pvDatabaseCPP

-

EPICS v4 Working Group, Working Draft, 11-Dec-2012

+

EPICS v4 Working Group, Working Draft, 09-Apr-2013

Latest version:
This version:
pvDatabaseCPP20121211.html + href="http://epics-pvdata.hg.sourceforge.net/hgweb/epics-pvdata/pvDatabaseCPP/raw-file/tip/documentation/pvDatabaseCPP_20130409.html">pvDatabaseCPP20131211.html
Previous version:
pvDatabaseCPP_20121127.html + href="http://epics-pvdata.hg.sourceforge.net/hgweb/epics-pvdata/pvDatabaseCPP/raw-file/tip/documentation/pvDatabaseCPP_20131211.html">pvDatabaseCPP20131211.html
Editors:
Marty Kraimer, BNL
@@ -67,7 +67,7 @@ href="http://epics-pvdata.sourceforge.net/LICENSE.html">EPICS open source licens which is a framework for implementing a network accessable database of smart memory resident records. Network access is via pvAccess. The data in each record is a top level PVStructure as defined by pvData. The framework includes a complete implementation of ChannelProvider as defined by pvAccess. -The framework must be extended in order to create record instances. +The framework can be extended in order to create record instances that implements services. The minimum that an extenson must provide is a top level PVStructure and a process method but the framework provides for complex extensions.

@@ -97,7 +97,7 @@ V4 control system programming environment:

Status of this Document

-

This is the 11-Dec-2012 version of the definition of pvDatabaseCPP. +

This is the 09-Apr-2013 version of the definition of pvDatabaseCPP.

This is the beginning of the implementation of pvDataBaseCPP. It describes the features that will be provided. @@ -113,22 +113,16 @@ The class definition for PVDatabase are defined but not implemented.

Introduction

Overview

-

This document descibes a C++ implementation of some of the components in pvIOCJava. -It extracts the core components required to create a network accessible database of smart +

A brief description of a pvDatabase is that it is a set of network accessible, smart, memory resident records. -pvDatabaseCPP does not and will not implement any of the specialized support that pvIOCJava -provides. Instead other projects will implement the specialized support. -It is expected that many services will be created that do not require the full features provided -by pvIOCJava. In the future pvIOCJava should be split into multiple projects with one of -them named pvDatabaseJava. -

- -

A brief description of a pvDatase is that it is a network accessible set of smart memory resident -records. Each record has data composed of a top level PVStructure. Each record has a name which is -the channelName for pvAccess. A local Channel Provider implements the complete ChannelProvider and +Each record has data composed of a top level PVStructure. +Each record has a name which is the channelName for pvAccess. +A local Channel Provider implements the complete ChannelProvider and Channel interfaces as defined by pvAccess. +The local provider provides access to the records in the pvDatabase. This local provider is accessed by the remote pvAccess server. -A record is smart because code can be attached to a record.

+A record is smart because code can be attached to a record, which is accessed via a method named process.

+

This document describes components that provides the following features:

database
@@ -142,135 +136,182 @@ A record is smart because code can be attached to a record.

This is a database of pvRecords. Records can be added and removed from a database.
-
localChannelProvider
-
This is a complete implementation of ChannelProvider and Channel as defined by pvAccess. +
pvAccess
+
This is a complete implementation of a local ChannelProvider and Channel as defined by pvAccess. It is used by the server side of pvAccess to attach to pvRecords. This component also includes the monitor and pvCopy components from pvIOCJava
-

database does not itself implement pvRecord instances. -Instead it provides a base classes that make it easy to create record instances. -What does have to be implemented is a top +

database +provides base classes that make it easy to create record instances. +The code attached to each record must create the top level PVStructure and the following two methods:

+
init
+
This is a method for initializing the support. + It returns true if successful and false otherwise. +
process
This is what makes a record smart. - What process does is up to the implementation except that it must decide if - it's execution model is synchronous or asynchronous. - Synchronous means that when process returns the processing is complete. - Asynchronous means that when process returns the processing is not complete. - Instead process invokes other threads that will complete the processing at a later time.
-
isSynchronous
-
Which execution model is being implemented.
+
+

Relationship with pvIOCJava.

+

This document descibes a C++ implementation of some of the components in pvIOCJava, +which also implements a pvDatabase. +It extracts the core components required to create a network accessible database of smart +memory resident records. +pvDatabaseCPP does not implement any of the specialized support that pvIOCJava +provides. +It is expected that many services will be created that do not require the full features provided +by pvIOCJava. In the future pvIOCJava should be split into multiple projects with one of +them named pvDatabaseJava.

+

Similar to epics base, pvIOCJava implements the concept of synchronous and asynchronous record processing. +For pvDatabaseCPP the process method is allowed to block. +Until a need is demonstrated this will remain true. +The main user of a pvDatabase is pvAccess, and in particular, remote pvAccess. +The server side of remote pvAccess creates two threads for each client and always accesses +a record via these threads. +It is expected that these threads will be sufficient to efficently handle all channel methods except +channelRPC. For channelRPC pvAccess provides (or will provide) a thread pool for channelRPC requests. +If, in the future, a scanning facility is provided by pvDatabaseCPP or some other facility, +then the scanning facility will have to provide some way of handling process requests that block.

+

Example PVRecord Extension

-

Directory example/record has an example PVRecord implementation. -It implements a counter. -The top level structure is:

+

The example implements a simple counter. +The example can be run on linux as follows:

-structure
-    long value
-
-

NOTE: The example compiles but does not build because nothing -is implemented.

+mrk> pwd +/home/hg/pvDatabaseCPP +mrk> bin/linux-x86_64/exampleCounter -

exampleRecord.h

-

This is the class description. -The example extends PVRecord.

+ +

The example consists of two components:

+
+
ExampleCounter.h
+
The source code for the counter.
+
exampleCounterMain.cpp
+
A main program that runs the example so that it can be accessed + by a pvAccess client.
+
+

ExampleCounter.h

+

The example resides in src/database. +The complete implementation is in the header file. +A serious implementation would probably break the code into two files: +1) a header, and 2) the implementation. The description consists of

-class ExampleRecord :
-  public virtual PVRecord
+class ExampleCounter;
+typedef std::tr1::shared_ptr<ExampleCounter> ExampleCounterPtr;
+
+class ExampleCounter :
+    public PVRecord
 {
 public:
-    POINTER_DEFINITIONS(ExampleRecord);
-    static PVRecordPtr create(epics::pvData::String const & recordName);
-    virtual ~ExampleRecord();
-    virtual bool isSynchronous();
-    virtual void process(
-        epics::pvDatabase::RecordProcessRequesterPtr const &processRequester);
+    POINTER_DEFINITIONS(ExampleCounter);
+    static ExampleCounterPtr create(
+        epics::pvData::String const & recordName);
+    virtual ~ExampleCounter() {}
+    virtual bool init();
+    virtual void process();
 private:
-    ExampleRecord(epics::pvData::String const & recordName,
-        epics::pvData::PVStructurePtr const & pvStructure,
-        epics::pvData::PVLongPtr const &pvValue);
+    ExampleCounter(epics::pvData::String const & recordName,
+        epics::pvData::PVStructurePtr const & pvStructure);
     epics::pvData::PVLongPtr pvValue;
 };
 

where

create
-
This is example specific. See the implemention for details.
-
~ExampleRecord
+
This is example specific but each support should provide + a similar static method. +
+
~ExampleCounter
The destructor must be declared virtual.
-
isSynchronous
-
The implementation must say if process is synchronous or asynchronous.
+
init
+
A method to initialize the support. It returns true + if initialization is successful and false if not. + NOTE that this is a virtual method of PVRecord itself.
process
-
The implementation.
-
ExampleRecord
+
+ This again is a virtual method of PVRecord. +
+
ExampleCounter
For the example this is private.
+
pvValue
+
This is the field of the top level structure that process + accesses. +
- -

exampleRecord.cpp

-

This is the class implementation.

+

The implementation of create is:

-ExampleRecord::~ExampleRecord(){}
-
-PVRecordPtr ExampleRecord::create(String const & recordName)
+ExampleCounterPtr ExampleCounter::create(
+    epics::pvData::String const & recordName)
 {
-    String properties;
-    PVStructurePtr pvStructure = getStandardPVField()->scalar(pvLong,properties);
-    PVLongPtr pvValue =  pvStructure->getLongField("value");
-    PVRecordPtr pvRecord(new ExampleRecord(recordName,pvStructure,pvValue));
+    epics::pvData::PVStructurePtr pvStructure =
+       epics::pvData::getStandardPVField()->scalar(epics::pvData::pvDouble,"");
+    ExampleCounterPtr pvRecord(
+        new ExampleCounter(recordName,pvStructure));
+    if(!pvRecord->init()) pvRecord.reset();
     return pvRecord;
 }
-
-ExampleRecord::ExampleRecord(
-    String const & recordName,
-    PVStructurePtr const & pvStructure,
-    PVLongPtr const &pvValue)
-: PVRecord(recordName,pvStructure),
-  pvValue(pvValue)
-{}
-
-bool ExampleRecord::isSynchronous() {return true;}
-
-void ExampleRecord::process(
-    RecordProcessRequesterPtr const &processRequester,bool alreadyLocked)
+
+This: +
    +
  • Creates the top level structure.
  • +
  • Creates a ExampleCounterPtr via the constructor.
  • +
  • Calls init and if it fails resets the shared pointer.
  • +
  • Returns the shared pointer to the newly created record.
  • +
+

The private constructor is just:

+
+ExampleCounter::ExampleCounter(
+    epics::pvData::String const & recordName,
+    epics::pvData::PVStructurePtr const & pvStructure)
+: PVRecord(recordName,pvStructure)
+{ }
+
+The example is very simple. It just calls the base class constructor. +

The implementation of init is:

+
+bool ExampleCounter::init()
 {
-    if(!alreadyLocked) lock();
-    pvValue->put(pvValue->get() + 1);
-    processRequester->recordProcessResult(Status::Ok);
-    unlock();
-    processRequester->recordProcessComplete();
-    dequeueProcessRequest(processRequester);
+
+    initPVRecord();
+    epics::pvData::PVFieldPtr pvField;
+    pvValue = getPVStructure()->getLongField("value");
+    if(pvValue.get()==NULL) return false;
+    return true;
 }
 
-

where

-
-
create
-
Creates a PVStructure with a single subfield named value. - It gets the interface to the value field. - It then creates an ExampleRecord and returns it. -
-
~ExampleRecord
-
Does not have to do anything because of shared pointers.
-
ExampleRecord
-
Calls the base class constructor and sets pvValue.
-
isSynchronous
-
The example is synchronous.
-
process
-
Gets the curent value, increments it, and puts the new value. - It than calls two processRequester callbacks.
-
- -

exampleRecordMain.cpp

-

This is a main for creating and running the example.

+This: +
    +
  • Calls initRecord which is implemented by the base class. + It MUST be called.
  • +
  • Calls getLongField to get the interface to the value field, + which must be a scalar with type long.
  • +
  • If a long value field was not found it returns false.
  • +
  • Returns true
  • +
+

The implementation of process is:

+
+void ExampleCounter::process()
+{
+    pvValue->put(pvValue->get() + 1.0);
+}
+
+It just adds 1.0 to the current value. +

exampleCounterMain.cpp

+

This is in test/server. +The main program is:

 int main(int argc,char *argv[])
 {
-    String recordName("exampleRecord");
-    PVRecordPtr pvRecord = ExampleRecord::create(recordName);
-    PVDatabasePtr pvDatabase = PVDatabase::getMaster();
-    pvDatabase->addRecord(pvRecord);
-    cout << recordName << "\n";
+    PVDatabasePtr master = PVDatabase::getMaster();
+    ChannelProviderLocalPtr channelProvider = ChannelProviderLocal::create();
+    String recordName("exampleCounter");
+    PVRecordPtr pvRecord = ExampleCounter::create(recordName);
+    bool result = master->addRecord(pvRecord);
+    cout << "result of addRecord " << recordName << " " << result << endl;
+    pvRecord.reset();
+    cout << "exampleServer\n";
     string str;
     while(true) {
         cout << "Type exit to stop: \n";
@@ -281,16 +322,15 @@ int main(int argc,char *argv[])
     return 0;
 }
 
-

The main program creates an example record and adds it to the database. -It then runs until the process is stopped by typing exit. -

Until the process is stopped, -pvAccess clients can put and get the value field. -For example

-
-pvget exampleRecord
-pvput exampleRecord 5
-
-

Will both work.

+This: +
    +
  • Gets a pointer to the master database.
  • +
  • Creates the local Channel Provider. This starts the pvAccess server.
  • +
  • Creates a ExampleCounter record with the name exampleCounter +
  • +
  • Prints exampleCounter on standard out.
  • +
  • Runs forever until the user types exit on standard in.
  • +

Phased Development

This documentation describes the first phase of a phased implementation of pvDatabaseCPP:

@@ -299,16 +339,20 @@ pvput exampleRecord 5
pvDatabase
Database of PVRecords. Has methods find, add, and remove.
Local Channel Provider
-
These two features will be the first phase. - But only synchronous record processing will be supported.
+
Complete implementation of ChannelProvider and Channel. + This means that pvCopy and monitor are also implemented.
-

Future phases of pvDatabaseCPP should include:

+

Future phases of pvDatabaseCPP might include:

Install
-
This provides on-line add and delete.
+
This provides complete support for on-line add and delete + of sets of records. + With the first phase each "service" is responsible for it's own implementation. + All that is provided is addRecord and removeRecord. +
Field support
-
Add ability to optionally add support to fields. - In addition some of the basic support defined in pvIOCJava will also be implemented.
+
Add the ability to optionally add support to fields. + In addition some of the basic support defined in pvIOCJava could also be implemented.
XML parser
This provides the ability to create record instances without writing any code.
@@ -326,10 +370,10 @@ The rest of this document discusses only the first phase.

This provides the ability to monitor changes to fields of a record.
PVRecord and PVDatabase
Defined below.
-
local ChannelProvider
+
The localChannelProvider itself
This is the pvAccess package in pvIOCJava. The localChannelProvider will access data from PVRecords. - It will implement all channel methods except channelRPC.
+ It will implement all channel methods.

Minumum Features Required for pvRecord

The first phase will only implement record processing, i. e. @@ -340,9 +384,9 @@ The following are the minimium features required

PVDatabase
This holds a set of PVRecords. It has methods to find, add, and remove records.
PVRecord
-
This, and a set of related interfaces, provide the following: +
This, and a set of related interfaces, provides the following:
-
PVStructure
+
Access to top level PVStructure
PVRecord is a wrapper on a top level pvStructure.
Record locking
A record can be locked and unlocked. @@ -353,12 +397,45 @@ The following are the minimium features required

-

The following sections provide a first attempt to describe the classes required for the first -phase.

-

The last section gives a brief overview of the features provided by pvIOCJava.

+

The following sections describes the classes required for the first phase.

database

-

The classes in pvDatabase.h implement a database of memory resident +

This directory has the following files:

+
+
pvDatabase.h
+
+ This is what is described in this section. +
+
pvDatabase.cpp
+
+ The implementation of PVDatabase. +
+
pvRecord.cpp
+
+ The implementation of the base class for PVREcord. + It can also implement record instances with a process + method does nothing. + This can be used to create a "dumb" record where all changes are + done by clients. + The complete implementation is provided in the header file. + Thus code will be generated only if other code includes the + header file and creates a record instance. +
+
exampleCounter.h
+
+ This was described in the introduction. +
+
powerSupplyRecordTest.h
+
+ This provides code that simulates a power supply. + It computes the current from the voltage and power. + It is used for testing. + The complete implementation is provided in the header file. + Thus code will be generated only if other code includes the + header file and creates a record instance. +
+
+

The classes in pvDatabase.h describe a database of memory resident smart records. It describes the following classes:

@@ -371,11 +448,7 @@ It describes the following classes:

PVRecordClient
This is called by anything that acceses PVRecord.
PVListener
-
This is implemented by anything that wants to trap calls to the PVRecord::message.
-
RecordProcessRequester
-
This is implemented by anything that calls PVRecord::queueProcessRequest.
-
RecordPutRequester
-
This is implemented by anything that calls PVRecord::queuePutRequest.
+
This is implemented by anything that wants to trap calls to PVRecord::message.
PVDatabase
This is a database of PVRecords.
@@ -419,6 +492,10 @@ typedef std::tr1::shared_ptr<PVDatabase> PVDatabasePtr; i. e. via pvAccess. Thus this section is mainly of interest to the local channel provider and record implementers. +
  • Most readers will not care about most of the PVRecord methods. + Most of the methods are used by the pvAccess code. + Service implementers will mostly be interested in methods init and process. + These are described first.
    PVRecord Methods
    @@ -428,16 +505,17 @@ class PVRecord
     {
     public:
         POINTER_DEFINITIONS(PVRecord);
    +
    +    virtual bool init() {initPVRecord(); return true;}
    +    virtual void process() {}
    +
    +    static PVRecordPtr create(
    +        epics::pvData::String const & recordName,
    +        epics::pvData::PVStructurePtr const & pvStructure);
         PVRecord(
             epics::pvData::String const & recordName,
             epics::pvData::PVStructurePtr const & pvStructure);
         virtual ~PVRecord();
    -    virtual void process(
    -        RecordProcessRequesterPtr const &recordProcessRequester,
    -        bool alreadyLocked) = 0;
    -    virtual bool isSynchronous() = 0;
    -    virtual bool requestImmediatePut(epics::pvData::PVFieldPtr const &pvField);
    -    virtual void immediatePutDone();
         virtual void destroy();
         epics::pvData::String getRecordName();
         PVRecordStructurePtr getPVRecordStructure();
    @@ -445,25 +523,20 @@ public:
             epics::pvData::PVFieldPtr const & pvField);
         bool addRequester(epics::pvData::RequesterPtr const & requester);
         bool removeRequester(epics::pvData::RequesterPtr const & requester);
    +    inline void lock_guard() { epics::pvData::Lock theLock(mutex); }
         void lock();
         void unlock();
         bool tryLock();
         void lockOtherRecord(PVRecordPtr const & otherRecord);
    -    void addPVRecordClient(PVRecordClientPtr const & pvRecordClient);
    -    void removePVRecordClient(PVRecordClientPtr const & pvRecordClient);
    +    bool addPVRecordClient(PVRecordClientPtr const & pvRecordClient);
    +    bool removePVRecordClient(PVRecordClientPtr const & pvRecordClient);
         void detachClients();
    +    bool addListener(PVListenerPtr const & pvListener);
    +    bool removeListener(PVListenerPtr const & pvListener);
         void beginGroupPut();
         void endGroupPut();
    -    void queueProcessRequest(
    -        RecordProcessRequesterPtr const &recordProcessRequester);
    -    void dequeueProcessRequest(
    -        RecordProcessRequesterPtr const &recordProcessRequester);
    -    void queuePutRequest(
    -        RecordPutRequesterPtr const &recordPutRequester);
    -    void putDone(
    -        RecordPutRequesterPtr const &recordPutRequester);
    -    virtual epics::pvData::String getRequesterName();
    -    void message(
    +    epics::pvData::String getRequesterName() {return getRecordName();}
    +    virtual void message(
             epics::pvData::String const & message,
             epics::pvData::MessageType messageType);
         void message(
    @@ -472,63 +545,39 @@ public:
             epics::pvData::MessageType messageType);
         void toString(epics::pvData::StringBuilder buf);
         void toString(epics::pvData::StringBuilder buf,int indentLevel);
    -    //init MUST be called after derived class is constructed
    -    void init();
    -
    -};
    +protected:
    +    void initPVRecord();
    +    epics::pvData::PVStructurePtr getPVStructure();
    +    PVRecordPtr getPtrSelf()
    +    {
    +        return shared_from_this();
    +    }
    +private:
    +...
    +}
     

    The methods are:

    +
    init
    +
    Virtual method. +

    Derived classes must implement this method. + This method Must call initPVRecord.

    +
    +
    process
    +
    Virtual method. +

    Derived classes must implement this method. + The base implementation does nothing.

    +
    +
    create
    +
    Static method to create dumb records, i.e. records with a process method + that does nothing.
    PVRecord
    The constructor. It requires a recordName and a top level PVStructure.
    ~PVRecord
    The destructor which must be virtual. A derived class must also have a virtual destructor.
    -
    process
    -
    Pure virtual method. -

    Derived classes must implement this method.

    -

    A client must only call this method when - RecordProcessRequester::becomeProcessor is called as a result - of a queueProcessRequest. - A client can either call lock before calling processs - or let process lock the record. - If a client wants to put data into the record it should lock, put, and then call - process.

    -

    If the record is synchronous, process will return only when all processing - is complete. If the record is asynchronous then process arranges for some - other thread to do the processing and returns.

    -

    When processing is done the record calls two client callbacks:

    -
    -
    RecordProcessRequester::recordProcessResult
    -
    This is called with the record still locked. - The clients can get data from the record.
    -
    RecordProcessRequester::recordProcessComplete
    -
    This is called with the record unlocked. - The client can no longer access the record.
    -
    -
    -
    isSynchronous
    -
    Pure virtual method. Derived classes must implement this method.
    -
    requestImmediatePut
    -
    This is a virtual method. -

    The purpose is to allow the implementation to provide fields - that allow a client to abort process. - For example a motor record might provide a field stop

    -

    The default always returns false.

    -

    A record implementation can override the default and return true. - In it does requestImmediatePut it returns with the record locked.

    -

    The client can change the value of the associated field and then call - immediatePutDone

    -
    -
    immediatePutDone
    -
    This is a virtual method. -

    The default does nothing.

    -

    Must be called by client as a result of a call to requestImmediatePut - that returns true.

    -
    destroy
    This is a virtual method. -

    The default does nothing.

    getRecordName
    Return the recordName.
    @@ -540,6 +589,9 @@ public:
    Add a requester to receive messages.
    removeRequester
    Remove a message requester.
    +
    lock_guard
    +
    This is an inline method that locks the record. The record will automatically + be unlocked when control leaves the block that has the call.
    lock
    unlock
    Lock and Unlock the record. @@ -572,31 +624,18 @@ public:
    endGroupPut
    End a group of puts. This results in all registered PVListeners being called.
    -
    queueProcessRequest
    -
    Queue a process request.
    -
    dequeueProcessRequest
    -
    This must be called by record implementation after it has - completed a process request. -
    -
    queuePutRequest
    -
    Queue a put request. -

    This is for code that wants to change data in a record without processing. - If RecordPutRequester::requestResult is called with result true - then the record is locked and the client can make changes. - When done the client must call putDone

    -
    -
    putDone
    -
    Called by RecordPutRequester after changing values in record. - This method unlocks the record
    getRequesterName
    virtual method of Requester
    message
    Can be called by implementation code. The message will be sent to every requester.
    -
    init
    -
    This method must be called by derived class - after class is completely constructed.
    +
    toString
    +
    Just calls the top level PVStructure toString method.
    +
    initRecord
    +
    This method must be called by derived class.
    +
    getPVStructure
    +
    Called by derived class.

    class PVRecordField

    @@ -621,6 +660,14 @@ public:
         virtual void message(
             epics::pvData::String const & message,
             epics::pvData::MessageType messageType);
    +protected:
    +    PVRecordFieldPtr getPtrSelf()
    +    {
    +        return shared_from_this();
    +    }
    +    virtual void init();
    +private:
    +...
     };
     

    When PVRecord is created it creates a PVRecordField for every field in the PVStructure @@ -669,6 +716,10 @@ public: epics::pvData::PVStructurePtr getPVStructure(); virtual void removeListener(PVListenerPtr const & pvListener); virtual void postPut(); +protected: + virtual void init(); +private: +... };

    When PVRecord is created it creates a PVRecordStructure for every structure field in the PVStructure @@ -737,63 +788,6 @@ public:

    endGroupPut
    A related set of changes is done.
  • -

    class RecordProcessRequester

    -
    -class RecordProcessRequester :
    -    virtual public PVRecordClient,
    -    virtual public epics::pvData::Requester
    -{
    -public:
    -    POINTER_DEFINITIONS(RecordProcessRequester);
    -    virtual ~RecordProcessRequester();
    -    virtual void recordDestroyed() = 0;
    -    virtual void becomeProcessor() = 0;
    -    virtual void recordProcessResult(epics::pvData::Status status) = 0;
    -    virtual void recordProcessComplete() = 0;
    -};
    -
    -

    where

    -
    -
    ~RecordProcessRequester
    -
    The destructor.
    -
    recordDestroyed
    -
    Record is being destroyed.
    -
    becomeProcessor
    -
    Called as a result of queueRequeProcessst. The requester can the call process.
    -
    recordProcessResult
    -
    The results of record processing. - This is called with the record locked so that the process requester - can access data from the record.
    -
    recordProcessComplete
    -
    Processing is complete. - This is called with the record unlocked. - If the process requester called process with leaveActive true then the requester - must call setInactive.
    -
    -

    class RecordPutRequester

    -
    -class RecordPutRequester :
    -    virtual public PVRecordClient
    -{
    -public:
    -    POINTER_DEFINITIONS(RecordPutRequester);
    -    virtual ~RecordPutRequester();
    -    virtual void requestResult(bool result) = 0;
    -};
    -
    -

    where

    -
    -
    ~RecordPutRequester
    -
    The destructor.
    -
    requestResult
    -
    Result of a call to queuePutRequest. If requestResult is false - then the caller can not access the record. - If requestResult is true - then the record is locked and the caller can get and put data in the record. - When done the caller must call PVRecord::putDone, which will unlock the - record. -
    -

    class PVDatabase

     class PVDatabase : virtual public epics::pvData::Requester {
    @@ -804,6 +798,10 @@ public:
         PVRecordPtr findRecord(epics::pvData::String const& recordName);
         bool addRecord(PVRecordPtr const & record);
         bool removeRecord(PVRecordPtr const & record);
    +    virtual epics::pvData::String getRequesterName();
    +    virtual void message(
    +        epics::pvData::String const &message,
    +        epics::pvData::MessageType messageType);
     private:
         PVDatabase();
     };
    @@ -822,70 +820,21 @@ private:
        
    removeRecord
    Remove a record from the database. If the record was not in the database false is returned.
    +
    getRequesterName
    +
    Virtual method of Requester
    +
    message
    +
    Virtual message of Requester.
    -

    Local Channel Provider

    -

    Not yet described.

    +

    pvAccess

    +

    Not yet described. +It is only of interest to someone who wants to understand how it works. +

    A brief description is that it must implement the following components of pvIOCJava:

    pvCopy
    monitor
    -
    pvAccess
    -
    See the next section for a description
    +
    local ChannelProvider and Channel
    -

    Summary of Packages in pvIOCJAVA

    -

    The following are the direct sub packages of pvIOCJava/src/org/epics/pvioc:

    -
    -
    pvCopy
    -
    This provides a copy of an arbitrary subset of the fields in a PVRecord. - It also provides the ability to detect and report changes to fields. - It is required for pvAccess.
    -
    monitor
    -
    This provides the ability to monitor changes to a PVRecord. It is required for pvAccess monitors.
    -
    pvAccess
    -
    The local implementation of Channel Provider and Channel. - It is accessed by the remote pvAccess server and can also be accessed by code in the same IOC.
    -
    database
    -
    This defines and implements PVRecord, PVDatabase , and PVListener. - It supports the basic feature required the implement a local Channel Provider.
    -
    support
    -
    This provides the ability to optionally attach code to any field of a pvRecord. - It and several sub packages provide a set of standard support modules.
    -
    install
    -
    This provides the ability to dynamically initialize and add new PVRecords. It also provides - the ability to dynamicall delete PVRecords. -
    xml
    -
    This provides the ability to configure record instances without writing code.
    -
    util
    -
    This is misnamed since it is code related to scanning.
    -
    pdrv
    -
    This is portDriver, which is a proposed sucessor to the asynManager component of asynDriver.
    -
    swtshell
    -
    This is shell that is can either run under the same process as a JavaIOC or as a remote shell. - It is like a version of probe but for pvData/pvAccess. - Almost all of it's features work in either local or remote mode. - With a little more work all or it's features could work remotely. - This should be done and then only remote mode should be supported. - It can then be rewritten in a completely different language and using a complely different GUI - framework. -
    -
    caV3
    -
    This has two components: -
    -
    ClientFactory
    -
    This is a small wrapper on top of the caV3 client support implemented by pvAccess. - It allows code in the pvIOC to access V3Records via pvAccess.
    -
    ServerFactory
    -
    This is a caV3 server that allows a caV3 client to access a PVRecord. - The Java implementation uses CAJ, which does most of the work. - For now it will not be discussed in this document.
    -
    -
    -
    v3a
    -
    I do not know what this is.
    -
    -

    In addition there is one class file JavaIOC.java. -This is starting a IOC instance. -This is not required for pvIOCCPP which is either a main or runs as part of a V3 IOC.

    diff --git a/src/Makefile b/src/Makefile index 4f8faab..10c620f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,10 +10,15 @@ pvDatabase_LIBS += Com pvData pvAccess SRC_DIRS += $(DATABASE)/database INC += pvDatabase.h INC += powerSupplyRecordTest.h +INC += exampleCounter.h LIBSRCS += pvRecord.cpp +LIBSRCS += pvDatabase.cpp SRC_DIRS += $(DATABASE)/pvAccess +INC += channelProviderLocal.h INC += pvCopy.h +LIBSRCS += channelProviderLocal.cpp +LIBSRCS += channelLocal.cpp LIBSRCS += pvCopy.cpp include $(TOP)/configure/RULES diff --git a/src/database/exampleCounter.h b/src/database/exampleCounter.h new file mode 100644 index 0000000..df05f2f --- /dev/null +++ b/src/database/exampleCounter.h @@ -0,0 +1,74 @@ +/* exampleCounter.h */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvData is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ +/** + * @author mrk + * @date 2013.04.02 + */ +#ifndef EXAMPLECOUNTER_H +#define EXAMPLECOUNTER_H + +#include +#include + +namespace epics { namespace pvDatabase { + + +class ExampleCounter; +typedef std::tr1::shared_ptr ExampleCounterPtr; + +class ExampleCounter : + public PVRecord +{ +public: + POINTER_DEFINITIONS(ExampleCounter); + static ExampleCounterPtr create( + epics::pvData::String const & recordName); + virtual ~ExampleCounter(){} + virtual bool init(); + virtual void process(); +private: + ExampleCounter(epics::pvData::String const & recordName, + epics::pvData::PVStructurePtr const & pvStructure); + epics::pvData::PVLongPtr pvValue; +}; + +ExampleCounterPtr ExampleCounter::create( + epics::pvData::String const & recordName) +{ + epics::pvData::PVStructurePtr pvStructure = + epics::pvData::getStandardPVField()->scalar(epics::pvData::pvDouble,""); + ExampleCounterPtr pvRecord( + new ExampleCounter(recordName,pvStructure)); + if(!pvRecord->init()) pvRecord.reset(); + return pvRecord; +} + +ExampleCounter::ExampleCounter( + epics::pvData::String const & recordName, + epics::pvData::PVStructurePtr const & pvStructure) +: PVRecord(recordName,pvStructure) +{ +} + +bool ExampleCounter::init() +{ + + initPVRecord(); + epics::pvData::PVFieldPtr pvField; + pvValue = getPVStructure()->getLongField("value"); + if(pvValue.get()==NULL) return false; + return true; +} + +void ExampleCounter::process() +{ + pvValue->put(pvValue->get() + 1.0); +} + +}} + +#endif /* EXAMPLECOUNTER_H */ diff --git a/src/database/powerSupplyRecordTest.h b/src/database/powerSupplyRecordTest.h index 2d1e09d..71ef028 100644 --- a/src/database/powerSupplyRecordTest.h +++ b/src/database/powerSupplyRecordTest.h @@ -11,10 +11,7 @@ #ifndef POWERSUPPLYRECORDTEST_H #define POWERSUPPLYRECORDTEST_H -#include #include -#include -#include #include #include #include @@ -44,7 +41,6 @@ public: private: PowerSupplyRecordTest(epics::pvData::String const & recordName, epics::pvData::PVStructurePtr const & pvStructure); - epics::pvData::PVStructurePtr pvStructure; epics::pvData::PVDoublePtr pvCurrent; epics::pvData::PVDoublePtr pvPower; epics::pvData::PVDoublePtr pvVoltage; @@ -67,8 +63,7 @@ PowerSupplyRecordTestPtr PowerSupplyRecordTest::create( PowerSupplyRecordTest::PowerSupplyRecordTest( epics::pvData::String const & recordName, epics::pvData::PVStructurePtr const & pvStructure) -: PVRecord(recordName,pvStructure), - pvStructure(pvStructure) +: PVRecord(recordName,pvStructure) { } @@ -80,6 +75,7 @@ PowerSupplyRecordTest::~PowerSupplyRecordTest() bool PowerSupplyRecordTest::init() { initPVRecord(); + epics::pvData::PVStructurePtr pvStructure = getPVStructure(); epics::pvData::PVFieldPtr pvField; bool result; pvField = pvStructure->getSubField("timeStamp"); diff --git a/src/database/pvDatabase.cpp b/src/database/pvDatabase.cpp index 49f3b4f..f0853fc 100644 --- a/src/database/pvDatabase.cpp +++ b/src/database/pvDatabase.cpp @@ -14,6 +14,7 @@ using std::tr1::static_pointer_cast; using namespace epics::pvData; using namespace epics::pvAccess; +using namespace std; namespace epics { namespace pvDatabase { @@ -21,30 +22,51 @@ PVDatabase::~PVDatabase() {} PVDatabasePtr PVDatabase::getMaster() { - PVDatabasePtr xxx; - return xxx; + static PVDatabasePtr master; + static Mutex mutex; + Lock xx(mutex); + if(master.get()==NULL) { + master = PVDatabasePtr(new PVDatabase()); + } + return master; } +PVDatabase::PVDatabase() {} + PVRecordPtr PVDatabase::findRecord(String const& recordName) { + PVRecordMap::iterator iter = recordMap.find(recordName); + if(iter!=recordMap.end()) { + return (*iter).second; + } PVRecordPtr xxx; return xxx; } bool PVDatabase::addRecord(PVRecordPtr const & record) { - return false; + String recordName = record->getRecordName(); + PVRecordMap::iterator iter = recordMap.find(recordName); + if(iter!=recordMap.end()) return false; + recordMap.insert(PVRecordMap::value_type(recordName,record)); + return true; } bool PVDatabase::removeRecord(PVRecordPtr const & record) { - return false; + String recordName = record->getRecordName(); + PVRecordMap::iterator iter = recordMap.find(recordName); + if(iter!=recordMap.end()) { + recordMap.erase(iter); + return true; + } + return false; } String PVDatabase::getRequesterName() { - String xxx; - return xxx; + static String name("masterDatabase"); + return name; } void PVDatabase::message(String const & message,MessageType messageType) diff --git a/src/database/pvDatabase.h b/src/database/pvDatabase.h index 95e71b8..3fe38e5 100644 --- a/src/database/pvDatabase.h +++ b/src/database/pvDatabase.h @@ -12,6 +12,7 @@ #define PVDATABASE_H #include +#include #include #include @@ -21,6 +22,7 @@ namespace epics { namespace pvDatabase { class PVRecord; typedef std::tr1::shared_ptr PVRecordPtr; +typedef std::map PVRecordMap; class PVRecordField; typedef std::tr1::shared_ptr PVRecordFieldPtr; @@ -39,53 +41,201 @@ typedef std::tr1::shared_ptr PVListenerPtr; class PVDatabase; typedef std::tr1::shared_ptr PVDatabasePtr; +/** + * Base interface for a record. + * @author mrk + */ class PVRecord : public epics::pvData::Requester, public std::tr1::enable_shared_from_this { public: POINTER_DEFINITIONS(PVRecord); + + /** + * Virtual initialization method. + * Must be implemented by derived classes. + * This method Must call initPVRecord. + * @return true for success and false for failure. + */ + virtual bool init() {initPVRecord(); return true;} + /** + * Must be implemented by derived classes. + * It is the method that makes a record smart. + * If it encounters errors it should raise alarms and/or + * call the message method provided by the base class. + */ + virtual void process() {} + /** + * Creates a dump record, i.e. a record where process does nothing. + * @param recordName The name of the record, which is also the channelName. + * @param pvStructure The top level structure. + * @return A shared pointer to the newly created record. + */ static PVRecordPtr create( epics::pvData::String const & recordName, epics::pvData::PVStructurePtr const & pvStructure); + /** + * The constructor. + * @param recordName The name of the record, which is also the channelName. + * @param pvStructure The top level structure. + */ PVRecord( epics::pvData::String const & recordName, epics::pvData::PVStructurePtr const & pvStructure); + /** + * The Destructor. Must be virtual. + */ virtual ~PVRecord(); + /** + * Destroy the PVRecord. Release any resources used and + * get rid of listeners and requesters. + */ virtual void destroy(); + /** + * Get the name of the record. + * @return The name. + */ epics::pvData::String getRecordName(); + /** + * Get the top level PVStructure. + * @return The shared pointer. + */ PVRecordStructurePtr getPVRecordStructure(); + /** + * Find the PVRecordField for the PVField. + * @param pvField The PVField. + * @return The shared pointer to the PVRecordField. + */ PVRecordFieldPtr findPVRecordField( epics::pvData::PVFieldPtr const & pvField); + /** + * Add a requester to receive messages. + * @param requester The requester. + * @return true if requester was added. + */ bool addRequester(epics::pvData::RequesterPtr const & requester); + /** + * Remove a requester. + * @param requester The requester.` + * @return true if requester was removed. + */ bool removeRequester(epics::pvData::RequesterPtr const & requester); + /** + * This is an inline method that locks the record. + * The record will automatically + * be unlocked when control leaves the block that has the call. + */ inline void lock_guard() { epics::pvData::Lock theLock(mutex); } + /** + * Lock the record. + * Any code must lock while accessing a record. + */ void lock(); + /** + * Unlock the record. + */ void unlock(); + /** + * If true then just like lock. + * If falseclient can not access record. + * Code can try to simultaneously hold the lock for more than two records + * by calling this method but must be willing to accept failure. + * @return true if the record is locked. + */ bool tryLock(); + /** + * A client that holds the lock for one record can lock one other record. + * A client must not call this if the client already has the lock for + * more then one record. + * + * @param otherRecord The other record to lock. + */ void lockOtherRecord(PVRecordPtr const & otherRecord); + /** + * Every client that accesses the record must call this so that the + * client can be notified when the record is deleted. + * @param pvRecordClient The client. + * @return true if the client is added. + */ bool addPVRecordClient(PVRecordClientPtr const & pvRecordClient); + /** + * Remove a client. + * @param pvRecordClient The client. + * @return true if the client is removed. + */ bool removePVRecordClient(PVRecordClientPtr const & pvRecordClient); + /** + * remove all attached clients. + */ void detachClients(); + /** + * Add a PVListener. + * This must be called before calling pvRecordField.addListener. + * @param pvListener The listener. + * @return true if the listener was added. + */ bool addListener(PVListenerPtr const & pvListener); + /** + * Remove a listener. + * @param pvListener The listener. + * @return true if the listener was removed. + */ bool removeListener(PVListenerPtr const & pvListener); + /** + * Begins a group of puts. + */ void beginGroupPut(); + /** + * Ends a group of puts. + */ void endGroupPut(); + /** + * Virtual method of Requester + * @return the name of the requester. + */ epics::pvData::String getRequesterName() {return getRecordName();} + /** + * Can be called by implementation code. + * The message will be sent to every requester. + * @param message The message. + * @param messageType The severity of the message. + */ virtual void message( epics::pvData::String const & message, epics::pvData::MessageType messageType); + /** + * Called for a field. It will call the previous method + * after adding field name. + * @param pvRecordField The field for which the message is associated. + * @param message The message. + * @param messageType The severity of the message. + */ void message( PVRecordFieldPtr const & pvRecordField, epics::pvData::String const & message, epics::pvData::MessageType messageType); + /** + * Calls the next method with indentLevel = 0. + * @param buf String Builder. + */ void toString(epics::pvData::StringBuilder buf); + /** + * Dumps the data from the top level PVStructure. + * @param buf String Builder. + * @param indentLevel The indentation level. + */ void toString(epics::pvData::StringBuilder buf,int indentLevel); - //init MUST be called by derived class after derived class is constructed - virtual bool init() {initPVRecord(); return true;} - virtual void process() {} protected: + /** + * Initializes the base class. Must be called by derived classes. + */ void initPVRecord(); + /** + * Convience method for derived classes. + * @return The shared pointer to the top level PVStructure. + */ + epics::pvData::PVStructurePtr getPVStructure(); PVRecordPtr getPtrSelf() { return shared_from_this(); @@ -107,25 +257,82 @@ private: bool isDestroyed; }; +/** + * Interface for a field of a record. + * One exists for each field of the top level PVStructure. + * @author mrk + */ class PVRecordField : public virtual epics::pvData::PostHandler, public std::tr1::enable_shared_from_this { public: POINTER_DEFINITIONS(PVRecordField); + /** + * Constructor. + * @param pvField The field from the top level structure. + * @param The parent. + */ PVRecordField( epics::pvData::PVFieldPtr const & pvField, PVRecordStructurePtr const &parent, PVRecordPtr const & pvRecord); + /** + * Destructor. + */ virtual ~PVRecordField(); + /** + * Get the parent. + * @return The parent. + */ PVRecordStructurePtr getParent(); + /** + * Get the PVField. + * @return The shared pointer. + */ epics::pvData::PVFieldPtr getPVField(); + /** + * Get the full name of the field, i.e. field,field,.. + * @return The full name. + */ epics::pvData::String getFullFieldName(); + /** + * Get the recordName plus the full name of the field, i.e. recordName.field,field,.. + * @return The name. + */ epics::pvData::String getFullName(); + /** + * Returns the PVRecord to which this field belongs. + * @return The shared pointer, + */ PVRecordPtr getPVRecord(); + /** + * Add A PVListener to this field. + * Whenever this field or any subfield if this field is modified the listener will be notified. + * PVListener is described below. + * Before a listener can call addListener it must first call PVRecord.registerListener. + * @param pvListener The listener. + * @return if listener is added. + */ bool addListener(PVListenerPtr const & pvListener); + /** + * Remove a listener. + * @param pvListener The listener. + * @return if listener is removed. + */ virtual void removeListener(PVListenerPtr const & pvListener); + /** + * This is called by the code that implements the data interface. + * It is called whenever the put method is called. + */ virtual void postPut(); + /** + * Called by implementation code. + * It calls PVRecord::message after prepending the full fieldname. + * @param message The message, + * @param messageType The message severity. + * @return + */ virtual void message( epics::pvData::String const & message, epics::pvData::MessageType messageType); @@ -148,19 +355,51 @@ private: friend class PVRecord; }; +/** + * Interface for a field that is a structure. + * One exists for each structure field of the top level PVStructure. + * @author mrk + */ class PVRecordStructure : public PVRecordField { public: POINTER_DEFINITIONS(PVRecordStructure); + /** + * Constructor. + * @param pvStructure The data. + * @param parent The parent + * @param pvRecord The record that has this field. + */ PVRecordStructure( epics::pvData::PVStructurePtr const &pvStructure, PVRecordStructurePtr const &parent, PVRecordPtr const & pvRecord); + /** + * Destructor. + */ virtual ~PVRecordStructure(); + /** + * Get the sub fields. + * @return the array of PVRecordFieldPtr. + */ PVRecordFieldPtrArrayPtr getPVRecordFields(); + /** + * Get the data structure/ + * @return The shared pointer. + */ epics::pvData::PVStructurePtr getPVStructure(); + /** + * Called by PVRecord::removeListener. + * @param pvListener The listener. + */ virtual void removeListener(PVListenerPtr const & pvListener); + /** + * Called by implementation code of PVRecord. + */ virtual void postPut(); protected: + /** + * Called by implementation code of PVRecord. + */ virtual void init(); private: epics::pvData::PVStructurePtr pvStructure; @@ -168,41 +407,116 @@ private: friend class PVRecord; }; +/** + * An interface that must be implemented by any code that accesses the record. + * @author mrk + */ class PVRecordClient { public: POINTER_DEFINITIONS(PVRecordClient); + /** + * Destructor. + */ virtual ~PVRecordClient() {} + /** + * Detach from the record because it is being removed. + * @param pvRecord The record. + */ virtual void detach(PVRecordPtr const & pvRecord) = 0; }; +/** + * An interface that is implemented by code that traps calls to PVRecord::message. + * @author mrk + */ class PVListener : virtual public PVRecordClient { public: POINTER_DEFINITIONS(PVListener); + /** + * Destructor. + */ virtual ~PVListener() {} + /** + * pvField has been modified. + * This is called if the listener has called PVRecordField::addListener for pvRecordField. + * @param pvRecordField The modified field. + */ virtual void dataPut(PVRecordFieldPtr const & pvRecordField) = 0; + /** + * A subfield has been modified. + * @param requested The structure that was requested. + * @param pvRecordField The field that was modified. + */ virtual void dataPut( PVRecordStructurePtr const & requested, PVRecordFieldPtr const & pvRecordField) = 0; + /** + * Begin a set of puts. + * @param pvRecord The record. + */ virtual void beginGroupPut(PVRecordPtr const & pvRecord) = 0; + /** + * End a set of puts. + * @param pvRecord The record. + */ virtual void endGroupPut(PVRecordPtr const & pvRecord) = 0; }; +/** + * The interface to a database of PVRecords. + * @author mrk + */ class PVDatabase : virtual public epics::pvData::Requester { public: POINTER_DEFINITIONS(PVDatabase); + /** + * Get the master database. + * @return The shared pointer. + */ static PVDatabasePtr getMaster(); + /** + * Destructor + */ virtual ~PVDatabase(); + /** + * Find a record. + * An empty pointer is returned if the record is not in the database. + * @param recordName The record to find. + * @return The shared pointer. + */ PVRecordPtr findRecord(epics::pvData::String const& recordName); + /** + * Add a record. + * @param The record to add. + * @return true if record was added. + */ bool addRecord(PVRecordPtr const & record); + /** + * Remove a record. + * @param The record to remove. + * @return true if record was removed. + */ bool removeRecord(PVRecordPtr const & record); + /** + * Virtual method of Requester. + * @return The name. + */ virtual epics::pvData::String getRequesterName(); + /** + * Virtual method of Requester. + * @param message The message. + * @param messageType The message severity. + * @return + */ virtual void message( epics::pvData::String const & message, epics::pvData::MessageType messageType); private: PVDatabase(); + PVRecordMap recordMap; + }; }} diff --git a/src/database/pvRecord.cpp b/src/database/pvRecord.cpp index ef32519..9c31eed 100644 --- a/src/database/pvRecord.cpp +++ b/src/database/pvRecord.cpp @@ -92,6 +92,8 @@ String PVRecord::getRecordName() {return recordName;} PVRecordStructurePtr PVRecord::getPVRecordStructure() {return pvRecordStructure;} +PVStructurePtr PVRecord::getPVStructure() {return pvStructure;} + PVRecordFieldPtr PVRecord::findPVRecordField(PVFieldPtr const & pvField) { return findPVRecordField(pvRecordStructure,pvField); @@ -488,42 +490,4 @@ void PVRecordStructure::postPut() } } -PVDatabase::PVDatabase() -{ -} - -PVDatabase::~PVDatabase() {} - -PVDatabasePtr PVDatabase::getMaster() -{ - PVDatabasePtr xxx; - return xxx; -} - -PVRecordPtr PVDatabase::findRecord(String const& recordName) -{ - PVRecordPtr xxx; - return xxx; -} - -bool PVDatabase::addRecord(PVRecordPtr const & record) -{ - return false; -} - -bool PVDatabase::removeRecord(PVRecordPtr const & record) -{ - return false; -} - -String PVDatabase::getRequesterName() -{ - String xxx; - return xxx; -} - -void PVDatabase::message(String const & message,MessageType messageType) -{ -} - }} diff --git a/src/pvAccess/channelLocal.cpp b/src/pvAccess/channelLocal.cpp new file mode 100644 index 0000000..a9471bd --- /dev/null +++ b/src/pvAccess/channelLocal.cpp @@ -0,0 +1,687 @@ +/* channelLocal.cpp */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvData is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ +/** + * @author mrk + */ + +#include + +namespace epics { namespace pvDatabase { +using namespace epics::pvData; +using namespace epics::pvAccess; +using std::tr1::static_pointer_cast; +using std::tr1::dynamic_pointer_cast; + +class ChannelProcessLocal; +typedef std::tr1::shared_ptr ChannelProcessLocalPtr; +class ChannelGetLocal; +typedef std::tr1::shared_ptr ChannelGetLocalPtr; +class ChannelPutLocal; +typedef std::tr1::shared_ptr ChannelPutLocalPtr; +class ChannelPutGetLocal; +typedef std::tr1::shared_ptr ChannelPutGetLocalPtr; +class ChannelMonitorLocal; +typedef std::tr1::shared_ptr ChannelMonitorLocalPtr; +class ChannelRPCLocal; +typedef std::tr1::shared_ptr ChannelRPCLocalPtr; +class ChannelArrayLocal; +typedef std::tr1::shared_ptr ChannelArrayLocalPtr; + +class ChannelLocalDebug { +public: + static void setLevel(int level); + static int getLevel(); +}; + +static bool getProcess(PVStructurePtr pvRequest,bool processDefault) +{ + PVFieldPtr pvField = pvRequest->getSubField("record._options.process"); + if(pvField.get()==NULL || pvField->getField()->getType()!=scalar) { + return processDefault; + } + ScalarConstPtr scalar = static_pointer_cast( + pvField->getField()); + if(scalar->getScalarType()==pvString) { + PVStringPtr pvString = static_pointer_cast(pvField); + return pvString->get().compare("true")==0 ? true : false; + } else if(scalar->getScalarType()==pvBoolean) { + PVBooleanPtr pvBoolean = static_pointer_cast(pvField); + return pvBoolean.get(); + } + return processDefault; +} + +/* +class ChannelRequestLocal : + public ChannelRequest +{ +public: + POINTER_DEFINITIONS(ChannelRequestLocal) + ChannelRequestLocal() :thelock(mutex) {} + virtual ~ChannelRequestLocal() {} + virtual void destroy(){} + virtual void lock() {thelock.lock();} + virtual void unlock() {thelock.unlock();} +private: + Mutex mutex; + Lock thelock; +}; +*/ + + +class ChannelProcessLocal : + public ChannelProcess +{ +public: + POINTER_DEFINITIONS(ChannelProcessLocal); + virtual ~ChannelProcessLocal(); + static ChannelProcess::shared_pointer create( + ChannelProviderLocalPtr const &channelProvider, + ChannelProcess::shared_pointer const & channelProcessRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual void process(bool lastRequest); +}; + +class ChannelGetLocal : + public ChannelGet, + public std::tr1::enable_shared_from_this +{ +public: + POINTER_DEFINITIONS(ChannelGetLocal); + virtual ~ChannelGetLocal(){} + static ChannelGetLocalPtr create( + ChannelLocalPtr const &channelLocal, + ChannelGetRequester::shared_pointer const & channelGetRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual void get(bool lastRequest); + virtual void destroy(); + virtual void lock() {thelock.lock();} + virtual void unlock() {thelock.unlock();} +private: + shared_pointer getPtrSelf() + { + return shared_from_this(); + } + ChannelGetLocal( + bool callProcess, + ChannelLocalPtr const &channelLocal, + ChannelGetRequester::shared_pointer const & channelGetRequester, + PVCopyPtr const &pvCopy, + PVStructurePtr const&pvStructure, + BitSetPtr const & bitSet, + PVRecordPtr const &pvRecord) + : + firstTime(true), + isDestroyed(false), + callProcess(callProcess), + channelLocal(channelLocal), + channelGetRequester(channelGetRequester), + pvCopy(pvCopy), + pvStructure(pvStructure), + bitSet(bitSet), + pvRecord(pvRecord), + thelock(mutex) + { + thelock.unlock(); + } + bool firstTime; + bool isDestroyed; + bool callProcess; + ChannelLocalPtr channelLocal; + ChannelGetRequester::shared_pointer channelGetRequester,; + PVCopyPtr pvCopy; + PVStructurePtr pvStructure; + BitSetPtr bitSet; + PVRecordPtr pvRecord; + Mutex mutex; + Lock thelock; +}; + +ChannelGetLocalPtr ChannelGetLocal::create( + ChannelLocalPtr const &channelLocal, + ChannelGetRequester::shared_pointer const & channelGetRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord) +{ + PVCopyPtr pvCopy = PVCopy::create( + pvRecord, + pvRequest, + "field"); + if(pvCopy.get()==NULL) { + Status status( + Status::Status::STATUSTYPE_ERROR, + "invalid pvRequest"); + ChannelGet::shared_pointer channelGet; + PVStructurePtr pvStructure; + BitSetPtr bitSet; + channelGetRequester->channelGetConnect( + status, + channelGet, + pvStructure, + bitSet); + ChannelGetLocalPtr localGet; + return localGet; + } + PVStructurePtr pvStructure = pvCopy->createPVStructure(); + BitSetPtr bitSet(new BitSet(pvStructure->getNumberFields())); + ChannelGetLocalPtr get(new ChannelGetLocal( + getProcess(pvRequest,false), + channelLocal, + channelGetRequester, + pvCopy, + pvStructure, + bitSet, + pvRecord)); + channelLocal->addChannelGet(get); + channelGetRequester->channelGetConnect(Status::Ok, get, pvStructure,bitSet); + return get; +} + + +void ChannelGetLocal::destroy() +{ + if(isDestroyed) return; + isDestroyed = true; + channelLocal->removeChannelGet(getPtrSelf()); + channelLocal.reset(); + channelGetRequester.reset(); + pvCopy.reset(); + pvStructure.reset(); + bitSet.reset(); + pvRecord.reset(); +} + +void ChannelGetLocal::get(bool lastRequest) +{ + if(callProcess) pvRecord->process(); + if(isDestroyed) { + Status status( + Status::Status::STATUSTYPE_ERROR, + "was destroyed"); + channelGetRequester->getDone(status); + } + pvCopy->updateCopySetBitSet(pvStructure, bitSet, false); + if(firstTime) { + bitSet->clear(); + bitSet->set(0); + firstTime = false; + } + channelGetRequester->getDone(Status::Ok); + if(lastRequest) destroy(); +} + + +class ChannelLocalPut : + public ChannelPut +{ +public: + POINTER_DEFINITIONS(ChannelLocalPut); + virtual ~ChannelLocalPut(); + static ChannelPut::shared_pointer create( + ChannelProviderLocalPtr const &channelProvider, + ChannelPut::shared_pointer const & channelPutRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual void put(bool lastRequest); + virtual void get(bool lastRequest); +}; + +class ChannelPutGetLocal : + public ChannelPutGet +{ +public: + POINTER_DEFINITIONS(ChannelPutGetLocal); + virtual ~ChannelPutGetLocal(); + static ChannelPutGet::shared_pointer create( + ChannelProviderLocalPtr const &channelProvider, + ChannelPutGet::shared_pointer const & channelPutGetRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual void putGet(bool lastRequest); + virtual void getPut(); + virtual void getGet(); +}; + +class ChannelMonitorLocal : + public Monitor +{ +public: + POINTER_DEFINITIONS(ChannelMonitorLocal); + virtual ~ChannelMonitorLocal(); + static Monitor::shared_pointer create( + ChannelProviderLocalPtr const &channelProvider, + MonitorRequester::shared_pointer const & channelMonitorRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual Status start(); + virtual Status stop(); + virtual MonitorElementPtr poll(); + virtual void release(MonitorElementPtr const & monitorElement); +}; + +class ChannelRPCLocal : + public ChannelRPC +{ +public: + POINTER_DEFINITIONS(ChannelRPCLocal); + virtual ~ChannelRPCLocal(); + static ChannelRPC::shared_pointer create( + ChannelProviderLocalPtr const &channelProvider, + ChannelRPC::shared_pointer const & channelRPCRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual void request( + PVStructurePtr const & pvArgument, + bool lastRequest); +}; + +class ChannelArrayLocal : + public ChannelArray +{ +public: + POINTER_DEFINITIONS(ChannelArrayLocal); + virtual ~ChannelArrayLocal(); + static ChannelArray::shared_pointer create( + ChannelProviderLocalPtr const &channelProvider, + ChannelArray::shared_pointer const & channelArrayRequester, + PVStructurePtr const & pvRequest, + PVRecordPtr const &pvRecord); + virtual void putArray(bool lastRequest, int offset, int count); + virtual void getArray(bool lastRequest, int offset, int count); + virtual void setLength(bool lastRequest, int length, int capacity); +}; + +int ChannelLocalDebugLevel = 0; +void ChannelLocalDebug::setLevel(int level) {ChannelLocalDebugLevel = level;} +int ChannelLocalDebug::getLevel() {return ChannelLocalDebugLevel;} + + + +ChannelLocal::ChannelLocal( + ChannelProviderLocalPtr const & provider, + ChannelRequester::shared_pointer const & requester, + PVRecordPtr const & pvRecord) +: provider(provider), + requester(requester), + pvRecord(pvRecord), + beingDestroyed(false) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::ChannelLocal\n"); + } +} + +ChannelLocal::~ChannelLocal() +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::~ChannelLocal\n"); + } +} + +void ChannelLocal::destroy() +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::destroy beingDestroyed %s\n", + (beingDestroyed ? "true" : "false")); + } + { + Lock xx(mutex); + if(beingDestroyed) return; + beingDestroyed = true; + } + while(true) { + std::set::iterator it; + it = channelProcessList.begin(); + if(it==channelProcessList.end()) break; + it->get()->destroy(); + channelProcessList.erase(it); + } + while(true) { + std::set::iterator it; + it = channelGetList.begin(); + if(it==channelGetList.end()) break; + it->get()->destroy(); + channelGetList.erase(it); + } + while(true) { + std::set::iterator it; + it = channelPutList.begin(); + if(it==channelPutList.end()) break; + it->get()->destroy(); + channelPutList.erase(it); + } + while(true) { + std::set::iterator it; + it = channelPutGetList.begin(); + if(it==channelPutGetList.end()) break; + it->get()->destroy(); + channelPutGetList.erase(it); + } + while(true) { + std::set::iterator it; + it = channelMonitorList.begin(); + if(it==channelMonitorList.end()) break; + it->get()->destroy(); + channelMonitorList.erase(it); + } + while(true) { + std::set::iterator it; + it = channelRPCList.begin(); + if(it==channelRPCList.end()) break; + it->get()->destroy(); + channelRPCList.erase(it); + } + while(true) { + std::set::iterator it; + it = channelArrayList.begin(); + if(it==channelArrayList.end()) break; + it->get()->destroy(); + channelArrayList.erase(it); + } + provider->removeChannel(getPtrSelf()); +} + + +void ChannelLocal::addChannelProcess(ChannelProcess::shared_pointer const & channelProcess) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelProcess\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelProcessList.insert(channelProcess); +} + +void ChannelLocal::addChannelGet(ChannelGet::shared_pointer const &channelGet) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelGet\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelGetList.insert(channelGet); +} + +void ChannelLocal::addChannelPut(ChannelPut::shared_pointer const &channelPut) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelPut\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelPutList.insert(channelPut); +} + +void ChannelLocal::addChannelPutGet(ChannelPutGet::shared_pointer const &channelPutGet) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelPutGet\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelPutGetList.insert(channelPutGet); +} + +void ChannelLocal::addChannelMonitor(Monitor::shared_pointer const &monitor) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelMonitor\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelMonitorList.insert(monitor); +} + +void ChannelLocal::addChannelRPC(ChannelRPC::shared_pointer const &channelRPC) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelRPC\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelRPCList.insert(channelRPC); +} + +void ChannelLocal::addChannelArray(ChannelArray::shared_pointer const &channelArray) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::addChannelArray\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelArrayList.insert(channelArray); +} + +void ChannelLocal::removeChannelProcess(ChannelProcess::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelProcess\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelProcessList.erase(ref); +} + +void ChannelLocal::removeChannelGet(ChannelGet::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelGet\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelGetList.erase(ref); +} + +void ChannelLocal::removeChannelPut(ChannelPut::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelPut\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelPutList.erase(ref); +} + +void ChannelLocal::removeChannelPutGet(ChannelPutGet::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelPutGet\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelPutGetList.erase(ref); +} + +void ChannelLocal::removeChannelMonitor(Monitor::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelMonitor\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelMonitorList.erase(ref); +} + +void ChannelLocal::removeChannelRPC(ChannelRPC::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelRPC\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelRPCList.erase(ref); +} + +void ChannelLocal::removeChannelArray(ChannelArray::shared_pointer const &ref) +{ + if(ChannelLocalDebug::getLevel()>0) { + printf("ChannelLocal::removeChannelArray\n"); + } + Lock xx(mutex); + if(beingDestroyed) return; + channelArrayList.erase(ref); +} + +String ChannelLocal::getRequesterName() +{ + return requester->getRequesterName(); +} + +void ChannelLocal::message( + String const &message, + MessageType messageType) +{ + requester->message(message,messageType); +} + +ChannelProvider::shared_pointer ChannelLocal::getProvider() +{ + return provider; +} + +String ChannelLocal::getRemoteAddress() +{ + return String("local"); +} + +Channel::ConnectionState ChannelLocal::getConnectionState() +{ + return Channel::CONNECTED; +} + +String ChannelLocal::getChannelName() +{ + return pvRecord->getRecordName(); +} + +ChannelRequester::shared_pointer ChannelLocal::getChannelRequester() +{ + return requester; +} + +bool ChannelLocal::isConnected() +{ + return true; +} + +void ChannelLocal::getField(GetFieldRequester::shared_pointer const &requester, + String const &subField) +{ + Status status(Status::STATUSTYPE_ERROR, + String("client asked for illegal field")); + requester->getDone(status,FieldConstPtr()); +} + +AccessRights ChannelLocal::getAccessRights( + PVField::shared_pointer const &pvField) +{ + throw std::logic_error(String("Not Implemented")); +} + +ChannelProcess::shared_pointer ChannelLocal::createChannelProcess( + ChannelProcessRequester::shared_pointer const & channelProcessRequester, + PVStructure::shared_pointer const & pvRequest) +{ + Status status(Status::STATUSTYPE_ERROR, + String("ChannelProcess not supported")); + channelProcessRequester->channelProcessConnect( + status, + ChannelProcess::shared_pointer()); + return ChannelProcess::shared_pointer(); +} + +ChannelGet::shared_pointer ChannelLocal::createChannelGet( + ChannelGetRequester::shared_pointer const &channelGetRequester, + PVStructure::shared_pointer const &pvRequest) +{ + ChannelGetLocalPtr channelGet = + ChannelGetLocal::create( + getPtrSelf(), + channelGetRequester, + pvRequest, + pvRecord); + return channelGet; +} + +ChannelPut::shared_pointer ChannelLocal::createChannelPut( + ChannelPutRequester::shared_pointer const &channelPutRequester, + PVStructure::shared_pointer const &pvRequest) +{ + Status status(Status::STATUSTYPE_ERROR, + String("ChannelPut not supported")); + channelPutRequester->channelPutConnect( + status, + ChannelPut::shared_pointer(), + PVStructure::shared_pointer(), + BitSet::shared_pointer()); + return ChannelPut::shared_pointer(); +} + +ChannelPutGet::shared_pointer ChannelLocal::createChannelPutGet( + ChannelPutGetRequester::shared_pointer const &channelPutGetRequester, + PVStructure::shared_pointer const &pvRequest) +{ + Status status(Status::STATUSTYPE_ERROR, + String("ChannelPutGet not supported")); + channelPutGetRequester->channelPutGetConnect( + status, + ChannelPutGet::shared_pointer(), + PVStructure::shared_pointer(), + PVStructure::shared_pointer()); + return ChannelPutGet::shared_pointer(); +} + +ChannelRPC::shared_pointer ChannelLocal::createChannelRPC( + ChannelRPCRequester::shared_pointer const & channelRPCRequester, + PVStructure::shared_pointer const & pvRequest) +{ + Status status(Status::STATUSTYPE_ERROR, + String("ChannelRPC not supported")); + channelRPCRequester->channelRPCConnect(status,ChannelRPC::shared_pointer()); + return ChannelRPC::shared_pointer(); +} + +Monitor::shared_pointer ChannelLocal::createMonitor( + MonitorRequester::shared_pointer const &monitorRequester, + PVStructure::shared_pointer const &pvRequest) +{ + Status status(Status::STATUSTYPE_ERROR, + String("ChannelMonitor not supported")); + Monitor::shared_pointer thisPointer = dynamic_pointer_cast(getPtrSelf()); + monitorRequester->monitorConnect( + status, + thisPointer, + StructureConstPtr()); + return Monitor::shared_pointer(); +} + +ChannelArray::shared_pointer ChannelLocal::createChannelArray( + ChannelArrayRequester::shared_pointer const &channelArrayRequester, + PVStructure::shared_pointer const &pvRequest) +{ + Status status(Status::STATUSTYPE_ERROR, + String("ChannelArray not supported")); + channelArrayRequester->channelArrayConnect( + status, + ChannelArray::shared_pointer(), + PVArray::shared_pointer()); + return ChannelArray::shared_pointer(); +} + +void ChannelLocal::printInfo() +{ + printf("ChannelLocal provides access to service\n"); +} + +void ChannelLocal::printInfo(StringBuilder out) +{ + *out += "ChannelLocal provides access to service"; +} + +}} diff --git a/src/pvAccess/channelProviderLocal.cpp b/src/pvAccess/channelProviderLocal.cpp new file mode 100644 index 0000000..4b9cffb --- /dev/null +++ b/src/pvAccess/channelProviderLocal.cpp @@ -0,0 +1,222 @@ +/* channelChannelProviderLocal.cpp */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvData is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ +/** + * @author mrk + */ + +#include +#include + +namespace epics { namespace pvDatabase { + +using namespace epics::pvData; +using namespace epics::pvAccess; +using namespace std; + +static String providerName("local"); + +class LocalChannelCTX; +typedef std::tr1::shared_ptr LocalChannelCTXPtr; +class LocalChannelCTX : + public epics::pvData::Runnable, + public std::tr1::enable_shared_from_this +{ +public: + POINTER_DEFINITIONS(LocalChannelCTX); + static LocalChannelCTXPtr create( + ChannelProviderLocalPtr const &channelProvider); + ChannelProviderLocalPtr getChannelProviderLocal() {return channelProvider;} + virtual ~LocalChannelCTX(); + virtual void run(); +private: + LocalChannelCTX( + ChannelProviderLocalPtr const &channelProvider); + shared_pointer getPtrSelf() + { + return shared_from_this(); + } + epics::pvData::Event event; + ChannelProviderLocalPtr channelProvider; + epics::pvAccess::ServerContextImpl::shared_pointer ctx; + epics::pvData::Thread *thread; +}; + +LocalChannelCTXPtr LocalChannelCTX::create( + ChannelProviderLocalPtr const & channelProvider) +{ + static LocalChannelCTXPtr pvServiceChannelCTX; + static Mutex mutex; + Lock xx(mutex); + + if(pvServiceChannelCTX.get()==0) { + pvServiceChannelCTX = LocalChannelCTXPtr( + new LocalChannelCTX(channelProvider)); + } + return pvServiceChannelCTX; +} + +LocalChannelCTX::LocalChannelCTX( + ChannelProviderLocalPtr const &channelProvider) +: + channelProvider(channelProvider), + ctx(ServerContextImpl::create()), + thread(new Thread( + String("pvServiceChannel"), + lowerPriority, + this, + epicsThreadStackBig)) +{} + +LocalChannelCTX::~LocalChannelCTX() +{ + ctx->shutdown(); + // we need thead.waitForCompletion() + event.wait(); + epicsThreadSleep(1.0); + delete thread; +} +void LocalChannelCTX::run() +{ + registerChannelProvider(channelProvider); + String providerName = channelProvider->getProviderName(); + ctx->setChannelProviderName(providerName); + ctx->initialize(getChannelAccess()); + ctx->printInfo(); + ctx->run(0); + ctx->destroy(); + event.signal(); +} + + +ChannelProviderLocalPtr ChannelProviderLocal::create() +{ + ChannelProviderLocalPtr channelProvider(new ChannelProviderLocal()); + LocalChannelCTX::create(channelProvider); + return channelProvider; +} + +ChannelProviderLocal::ChannelProviderLocal() +: pvDatabase(PVDatabase::getMaster()), + beingDestroyed(false) +{ +} + +ChannelProviderLocal::~ChannelProviderLocal() +{ + pvDatabase.reset(); +} + +void ChannelProviderLocal::destroy() +{ + Lock xx(mutex); + if(beingDestroyed) return; + unregisterChannelProvider(getPtrSelf()); + beingDestroyed = true; + ChannelLocalList::iterator iter; + for(iter = channelList.begin(); iter!=channelList.end(); ++iter) { + ChannelLocalPtr channel = *iter; + channel->destroy(); + } + channelList.clear(); +} + +String ChannelProviderLocal::getProviderName() +{ + return providerName; +} + +ChannelFind::shared_pointer ChannelProviderLocal::channelFind( + String const & channelName, + ChannelFindRequester::shared_pointer const &channelFindRequester) +{ + Lock xx(mutex); + bool found = false; + ChannelLocalList::iterator iter; + for(iter = channelList.begin(); iter!=channelList.end(); ++iter) + { + if((*iter)->getChannelName().compare(channelName)==0) { + found = true; + break; + } + } + if(found) { + channelFindRequester->channelFindResult( + Status::Ok, + ChannelFind::shared_pointer(), + true); + } + PVRecordPtr pvRecord = pvDatabase->findRecord(channelName); + if(pvRecord.get()!=NULL) { + channelFindRequester->channelFindResult( + Status::Ok, + ChannelFind::shared_pointer(), + true); + + } else { + Status notFoundStatus(Status::STATUSTYPE_ERROR,String("pv not found")); + channelFindRequester->channelFindResult( + notFoundStatus, + ChannelFind::shared_pointer(), + false); + } + return ChannelFind::shared_pointer(); +} + +Channel::shared_pointer ChannelProviderLocal::createChannel( + String const & channelName, + ChannelRequester::shared_pointer const &channelRequester, + short priority) +{ + return createChannel(channelName,channelRequester,priority,""); +} + +Channel::shared_pointer ChannelProviderLocal::createChannel( + String const & channelName, + ChannelRequester::shared_pointer const &channelRequester, + short priority, + String const &address) +{ + Lock xx(mutex); + ChannelLocalList::iterator iter; + for(iter = channelList.begin(); iter!=channelList.end(); ++iter) + { + if((*iter)->getChannelName().compare(channelName)==0) { + return *iter; + } + } + PVRecordPtr pvRecord = pvDatabase->findRecord(channelName); + if(pvRecord.get()!=NULL) { + Channel::shared_pointer channel(new ChannelLocal( + getPtrSelf(),channelRequester,pvRecord)); + channelRequester->channelCreated( + Status::Ok, + channel); + return channel; + } + Status notFoundStatus(Status::STATUSTYPE_ERROR,String("pv not found")); + channelRequester->channelCreated( + notFoundStatus, + Channel::shared_pointer()); + return Channel::shared_pointer(); +} + +void ChannelProviderLocal::removeChannel( + Channel::shared_pointer const & channel) +{ + Lock xx(mutex); + if(beingDestroyed) return; + ChannelLocalList::iterator iter; + for(iter = channelList.begin(); iter!=channelList.end(); ++iter) + { + if((*iter).get()==channel.get()) { + channelList.erase(iter); + return; + } + } +} + +}} diff --git a/src/pvAccess/channelProviderLocal.h b/src/pvAccess/channelProviderLocal.h new file mode 100644 index 0000000..1ce8e85 --- /dev/null +++ b/src/pvAccess/channelProviderLocal.h @@ -0,0 +1,164 @@ +/* channelProviderLocal.h */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvData is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ +/** + * @author mrk + */ +#ifndef CHANNELPROVIDERLOCAL_H +#define CHANNELPROVIDERLOCAL_H +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + + +namespace epics { namespace pvDatabase { + + +class ChannelProviderLocal; +typedef std::tr1::shared_ptr ChannelProviderLocalPtr; +class ChannelLocal; +typedef std::tr1::shared_ptr ChannelLocalPtr; +typedef std::set ChannelLocalList; + +class ChannelProviderLocal : + public epics::pvAccess::ChannelProvider, + public std::tr1::enable_shared_from_this +{ +public: + POINTER_DEFINITIONS(ChannelProviderLocal); + + static ChannelProviderLocalPtr create(); + virtual ~ChannelProviderLocal(); + virtual void destroy(); + virtual epics::pvData::String getProviderName(); + virtual epics::pvAccess::ChannelFind::shared_pointer channelFind( + epics::pvData::String const &channelName, + epics::pvAccess::ChannelFindRequester::shared_pointer const & channelFindRequester); + virtual epics::pvAccess::Channel::shared_pointer createChannel( + epics::pvData::String const &channelName, + epics::pvAccess::ChannelRequester::shared_pointer const &channelRequester, + short priority); + virtual epics::pvAccess::Channel::shared_pointer createChannel( + epics::pvData::String const &channelName, + epics::pvAccess::ChannelRequester::shared_pointer const &channelRequester, + short priority, + epics::pvData::String const &address); + void removeChannel( + epics::pvAccess::Channel::shared_pointer const &channel); +private: + shared_pointer getPtrSelf() + { + return shared_from_this(); + } + ChannelProviderLocal(); + + PVDatabasePtr pvDatabase; + ChannelLocalList channelList; + epics::pvData::Mutex mutex; + bool beingDestroyed; + friend class ChannelProviderLocalRun; +}; + +class ChannelLocal : + public epics::pvAccess::Channel, + public std::tr1::enable_shared_from_this +{ +public: + POINTER_DEFINITIONS(ChannelLocal); + ChannelLocal( + ChannelProviderLocalPtr const &channelProvider, + epics::pvAccess::ChannelRequester::shared_pointer const & requester, + PVRecordPtr const & pvRecord + ); + virtual ~ChannelLocal(); + virtual void destroy(); + virtual epics::pvData::String getRequesterName(); + virtual void message( + epics::pvData::String const & message, + epics::pvData::MessageType messageType); + virtual epics::pvAccess::ChannelProvider::shared_pointer getProvider(); + virtual epics::pvData::String getRemoteAddress(); + virtual epics::pvAccess::Channel::ConnectionState getConnectionState(); + virtual epics::pvData::String getChannelName(); + virtual epics::pvAccess::ChannelRequester::shared_pointer getChannelRequester(); + virtual bool isConnected(); + virtual void getField( + epics::pvAccess::GetFieldRequester::shared_pointer const &requester, + epics::pvData::String const & subField); + virtual epics::pvAccess::AccessRights getAccessRights( + epics::pvData::PVField::shared_pointer const &pvField); + virtual epics::pvAccess::ChannelProcess::shared_pointer createChannelProcess( + epics::pvAccess::ChannelProcessRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual epics::pvAccess::ChannelGet::shared_pointer createChannelGet( + epics::pvAccess::ChannelGetRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual epics::pvAccess::ChannelPut::shared_pointer createChannelPut( + epics::pvAccess::ChannelPutRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual epics::pvAccess::ChannelPutGet::shared_pointer createChannelPutGet( + epics::pvAccess::ChannelPutGetRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual epics::pvAccess::ChannelRPC::shared_pointer createChannelRPC( + epics::pvAccess::ChannelRPCRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual epics::pvData::Monitor::shared_pointer createMonitor( + epics::pvData::MonitorRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual epics::pvAccess::ChannelArray::shared_pointer createChannelArray( + epics::pvAccess::ChannelArrayRequester::shared_pointer const &requester, + epics::pvData::PVStructurePtr const &pvRequest); + virtual void printInfo(); + virtual void printInfo(epics::pvData::StringBuilder out); + // following called by derived classes + void addChannelProcess(epics::pvAccess::ChannelProcess::shared_pointer const &); + void addChannelGet(epics::pvAccess::ChannelGet::shared_pointer const &); + void addChannelPut(epics::pvAccess::ChannelPut::shared_pointer const &); + void addChannelPutGet(epics::pvAccess::ChannelPutGet::shared_pointer const &); + void addChannelMonitor(epics::pvData::Monitor::shared_pointer const &); + void addChannelRPC(epics::pvAccess::ChannelRPC::shared_pointer const &); + void addChannelArray(epics::pvAccess::ChannelArray::shared_pointer const &); + void removeChannelProcess(epics::pvAccess::ChannelProcess::shared_pointer const &); + void removeChannelGet(epics::pvAccess::ChannelGet::shared_pointer const &); + void removeChannelPut(epics::pvAccess::ChannelPut::shared_pointer const &); + void removeChannelPutGet(epics::pvAccess::ChannelPutGet::shared_pointer const &); + void removeChannelMonitor(epics::pvData::Monitor::shared_pointer const &); + void removeChannelRPC(epics::pvAccess::ChannelRPC::shared_pointer const &); + void removeChannelArray(epics::pvAccess::ChannelArray::shared_pointer const &); +protected: + shared_pointer getPtrSelf() + { + return shared_from_this(); + } +private: + ChannelProviderLocalPtr provider; + epics::pvAccess::ChannelRequester::shared_pointer requester; + PVRecordPtr pvRecord; + bool beingDestroyed; + std::set channelProcessList; + std::set channelGetList; + std::set channelPutList; + std::set channelPutGetList; + std::set channelMonitorList; + std::set channelRPCList; + std::set channelArrayList; + epics::pvData::Mutex mutex; +}; + + + +}} +#endif /* CHANNELPROVIDERLOCAL_H */ diff --git a/test/Makefile b/test/Makefile index 88b61c1..0ac3794 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,5 +2,6 @@ TOP = .. include $(TOP)/configure/CONFIG DIRS += record DIRS += pvCopy +DIRS += server include $(TOP)/configure/RULES_DIRS diff --git a/test/server/Makefile b/test/server/Makefile new file mode 100644 index 0000000..29ac35b --- /dev/null +++ b/test/server/Makefile @@ -0,0 +1,17 @@ +TOP=../.. + +include $(TOP)/configure/CONFIG + +PROD_HOST += testExampleServer +testExampleServer_SRCS += testExampleServerMain.cpp +testExampleServer_LIBS += pvDatabase pvAccess pvData Com + +PROD_HOST += exampleCounter +exampleCounter_SRCS += exampleCounterMain.cpp +exampleCounter_LIBS += pvDatabase pvAccess pvData Com + + +include $(TOP)/configure/RULES +#---------------------------------------- +# ADD RULES AFTER THIS LINE + diff --git a/test/server/testExampleServerMain.cpp b/test/server/testExampleServerMain.cpp new file mode 100644 index 0000000..bef00fb --- /dev/null +++ b/test/server/testExampleServerMain.cpp @@ -0,0 +1,102 @@ +/*testExampleRecordMain.cpp */ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvData is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ +/** + * @author mrk + */ + +/* Author: Marty Kraimer */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace std; +using std::tr1::static_pointer_cast; +using namespace epics::pvData; +using namespace epics::pvAccess; +using namespace epics::pvDatabase; + + +static PVStructurePtr createPowerSupply() +{ + FieldCreatePtr fieldCreate = getFieldCreate(); + StandardFieldPtr standardField = getStandardField(); + PVDataCreatePtr pvDataCreate = getPVDataCreate(); + size_t nfields = 5; + StringArray names; + names.reserve(nfields); + FieldConstPtrArray powerSupply; + powerSupply.reserve(nfields); + names.push_back("alarm"); + powerSupply.push_back(standardField->alarm()); + names.push_back("timeStamp"); + powerSupply.push_back(standardField->timeStamp()); + String properties("alarm,display"); + names.push_back("voltage"); + powerSupply.push_back(standardField->scalar(pvDouble,properties)); + names.push_back("power"); + powerSupply.push_back(standardField->scalar(pvDouble,properties)); + names.push_back("current"); + powerSupply.push_back(standardField->scalar(pvDouble,properties)); + return pvDataCreate->createPVStructure( + fieldCreate->createStructure(names,powerSupply)); +} + + +int main(int argc,char *argv[]) +{ + PVDatabasePtr master = PVDatabase::getMaster(); + ChannelProviderLocalPtr channelProvider = ChannelProviderLocal::create(); + StandardPVFieldPtr standardPVField = getStandardPVField(); + String properties; + ScalarType scalarType; + String recordName; + properties = "alarm,timeStamp"; + scalarType = pvDouble; + recordName = "exampleDouble"; + PVStructurePtr pvStructure; + pvStructure = standardPVField->scalar(scalarType,properties); + PVRecordPtr pvRecord = PVRecord::create(recordName,pvStructure); + { + pvRecord->lock_guard(); + pvRecord->process(); + } + pvStructure.reset(); + bool result = master->addRecord(pvRecord); + pvRecord.reset(); + recordName = "powerSupplyExample"; + pvStructure = createPowerSupply(); + PowerSupplyRecordTestPtr psr = + PowerSupplyRecordTest::create(recordName,pvStructure); + if(psr.get()==NULL) { + cout << "PowerSupplyRecordTest::create failed" << endl; + return 1; + } + pvStructure.reset(); + result = master->addRecord(psr); + cout << "result of addRecord " << recordName << " " << result << endl; + psr.reset(); + cout << "exampleServer\n"; + string str; + while(true) { + cout << "Type exit to stop: \n"; + getline(cin,str); + if(str.compare("exit")==0) break; + + } + return 0; +} +