hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
net.H
Go to the documentation of this file.
1 /********
2  * net : I/O for a "net REPL" between processes
3  ********/
4 
5 #ifndef HOBBES_EVENTS_NET_HPP_INCLUDED
6 #define HOBBES_EVENTS_NET_HPP_INCLUDED
7 
8 #include <hobbes/lang/type.H>
9 #include <string>
10 #include <map>
11 #include <queue>
12 
13 namespace hobbes {
14 
15 // get the port number associated with a named port
16 int lookupPort(const std::string&);
17 
18 // create a socket listening on a port
19 int allocateServer(int port);
20 int allocateServer(const std::string& port);
21 
22 // connect to a host/port
23 int connectSocket(const std::string& host, int port);
24 int connectSocket(const std::string& hostport);
25 
26 // create a unix domain socket backed by a file
27 int allocateFileSocketServer(const std::string& filepath);
28 
29 // get the name of the host on the other end of this socket
30 std::string remoteHostname(int socket);
31 
32 // run a REPL server at some port, either indirectly or directly with a compiler
33 typedef std::vector<uint8_t> RawData;
34 typedef uint32_t exprid;
35 
36 struct Server {
37  virtual void connect (int conn) = 0;
38  virtual ExprPtr readExpr (const std::string&) = 0;
39  virtual MonoTypePtr prepare (int conn, exprid, const ExprPtr&, const MonoTypePtr&) = 0;
40  virtual void evaluate (int conn, exprid) = 0;
41  virtual void disconnect(int conn) = 0;
42 };
43 
44 int installNetREPL(int port, Server*);
45 // install a net repl on a unix domain socket (using file paths)
46 int installNetREPL(const std::string& /*filepath*/, Server*);
47 
48 class cc;
49 int installNetREPL(int port, cc*);
50 // install a net repl on a unix domain socket (using file paths)
51 int installNetREPL(const std::string& /*filepath*/, cc*);
52 
53 // connect to a running net REPL somewhere
54 class Client {
55 public:
56  /* init/compile-time methods */
57  Client(const std::string& hostport);
58  ~Client();
59  const std::string& remoteHost() const;
60 
61  exprid remoteExpr(const ExprPtr&, const MonoTypePtr& inty);
62 
63  MonoTypePtr input(exprid) const;
64  MonoTypePtr output(exprid) const;
65  MonoTypePtr output(const ExprPtr&, const MonoTypePtr& inty);
66 
67  inline int fd() const { return this->c; }
68 private:
69  int c;
70  std::string hostport;
71  exprid eid;
72 
73  struct ExprDef {
77  };
78  typedef std::map<exprid, ExprDef> ExprDefs;
79  ExprDefs exprDefs;
80 
81  typedef std::pair<const void*,const void*> ExprTy;
82  typedef std::map<ExprTy, exprid> ExprTyToID;
83  ExprTyToID exprTyToID;
84 public:
85  /* run-time methods */
86  void show(std::ostream&) const;
87 
88  // append a receive function to the end of the sequence of receive handlers
89  // these functions will be applied in order to read values from the remote process
90  typedef char* (*ReadFn)(int);
91  size_t appendReadFn(ReadFn);
92 
93  // read a result (reading and discarding any intermediate results)
94  char* readValue(size_t);
95 
96  // only used by generated code
97  static size_t unsafeAppendReadFn(size_t, ReadFn);
98  static char* unsafeRead(size_t, size_t);
99 private:
100  typedef std::queue<ReadFn> ReadFns;
101  ReadFns readFns;
102  size_t rbno;
103  size_t reno;
104 };
105 
106 }
107 
108 #endif
109 
virtual ExprPtr readExpr(const std::string &)=0
static int input(void)
Definition: hexpr.lex.C:1957
exprid eid
Definition: net.H:71
ExprPtr expr
Definition: net.H:74
int c
Definition: net.H:69
ReadFns readFns
Definition: net.H:101
virtual void connect(int conn)=0
std::map< exprid, ExprDef > ExprDefs
Definition: net.H:78
Definition: net.H:36
Definition: boot.H:7
int allocateFileSocketServer(const std::string &filepath)
Definition: net.C:72
uint32_t exprid
Definition: net.H:34
virtual void evaluate(int conn, exprid)=0
int fd() const
Definition: net.H:67
virtual MonoTypePtr prepare(int conn, exprid, const ExprPtr &, const MonoTypePtr &)=0
size_t rbno
Definition: net.H:102
MonoType::ptr MonoTypePtr
Definition: type.H:71
std::queue< ReadFn > ReadFns
Definition: net.H:100
ExprDefs exprDefs
Definition: net.H:79
std::pair< const void *, const void * > ExprTy
Definition: net.H:81
int installNetREPL(int port, Server *)
Definition: net.C:304
virtual void disconnect(int conn)=0
std::vector< uint8_t > RawData
Definition: net.H:33
int lookupPort(const std::string &)
Definition: net.C:22
std::string show(const Expr &e)
Definition: expr.C:19
MonoTypePtr inty
Definition: net.H:75
std::shared_ptr< Expr > ExprPtr
Definition: expr.H:58
int allocateServer(int port)
Definition: net.C:36
std::string hostport
Definition: net.H:70
Definition: cc.H:64
std::map< ExprTy, exprid > ExprTyToID
Definition: net.H:82
std::string remoteHostname(int socket)
Definition: net.C:169
size_t reno
Definition: net.H:103
int connectSocket(const std::string &host, int port)
Definition: net.C:152
MonoTypePtr outty
Definition: net.H:76
ExprTyToID exprTyToID
Definition: net.H:83
Definition: net.H:54
Definition: net.H:73