BowlerKernel
BowlerAbstractDeviceServer.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.common.device.server;
2 
3 import java.io.IOException;
4 import java.util.ArrayList;
5 
6 import com.neuronrobotics.sdk.commands.bcs.core.NamespaceCommand;
7 import com.neuronrobotics.sdk.commands.bcs.core.PingCommand;
8 import com.neuronrobotics.sdk.common.BowlerAbstractCommand;
9 import com.neuronrobotics.sdk.common.BowlerAbstractDevice;
10 import com.neuronrobotics.sdk.common.BowlerDatagram;
11 import com.neuronrobotics.sdk.common.BowlerDatagramFactory;
12 import com.neuronrobotics.sdk.common.Log;
13 
14 
15 // TODO: Auto-generated Javadoc
19 public abstract class BowlerAbstractDeviceServer extends BowlerAbstractDevice {
20 
22  private String core = "bcs.core.*;0.3;;";
23 
25  private ArrayList<String> namespaces = new ArrayList<String>();
26 
27  /* (non-Javadoc)
28  * @see com.neuronrobotics.sdk.common.BowlerAbstractDevice#connect()
29  */
30  @Override
31  public boolean connect(){
32  super.connect();
34  return isAvailable();
35  }
36 
42  public void addNamespace(String nms){
43  if(!namespaces.contains(nms))
44  namespaces.add(nms);
45  }
46 
47  /* (non-Javadoc)
48  * @see com.neuronrobotics.sdk.common.BowlerAbstractDevice#onAllResponse(com.neuronrobotics.sdk.common.BowlerDatagram)
49  */
50  public void onAllResponse(BowlerDatagram data) {
51  String rpc = data.getRPC();
52  if(rpc.contains("_nms")) {
53  Log.info("Got a namespace packet of size: "+data.getData().size());
54  if(data.getData().size() == 0) {
55  try {
57  } catch (IOException e) {
58  e.printStackTrace();
59  }
60  return;
61  }
62  if(data.getData().size() == 1) {
63  int index = data.getData().get(0);
64  try {
65  sendSyncResponse(new NamespaceCommand(namespaces.size(),namespaces.get(index)));
66  } catch (IOException e) {
67  e.printStackTrace();
68  }
69  return;
70  }
71  }else if(rpc.contains("_png")){
72  //Log.info("Got ping");
73  try {
75  } catch (IOException e) {
76  e.printStackTrace();
77  }
78  return;
79  }else
80  onSynchronusRecive(data);
81 
82  }
83 
84  /* (non-Javadoc)
85  * @see com.neuronrobotics.sdk.common.IBowlerDatagramListener#onAsyncResponse(com.neuronrobotics.sdk.common.BowlerDatagram)
86  */
87  public void onAsyncResponse(BowlerDatagram data) {
88  // TODO Auto-generated method stub
89 
90  }
91 
99  public void sendAsync(BowlerAbstractCommand command, int rpcIndexID) throws IOException {
100  command.setNamespaceIndex(rpcIndexID);
101  BowlerDatagram bd = BowlerDatagramFactory.build(getAddress(), command);
102  //Log.debug("ASYN>>\n"+bd.toString());
103  getConnection().sendAsync(bd);
104  getConnection().getDataOuts().flush();
105  }
106 
113  public void sendSyncResponse(BowlerAbstractCommand command) throws IOException {
114  BowlerDatagram bd =BowlerDatagramFactory.build(getAddress(), command);
115  //Log.debug("RESP>>\n"+bd.toString());
116  getConnection().sendAsync(bd);
117  getConnection().getDataOuts().flush();
118  }
119 
126  public void sendPacketWithNoResponse(BowlerDatagram data) throws IOException {
127  //Log.debug("RESP>>\n"+bd.toString());
128  getConnection().sendAsync(data);
129  getConnection().getDataOuts().flush();
130  }
131 
137  public abstract void onSynchronusRecive(BowlerDatagram data);
138 }
static void info(String message)
Definition: Log.java:110