Atlas  0.7.0
Networking protocol for the Worldforge system.
XMLish.cpp
1 // This file may be redistributed and modified under the terms of the
2 // GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2000 Michael Day
4 
5 // $Id$
6 
7 #include <Atlas/Codecs/Utility.h>
8 #include <Atlas/Codec.h>
9 
10 #include <iostream>
11 
12 using namespace Atlas;
13 
14 /*
15 
16  FIXME documentation forthcoming
17 
18 */
19 
20 class XMLish : public Codec<std::iostream>
21 {
22  public:
23 
25 
26  virtual void poll();
27 
28  virtual void streamBegin();
29  virtual void streamMessage();
30  virtual void streamEnd();
31 
32  virtual void mapItem(const std::string& name, const Map&);
33  virtual void mapItem(const std::string& name, const List&);
34  virtual void mapItem(const std::string& name, std::int64_t);
35  virtual void mapItem(const std::string& name, double);
36  virtual void mapItem(const std::string& name, const std::string&);
37  virtual void mapEnd();
38 
39  virtual void listItem(const Map&);
40  virtual void listItem(const List&);
41  virtual void listItem(std::int64_t);
42  virtual void listItem(double);
43  virtual void listItem(const std::string&);
44  virtual void listEnd();
45 
46  protected:
47 
48  std::iostream& socket;
49  Bridge* bridge;
50 };
51 
52 namespace
53 {
54  /*
55  We're not going to enable this sucker until it actually works.
56 
57  Codec<std::iostream>::Factory<XMLish> factory(
58  "XMLish", // name
59  Codec<std::iostream>::Metrics(1, 2) // metrics
60  );
61 
62  */
63 }
64 
65 XMLish::XMLish(const Codec<std::iostream>::Parameters& p)
66  : socket(p.stream), bridge(p.bridge)
67 {
68 }
69 
70 void XMLish::poll()
71 {
72  if (!can_read) return;
73  do
74  {
75  char next = socket.get();
76 
77  // FIXME handle incoming characters
78  }
79  while (socket.rdbuf()->in_avail());
80 }
81 
82 void XMLish::streamBegin()
83 {
84  socket << "<atlas>";
85 }
86 
87 void XMLish::streamMessage()
88 {
89  socket << "<map>";
90 }
91 
92 void XMLish::streamEnd()
93 {
94  socket << "</atlas>";
95 }
96 
97 void XMLish::mapItem(const std::string& name, const Map&)
98 {
99  socket << "<map name=\"" << name << "\">";
100 }
101 
102 void XMLish::mapItem(const std::string& name, const List&)
103 {
104  socket << "<list name=\"" << name << "\">";
105 }
106 
107 void XMLish::mapItem(const std::string& name, std::int64_t data)
108 {
109  socket << "<int name=\"" << name << "\">" << data << "</int>";
110 }
111 
112 void XMLish::mapItem(const std::string& name, double data)
113 {
114  socket << "<float name=\"" << name << "\">" << data << "</float>";
115 }
116 
117 void XMLish::mapItem(const std::string& name, const std::string& data)
118 {
119  socket << "<string name=\"" << name << "\">" << data << "</string>";
120 }
121 
122 void XMLish::mapEnd()
123 {
124  socket << "</map>";
125 }
126 
127 void XMLish::listItem(const Map&)
128 {
129  socket << "<map>";
130 }
131 
132 void XMLish::listItem(const List&)
133 {
134  socket << "<list>";
135 }
136 
137 void XMLish::listItem(std::int64_t data)
138 {
139  socket << "<int>" << data << "</int>";
140 }
141 
142 void XMLish::listItem(double data)
143 {
144  socket << "<float>" << data << "</float>";
145 }
146 
147 void XMLish::listItem(const std::string& data)
148 {
149  socket << "<string>" << data << "</string>";
150 }
151 
152 void XMLish::listEnd()
153 {
154  socket << "</list>";
155 }
156 
Definition: Bridge.h:21