ffead.server.doc
BeanContext.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * BeanContext.cpp
18  *
19  * Created on: Mar 27, 2010
20  * Author: sumeet
21  */
22 
23 #include "BeanContext.h"
24 
25 RemoteComponentInt::RemoteComponentInt() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 RemoteComponentInt::~RemoteComponentInt() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 void RemoteComponentInt::setBeanContext(BeanContext cntxt)
35 {
36  this->cntxt = cntxt;;
37 }
38 BeanContext RemoteComponentInt::getContext()
39 {
40  return this->cntxt;
41 }
42 
43 
44 BeanContext::BeanContext() {
45  logger = Logger::getLogger("BeanContext");
46 }
47 
48 BeanContext::BeanContext(string host,int port) {
49  this->setHost(host);
50  this->setPort(port);
51 }
52 
53 BeanContext::~BeanContext() {
54  // TODO Auto-generated destructor stub
55 }
56 string BeanContext::getHost() const
57 {
58  return host;
59 }
60 
61 void BeanContext::setHost(string host)
62 {
63  this->host = host;
64 }
65 
66 int BeanContext::getPort() const
67 {
68  return port;
69 }
70 
71 void BeanContext::setPort(int port)
72 {
73  this->port = port;
74 }
75 
76 void* BeanContext::lookup(string cmpName)
77 {
78  void *_temp = NULL;
79  if(!client.isConnected())
80  client.connection(host,port);
81  if(client.isConnected())
82  {
83  Reflector ref;
84  string classn;
85  classn = "Component_"+cmpName+"_Remote";
86  ClassInfo clas = ref.getClassInfo(classn);
87  args argus;
88  Constructor ctor = clas.getConstructor(argus);
89  _temp = ref.newInstanceGVP(ctor);
90  RemoteComponentInt* intf = (RemoteComponentInt*)_temp;
91  intf->setBeanContext(*this);
92  }
93  return _temp;
94 }
95 
96 void* BeanContext::invoke(string name,vector<Object> args,string bname,string rettyp)
97 {
98  void* retval = NULL;
99  if(client.isConnected())
100  {
101  Serialize ser;
102  string argus;
103  if(args.size()>0)
104  {
105  for (unsigned int var = 0; var < args.size(); ++var)
106  {
107  argus += "<argument type=\""+rettyp+"\">"+ser.serializeUnknown(args.at(var).getVoidPointer(),rettyp)+"</argument>";
108  }
109  }
110  string call = "<service name=\""+name+"\" beanName=\""+bname+"\" lang=\"c++\" returnType=\""+rettyp+"\"><args>"+argus+"</args></service>";
111  client.sendData(call);
112  call = "";
113  while((call=client.getData())=="")
114  {
115  }
116  //logger << call << flush;
117  XmlParser parser("Parser");
118  Document doc = parser.getDocument(call);
119  Element message = doc.getRootElement();
120  if(message.getTagName().find("<return:exception>")==string::npos)
121  {
122  string tag = message.getTagName();
123  StringUtil::replaceFirst(tag,"return:","");
124  message.setTagName(tag);
125  call = message.render();
126  //logger << call << flush;
127  retval = ser.unSerializeUnknown(call,tag);
128  }
129  else
130  {
131  throw "Exception occurred";
132  }
133  //logger << retval << flush;
134  }
135  else
136  {
137  throw "Invalid BeanContext";
138  }
139  return retval;
140 }