Example #1
0
        void ResourceHosting::discoveryHandler(RemoteObjectPtr remoteResource)
        {
            std::cout << "ResourceHosting: Discovered device with uri: " << remoteResource->getUri() << std::endl;
            auto discoverdUri = remoteResource->getUri();
            if (discoverdUri.compare(
                    discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
            {
                return;
            }

            auto foundHostingObject = findRemoteResource(remoteResource);
            if (foundHostingObject != nullptr) return;

            try
            {
                HostingObjectKey key = generateHostingObjectKey(remoteResource);
                foundHostingObject = HostingObject::createHostingObject(remoteResource,
                        std::bind(&ResourceHosting::destroyedHostingObject, this, key));

                RHLock lock(m_mutexForList);
                m_hostingObjects.insert(std::make_pair(key, foundHostingObject));

            } catch (const RCSException & e)
            {
                OIC_HOSTING_LOG(DEBUG,
                        "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
            }
        }
HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjectPtr rObject)
{
    ResourceObjectPtr retResource = nullptr;
    if(rObject != nullptr)
    {
        std::string fulluri = rObject->getUri();
        std::string uri = fulluri.substr(0, fulluri.size()-8);
        std::vector<std::string> types = rObject->getTypes();
        std::vector<std::string> interfaces = rObject->getInterfaces();
        try
        {
            std::string type = types.begin()->c_str();
            std::string interface = interfaces.begin()->c_str();
            retResource = RCSResourceObject::Builder(uri, type, interface).
                    setDiscoverable(true).setObservable(true).build();

            // TODO need to bind types and interfaces
            retResource->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
            retResource->setSetRequestHandler(pSetRequestHandler);
        }catch(...)
        {
            OIC_HOSTING_LOG(DEBUG, "[HostingObject::createMirroredServer] %s", "PlatformException");
            throw;
        }
    }
    else
    {
        throw PlatformException(OC_STACK_ERROR);
    }

    return retResource;
}
bool ResourceHosting::isSameRemoteResource(
        RemoteObjectPtr remoteResource_1, RemoteObjectPtr remoteResource_2)
{
    bool ret = false;
    if(remoteResource_1->getAddress() == remoteResource_2->getAddress() &&
       remoteResource_1->getUri() == remoteResource_2->getUri())
    {
        ret = true;
    }
    return ret;
}
Example #4
0
void petabricks::RemoteHost::createRemoteObject(const RemoteObjectPtr& local,
                                                RemoteObjectGenerator remote,
                                                const void* data, size_t len){
  local->markInitiatorMu();
  local->setHostMu(this);
  GeneralMessage msg = { MessageTypes::REMOTEOBJECT_CREATE,
                         0,
                         len,
                         EncodeDataPtr(local.asPtr()),
                         EncodeTextPtr(remote) };
  sendMsg(&msg, data, len);
  JLOCKSCOPE(_controlmu);
  _objects.push_back(local);
}
void ResourceHosting::discoverHandler(RemoteObjectPtr remoteResource)
{
    std::string discoverdUri = remoteResource->getUri();
    if(discoverdUri.compare(
            discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0)
    {
        return;
    }

    HostingObjectPtr foundHostingObject = findRemoteResource(remoteResource);
    if(foundHostingObject == nullptr)
    {
        try
        {
            foundHostingObject = std::make_shared<HostingObject>();
            foundHostingObject->initializeHostingObject(remoteResource,
                    std::bind(&ResourceHosting::destroyedHostingObject, this,
                            HostingObjectWeakPtr(foundHostingObject)));
            hostingObjectList.push_back(foundHostingObject);
        }catch(const RCSInvalidParameterException &e)
        {
            OIC_HOSTING_LOG(DEBUG,
                    "[ResourceHosting::discoverHandler]InvalidParameterException:%s", e.what());
        }
    }
}
void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject)
{
    remoteState = state;

    switch (state)
    {
    case ResourceState::ALIVE:
    {
        if(rObject->isCaching() == false)
        {
            try
            {
                rObject->startCaching(pDataUpdateCB);
            }catch(InvalidParameterException &e)
            {
                OIC_HOSTING_LOG(DEBUG,
                        "[HostingObject::stateChangedCB]startCaching InvalidParameterException:%s",
                        e.what());
            }
        }
        break;
    }
    case ResourceState::LOST_SIGNAL:
    case ResourceState::DESTROYED:
    {
        if(rObject->isCaching() == true)
        {
            try
            {
                rObject->stopCaching();
            }catch(InvalidParameterException &e)
            {
                OIC_HOSTING_LOG(DEBUG,
                        "[HostingObject::stateChangedCB]stopCaching InvalidParameterException:%s",
                        e.what());
            }
        }
        if(rObject->isMonitoring() == true)
        {
            try
            {
                rObject->stopMonitoring();
            }catch(InvalidParameterException &e)
            {
                OIC_HOSTING_LOG(DEBUG,
                        "[HostingObject::stateChangedCB]stopWatching InvalidParameterException:%s",
                        e.what());
            }
        }
        mirroredServer = nullptr;
        destroyHostingObject();
        break;
    }
    default:
        // not support of state
        break;
    }
}
int main(int argc, const char** argv){
  const char* filename = "testdata/Helmholtz3DB1";

  IndexT m1[] = {1,1,1};
  IndexT m123[] = {1,2,3};
  IndexT m456[] = {4,5,6};
  IndexT m2[] = {2,2,2};
  IndexT m3[] = {3,3,3};
  IndexT m234[] = {2,3,4};

  IndexT size[] = {8,9,8};
  //
  // Create a RegionMatrix
  MatrixRegion3D regionMatrix(size);

  if(argc==1){
    printf("main %d\n", getpid());

    RemoteHostDB::instance().remotefork(NULL, argc, argv);
    RemoteHostDB::instance().accept("");
    RemoteHostDB::instance().spawnListenThread();

    // Split the matrix in to multiple parts of size m2
    regionMatrix.splitData(m2);

    // Assign a chunk of data to remote host
    //   - put part 0 in hdb.host(0)
    //   - the other parts are created locally
    regionMatrix.createDataPart(0, RemoteHostDB::instance().host(0));

    // import data
    MatrixRegion3D in = MatrixIO(filename,"r").read_distributed<3>();
    regionMatrix.copyDataFromRegion(in);

    // or, allocate empty matrix
    // regionMatrix->allocData();

    printf("== data should be in 2 hosts ==\n");
    regionMatrix.printDataHosts();

    CellProxy cell = regionMatrix.cell(m234);
    JASSERT(fabs(cell - 0.52076837) < 0.00000001);
    cell = 5;
    JASSERT(fabs(cell - 5) < 0.00000001);

    // Test split
    MatrixRegion3D split3 = regionMatrix.region(m123, m456);
    printf("== region((1,2,3), (4,5,6)) : will send this to process2 ==\n");
    MatrixIO().write(split3);
    split3.printDataHosts();

    MatrixRegion3D split2 = split3.region(m1, m3);
    printf("== region((1,1,1), (3,3,3)) ==\n");
    MatrixIO().write(split2);
    split2.printDataHosts();

    MatrixRegion2D slice1 = split2.slice(2, 0);
    printf("== slice(2,0) ==\n");
    MatrixIO().write(slice1);
    slice1.printDataHosts();

    MatrixRegion1D slice2 = slice1.slice(1, 1);
    printf("== slice(1,1) ==\n");
    MatrixIO().write(slice2);
    slice2.printDataHosts();

    // Test slice
    MatrixRegion2D slice3 = regionMatrix.slice(1, 0);
    printf("== slice(1,0) of original matrix ==\n");
    MatrixIO().write(slice3);
    slice3.printDataHosts();

    ///////////////////////////////////
    // Migrate to process 2

    char* buf = new char[split3.serialSize()];
    split3.serialize(buf, *RemoteHostDB::instance().host(0));

    RemoteObjectPtr local;
    RemoteHostDB::instance().host(0)->createRemoteObject(local=gen(), &gen);
    local->waitUntilCreated();
    local->send(buf, split3.serialSize());

    printf("== sent matrix to process2 ==\n");

    RemoteHostDB::instance().listenLoop();
    return 0;
  } else {
    printf("main2 %d\n", getpid());
    JASSERT(argc==3);
    RemoteHostDB::instance().connect(argv[1], jalib::StringToInt(argv[2]));
    RemoteHostDB::instance().spawnListenThread();
    RemoteHostDB::instance().listenLoop();
    return 0;
  }
}
Example #8
0
bool petabricks::RemoteHost::recv() {
  GeneralMessage msg;

  if(!_controlmu.trylock()) {
    JTRACE("skipping recv, locked");
    return false;
  }

  ssize_t cnt = _control.tryReadAll((char*)&msg, sizeof msg);
  if(cnt==0) {
    _controlmu.unlock();
    return false;
  }
  if(cnt<0) {
    _controlmu.unlock();
    JASSERT(false)(_id).Text("disconnected");
    return false;
  }
  JASSERT(cnt==sizeof msg)(cnt);

  if(msg.len>0){
    JASSERT(msg.chan>=0 && msg.chan<REMOTEHOST_DATACHANS);
    _datamu[msg.chan].lock();
  }
  _controlmu.unlock();
  RemoteObjectGenerator gen = 0;
  RemoteObjectPtr obj = 0;
  void* buf = 0;

  switch(msg.type) {
  case MessageTypes::REMOTEOBJECT_CREATE: 
    {
      gen = DecodeTextPtr<RemoteObjectGenerator>(msg.dstptr);
      obj = (*gen)();
      JLOCKSCOPE(*obj);
      obj->setHostMu(this);
      obj->setRemoteObjMu(msg.srcptr);
      if(msg.len>0){
        buf = obj->allocRecvInitial(msg.len);
        _data[msg.chan].readAll((char*)buf, msg.len);
        _datamu[msg.chan].unlock();
        obj->onRecvInitial(buf, msg.len);
        obj->freeRecvInitial(buf, msg.len);
      }
      obj->onCreated();
      obj->markCreatedMu();
      { GeneralMessage ackmsg = { MessageTypes::REMOTEOBJECT_CREATE_ACK, 0, 0, EncodeDataPtr(obj.asPtr()), msg.srcptr };
        sendMsg(&ackmsg);
      }
      JLOCKSCOPE(_controlmu);
      _objects.push_back(obj);
      break;
    }
  case MessageTypes::REMOTEOBJECT_CREATE_ACK:
    {
      obj = DecodeDataPtr<RemoteObject>(msg.dstptr);
      JASSERT(msg.len==0);
      JLOCKSCOPE(*obj);
      obj->setRemoteObjMu(msg.srcptr);
      obj->onCreated();
      obj->markCreatedMu();
      break;
    }
  case MessageTypes::REMOTEOBJECT_DATA:
    {
      obj = DecodeDataPtr<RemoteObject>(msg.dstptr);
      JLOCKSCOPE(*obj);
      if(msg.len>0){
        buf = obj->allocRecv(msg.len);
        _data[msg.chan].readAll((char*)buf, msg.len);
        _datamu[msg.chan].unlock();
        obj->onRecv(buf, msg.len);
        obj->freeRecv(buf, msg.len);
      }
      break;
    }
  case MessageTypes::REMOTEOBJECT_SIGNAL:
    {
      obj = DecodeDataPtr<RemoteObject>(msg.dstptr);
      JASSERT(msg.len==0);
      JLOCKSCOPE(*obj);
      obj->signal();
      break;
    }
  case MessageTypes::REMOTEOBJECT_BROADCAST:
    {
      obj = DecodeDataPtr<RemoteObject>(msg.dstptr);
      JASSERT(msg.len==0);
      JLOCKSCOPE(*obj);
      obj->broadcast();
      break;
    }
  case MessageTypes::REMOTEOBJECT_NOTIFY:
    {
      obj = DecodeDataPtr<RemoteObject>(msg.dstptr);
      JASSERT(msg.len==0);
      JLOCKSCOPE(*obj);
      obj->onNotify(msg.srcptr);
      break;
    }
  case MessageTypes::REMOTEOBJECT_MARKCOMPLETE:
    {
      obj = DecodeDataPtr<RemoteObject>(msg.dstptr);
      JASSERT(msg.len==0);
      JLOCKSCOPE(*obj);
      obj->onComplete();
      obj->markCompleteMu();
      break;
    }
  default:
    JASSERT(false);
  }
  return true;
}
Example #9
0
 ResourceHosting::HostingObjectKey ResourceHosting::generateHostingObjectKey(
         RemoteObjectPtr rResource)
 {
     return generateHostingObjectKey(rResource->getAddress(), rResource->getUri());
 }