CPN
Computational Process Networks
RemoteContextClient.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #ifndef CPN_REMOTECONTEXTCLIENT_H
24 #define CPN_REMOTECONTEXTCLIENT_H
25 #pragma once
26 
27 #include <cpn/common.h>
29 #include <cpn/Context.h>
30 #include <cpn/QueueAttr.h>
31 #include <cpn/NodeAttr.h>
34 #include <Variant/Variant.h>
35 #include <list>
36 
37 class Pthread;
38 
39 namespace CPN {
40 
48  class RemoteContextClient : public Context {
49  public:
50  virtual ~RemoteContextClient();
51 
52  virtual int LogLevel() const;
53  virtual int LogLevel(int level);
54  virtual void Log(int level, const std::string &logmsg);
55 
56  virtual CPN::Key_t SetupKernel(const std::string &name, const std::string &hostname,
57  const std::string &servname, CPN::KernelBase *kernel);
58  virtual CPN::Key_t SetupKernel(const std::string &name, KernelBase *kernel) { ASSERT(false, "RemoteContextClient requires remote operation is turned on."); }
59  virtual CPN::Key_t GetKernelKey(const std::string &kernel);
60  virtual std::string GetKernelName(CPN::Key_t kernelkey);
61  virtual void GetKernelConnectionInfo(CPN::Key_t kernelkey, std::string &hostname, std::string &servname);
62  virtual CPN::Key_t WaitForKernelStart(const std::string &kernel);
63  virtual void SignalKernelStart(CPN::Key_t kernelkey);
64  virtual void SignalKernelEnd(CPN::Key_t kernelkey);
65 
66  virtual void SendCreateWriter(CPN::Key_t kernelkey, const CPN::SimpleQueueAttr &attr);
67  virtual void SendCreateReader(CPN::Key_t kernelkey, const CPN::SimpleQueueAttr &attr);
68  virtual void SendCreateQueue(CPN::Key_t kernelkey, const CPN::SimpleQueueAttr &attr);
69  virtual void SendCreateNode(CPN::Key_t kernelkey, const CPN::NodeAttr &attr);
70 
71  virtual CPN::Key_t CreateNodeKey(CPN::Key_t kernelkey, const std::string &nodename);
72  virtual CPN::Key_t GetNodeKey(const std::string &nodename);
73  virtual std::string GetNodeName(CPN::Key_t nodekey);
74  virtual CPN::Key_t GetNodeKernel(CPN::Key_t nodekey);
75  virtual void SignalNodeStart(CPN::Key_t nodekey);
76  virtual void SignalNodeEnd(CPN::Key_t nodekey);
77 
78  virtual CPN::Key_t WaitForNodeStart(const std::string &nodename);
79  virtual void WaitForNodeEnd(const std::string &nodename);
80  virtual void WaitForAllNodeEnd();
81 
82 
83  virtual CPN::Key_t GetCreateReaderKey(CPN::Key_t nodekey, const std::string &portname);
84  virtual CPN::Key_t GetReaderNode(CPN::Key_t portkey);
85  virtual CPN::Key_t GetReaderKernel(CPN::Key_t portkey);
86  virtual std::string GetReaderName(CPN::Key_t portkey);
87 
88  virtual CPN::Key_t GetCreateWriterKey(CPN::Key_t nodekey, const std::string &portname);
89  virtual CPN::Key_t GetWriterNode(CPN::Key_t portkey);
90  virtual CPN::Key_t GetWriterKernel(CPN::Key_t portkey);
91  virtual std::string GetWriterName(CPN::Key_t portkey);
92 
93  virtual void ConnectEndpoints(CPN::Key_t writerkey, CPN::Key_t readerkey, const std::string &qname);
94  virtual CPN::Key_t GetReadersWriter(CPN::Key_t readerkey);
95  virtual CPN::Key_t GetWritersReader(CPN::Key_t writerkey);
96 
97  virtual void Terminate();
98  virtual bool IsTerminated();
99 
100  virtual bool RequireRemote();
101 
106  void DispatchMessage(const Variant &msg);
107  protected:
114  virtual void SendMessage(const Variant &msg) = 0;
115 
117  private:
118 
123  struct WaiterInfo {
124  WaiterInfo(unsigned wid) : waiterid(wid), signaled(false) {}
125  unsigned waiterid;
127  bool signaled;
128  Variant msg;
129  };
130 
135  struct GenericWaiter {
137  std::list<Variant> messages;
138  };
139  typedef shared_ptr<GenericWaiter> GenericWaiterPtr;
140 
141  void SendQueueMsg(CPN::Key_t kernelkey, RCTXMT_t msgtype, const CPN::SimpleQueueAttr &attr);
142  unsigned NewTranID();
143  void AddWaiter(WaiterInfo *info);
145  void InternalTerminate();
147  void *TerminateThread();
148  Variant RemoteCall(Variant msg);
149 
150  CPN::Key_t GetCreateEndpointKey(RCTXMT_t msgtype, CPN::Key_t nodekey, const std::string &portname);
151  Variant GetEndpointInfo(RCTXMT_t msgtype, CPN::Key_t portkey);
152 
153  auto_ptr<Pthread> terminateThread;
154  typedef std::map<unsigned, WaiterInfo*> WaiterMap;
156  typedef std::list<weak_ptr<GenericWaiter> > WaiterList;
158  typedef std::map<CPN::Key_t, CPN::KernelBase*> KernelMap;
160  unsigned trancounter;
161  bool shutdown;
162  int loglevel;
163  };
164 }
165 #endif
std::map< unsigned, WaiterInfo * > WaiterMap
virtual void WaitForNodeEnd(const std::string &nodename)
Waits for the given node to signal end.
virtual int LogLevel() const
void SendQueueMsg(CPN::Key_t kernelkey, RCTXMT_t msgtype, const CPN::SimpleQueueAttr &attr)
virtual CPN::Key_t GetReaderKernel(CPN::Key_t portkey)
Attributes for a node.
Definition: NodeAttr.h:58
RCTXMT_t
RCTXMT Remote Context Message Type These are the message types that the remote context uses to send i...
Definition: RCTXMT.h:33
Variant GetEndpointInfo(RCTXMT_t msgtype, CPN::Key_t portkey)
This abstract class is the remote context client.
Variant RemoteCall(Variant msg)
virtual CPN::Key_t GetNodeKernel(CPN::Key_t nodekey)
virtual std::string GetWriterName(CPN::Key_t portkey)
virtual void GetKernelConnectionInfo(CPN::Key_t kernelkey, std::string &hostname, std::string &servname)
obtain the connection information for the given kernel
virtual std::string GetKernelName(CPN::Key_t kernelkey)
virtual CPN::Key_t GetReadersWriter(CPN::Key_t readerkey)
CPN::Key_t GetCreateEndpointKey(RCTXMT_t msgtype, CPN::Key_t nodekey, const std::string &portname)
virtual CPN::Key_t GetCreateWriterKey(CPN::Key_t nodekey, const std::string &portname)
void AddWaiter(WaiterInfo *info)
void DispatchMessage(const Variant &msg)
virtual void Terminate()
Signal to the Context that the network is terminating. After this call most methods will throw a Shut...
Definition of the queue attributes.
GenericWaiterPtr NewGenericWaiter()
Definition for node attributes.
The Context abstract data type.
virtual void SignalKernelEnd(CPN::Key_t kernelkey)
Signal to the Context that the given kernel is dead.
virtual CPN::Key_t SetupKernel(const std::string &name, const std::string &hostname, const std::string &servname, CPN::KernelBase *kernel)
Called by the Kernel when it has successfully set it self up. This gives the Context a way to notify ...
virtual CPN::Key_t CreateNodeKey(CPN::Key_t kernelkey, const std::string &nodename)
Tell the context to allocate a new node key and data structure for a node with nodename which is on k...
virtual void SignalNodeStart(CPN::Key_t nodekey)
Called by the node startup routine to indicate that the node has started.
virtual CPN::Key_t WaitForKernelStart(const std::string &kernel)
Does not return until the given kernel has started.
virtual CPN::Key_t GetKernelKey(const std::string &kernel)
uint64_t Key_t
Definition: common.h:79
virtual void SendCreateNode(CPN::Key_t kernelkey, const CPN::NodeAttr &attr)
Tell a given kernel that it needs to create a node.
virtual CPN::Key_t SetupKernel(const std::string &name, KernelBase *kernel)
Called by the kernel when it is not in remote mode.
virtual std::string GetReaderName(CPN::Key_t portkey)
virtual CPN::Key_t GetWritersReader(CPN::Key_t writerkey)
The CPN::Context abstraction that holds all the global state for the process network.
Definition: Context.h:47
virtual CPN::Key_t WaitForNodeStart(const std::string &nodename)
Waits until the node starts and returns the key, if the node is already started returns the key...
virtual void SendCreateWriter(CPN::Key_t kernelkey, const CPN::SimpleQueueAttr &attr)
Tell a given kernel that it needs to create a queue write end.
virtual bool RequireRemote()
Lets the kernel know that this context type requires remote activity. This overrides the kernel optio...
std::list< weak_ptr< GenericWaiter > > WaiterList
virtual void Log(int level, const std::string &logmsg)
Log a message to this outputer.
std::map< CPN::Key_t, CPN::KernelBase * > KernelMap
virtual void ConnectEndpoints(CPN::Key_t writerkey, CPN::Key_t readerkey, const std::string &qname)
Called by the kernel when a queue is created. Note that the endpoints may have been created when the ...
virtual CPN::Key_t GetCreateReaderKey(CPN::Key_t nodekey, const std::string &portname)
Get the key associated with the given endpoint for the given node. Creates the information if it does...
virtual void SendCreateReader(CPN::Key_t kernelkey, const CPN::SimpleQueueAttr &attr)
Tell a given kernel that it needs to create a queue read end.
virtual void SendMessage(const Variant &msg)=0
virtual void SendCreateQueue(CPN::Key_t kernelkey, const CPN::SimpleQueueAttr &attr)
Tell a given kernel that it needs to create a queue.
virtual void SignalNodeEnd(CPN::Key_t nodekey)
Called by the node cleanup routine to indicate that the node has ended.
virtual CPN::Key_t GetWriterKernel(CPN::Key_t portkey)
virtual std::string GetNodeName(CPN::Key_t nodekey)
auto_ptr< Pthread > terminateThread
virtual void SignalKernelStart(CPN::Key_t kernelkey)
Signal to the context that the given kernel has started.
shared_ptr< GenericWaiter > GenericWaiterPtr
virtual CPN::Key_t GetReaderNode(CPN::Key_t portkey)
This is a simplified internal representation of the queue attributes needed to create a queue...
Definition: QueueAttr.h:237
virtual CPN::Key_t GetWriterNode(CPN::Key_t portkey)
virtual CPN::Key_t GetNodeKey(const std::string &nodename)
#define ASSERT(exp,...)
virtual void WaitForAllNodeEnd()
Convenience method which waits until there are no nodes running. If no node have started then this wi...