Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
14 KB
Referenced Files
None
Subscribers
None
diff --git a/util/nacl/network-system.cpp b/util/nacl/network-system.cpp
index 128f65cd..9bb44724 100644
--- a/util/nacl/network-system.cpp
+++ b/util/nacl/network-system.cpp
@@ -1,191 +1,293 @@
#ifdef NACL
+/* documentation for ppapi
+ * http://code.google.com/chrome/nativeclient/docs/reference/peppercpp/inherits.html
+ */
+
+/* issues with getting data
+ * 1. the function that starts the game is called from the main chrome thread
+ * which starts from a javascript call to module.PostMessage('run').
+ * ...
+ *
+ */
+
#include "network-system.h"
#include "../funcs.h"
#include "../debug.h"
#include <ppapi/c/pp_errors.h>
#include <ppapi/cpp/url_loader.h>
#include <ppapi/cpp/url_request_info.h>
#include <ppapi/cpp/completion_callback.h>
using std::string;
namespace Nacl{
typedef Path::AbsolutePath AbsolutePath;
typedef Path::RelativePath RelativePath;
enum RequestType{
Exists
};
struct Request{
RequestType type;
AbsolutePath absolute;
RelativePath relative;
bool complete;
+ bool success;
};
Request operation;
+struct NaclRequest{
+ virtual ~NaclRequest(){
+ }
+
+ virtual void start(pp::CompletionCallback callback) = 0;
+};
+
+struct NaclRequestOpen: public NaclRequest {
+ NaclRequestOpen(pp::Instance * instance, const string & url):
+ request(instance),
+ loader(instance){
+ request.SetURL(url);
+ request.SetMethod("GET");
+ }
+
+ void start(pp::CompletionCallback callback){
+ Global::debug(0) << "Request open" << std::endl;
+ int32_t ok = loader.Open(request, callback);
+ Global::debug(0) << "Open " << ok << std::endl;
+ if (ok != PP_OK_COMPLETIONPENDING){
+ callback.Run(ok);
+ }
+ Global::debug(0) << "Callback running" << std::endl;
+ }
+
+ pp::URLRequestInfo request;
+ pp::URLLoader loader;
+};
+
+class Manager{
+public:
+ Manager(pp::Instance * instance, Util::Thread::LockObject & portal):
+ instance(instance),
+ portal(portal),
+ factory(this){
+ }
+
+ pp::Instance * instance;
+ Util::Thread::LockObject & portal;
+ Util::ReferenceCount<NaclRequest> request;
+ pp::CompletionCallbackFactory<Manager> factory;
+
+ /*
+ void start(){
+ pp::CompletionCallback callback = factory.NewCallback(&Manager::OnOpen);
+ int32_t ok = loader.Open(request, callback);
+ Global::debug(0) << "Open " << ok << std::endl;
+ if (ok != PP_OK_COMPLETIONPENDING){
+ callback.Run(ok);
+ }
+ Global::debug(0) << "Callback running" << std::endl;
+ }
+ */
+
+ void OnOpen(int32_t response){
+ if (response == 0){
+ Global::debug(0) << "Opened!" << std::endl;
+ operation.success = true;
+ } else {
+ Global::debug(0) << "Failed to open :(" << std::endl;
+ operation.success = false;
+ }
+
+ run2();
+ }
+
+ void doit(){
+ Global::debug(0) << "Requesting a file operation" << std::endl;
+
+ request = new NaclRequestOpen(instance, operation.absolute.path());
+ Global::debug(0) << "Made request" << std::endl;
+ request->start(factory.NewCallback(&Manager::OnOpen));
+ /* the handler will call into the browser. the browser will asynchounsly
+ * call the handler again and the handler will call run2 to finish up
+ */
+ Global::debug(0) << "Releasing control back to the browser" << std::endl;
+ }
+
+ static int run(void * me){
+ Manager * self = (Manager*) me;
+ self->doit();
+ return 0;
+ }
+
+ void run2(){
+ portal.acquire();
+ Global::debug(0) << "File operation done" << std::endl;
+ operation.complete = true;
+ portal.signal();
+ portal.release();
+ }
+
+ /*
+ pp::URLRequestInfo request;
+ pp::URLLoader loader;
+ */
+};
+
NetworkSystem::NetworkSystem(const string & serverPath, pp::Instance * instance):
instance(instance),
-serverPath(serverPath){
+serverPath(serverPath),
+manager(new Manager(instance, portal)){
}
/* TODO */
NetworkSystem::~NetworkSystem(){
}
/* TODO */
AbsolutePath NetworkSystem::find(const RelativePath & path){
return Util::getDataPath2().join(path);
}
/* TODO */
RelativePath NetworkSystem::cleanse(const AbsolutePath & path){
return RelativePath();
}
bool NetworkSystem::exists(const RelativePath & path){
try{
AbsolutePath absolute = find(path);
return true;
} catch (const Storage::NotFound & found){
return false;
}
}
+/*
class Handler{
public:
Handler(pp::Instance * instance, const AbsolutePath & path, NetworkSystem * system):
request(instance),
loader(instance),
factory(this),
http(false),
system(system){
request.SetURL(path.path());
request.SetMethod("GET");
}
virtual void start(){
pp::CompletionCallback callback = factory.NewCallback(&Handler::OnOpen);
int32_t ok = loader.Open(request, callback);
Global::debug(0) << "Open " << ok << std::endl;
if (ok != PP_OK_COMPLETIONPENDING){
callback.Run(ok);
}
Global::debug(0) << "Callback running" << std::endl;
}
virtual void OnOpen(int32_t ok){
Global::debug(0) << "Opened! " << ok << std::endl;
system->run2();
}
pp::URLRequestInfo request;
pp::URLLoader loader;
pp::CompletionCallbackFactory<Handler> factory;
volatile bool http;
NetworkSystem * system;
};
+*/
void NetworkSystem::run(){
- portal.acquire();
- portal.wait();
- Global::debug(0) << "Requesting a file operation" << std::endl;
-
- Handler * handler = new Handler(instance, operation.absolute, this);
- handler->start();
- /* the handler will call into the browser. the browser will asynchounsly
- * call the handler again and the handler will call run2 to finish up
- */
- Global::debug(0) << "Releasing control back to the browser" << std::endl;
-}
-
-void NetworkSystem::run2(){
- Global::debug(0) << "File operation done" << std::endl;
- operation.complete = true;
- portal.release();
+ Util::Thread::Id thread;
+ Util::Thread::createThread(&thread, NULL, &Manager::run, manager);
+ // manager->run();
}
bool NetworkSystem::exists(const AbsolutePath & path){
/*
Global::debug(0) << "Checking for " << path.path() << std::endl;
pp::CompletionCallback::Block blocking;
pp::CompletionCallback callback(blocking);
pp::URLRequestInfo request(instance);
pp::URLLoader loader(instance);
string url = serverPath + path.path();
Global::debug(0) << "Url: " << url << std::endl;
// request.SetURL(serverPath + path.path());
request.SetURL("/" + path.path());
request.SetMethod("GT");
int32_t ok = loader.Open(request, callback);
Global::debug(0) << "Ok: " << ok << std::endl;
if (ok == PP_OK){
return true;
} else {
return false;
}
*/
portal.acquire();
operation.type = Exists;
operation.absolute = path;
operation.complete = false;
+ operation.success = false;
Global::debug(0) << "Request open" << std::endl;
- portal.signal();
+ run();
+ // portal.signal();
portal.wait(operation.complete);
portal.release();
Global::debug(0) << "Request open complete" << std::endl;
return false;
/*
Handler handler(instance, path);
handler.start();
handler.wait();
return false;
*/
}
/* TODO */
std::vector<AbsolutePath> NetworkSystem::getFilesRecursive(const AbsolutePath & dataPath, const std::string & find, bool caseInsensitive){
std::vector<AbsolutePath> paths;
return paths;
}
/* TODO */
std::vector<AbsolutePath> NetworkSystem::getFiles(const AbsolutePath & dataPath, const std::string & find, bool caseInsensitive){
std::vector<AbsolutePath> paths;
return paths;
}
/* TODO */
AbsolutePath NetworkSystem::configFile(){
return AbsolutePath();
}
/* TODO */
AbsolutePath NetworkSystem::userDirectory(){
return AbsolutePath();
}
/* TODO */
std::vector<AbsolutePath> NetworkSystem::findDirectories(const RelativePath & path){
return std::vector<AbsolutePath>();
}
/* TODO */
AbsolutePath NetworkSystem::findInsensitive(const RelativePath & path){
return AbsolutePath();
}
/* TODO */
AbsolutePath NetworkSystem::lookupInsensitive(const AbsolutePath & directory, const RelativePath & path){
return AbsolutePath();
}
}
#endif
diff --git a/util/nacl/network-system.h b/util/nacl/network-system.h
index b456b7b3..39375f24 100644
--- a/util/nacl/network-system.h
+++ b/util/nacl/network-system.h
@@ -1,49 +1,50 @@
#ifndef _paintown_network_system_h
#define _paintown_network_system_h
#ifdef NACL
#include <string>
#include "../file-system.h"
#include "../thread.h"
namespace pp{
class Instance;
}
namespace Nacl{
typedef Path::AbsolutePath AbsolutePath;
typedef Path::RelativePath RelativePath;
+class Manager;
class NetworkSystem: public Storage::System {
public:
NetworkSystem(const std::string & serverPath, pp::Instance * instance);
virtual ~NetworkSystem();
virtual AbsolutePath find(const RelativePath & path);
virtual RelativePath cleanse(const AbsolutePath & path);
virtual bool exists(const RelativePath & path);
virtual bool exists(const AbsolutePath & path);
virtual std::vector<AbsolutePath> getFilesRecursive(const AbsolutePath & dataPath, const std::string & find, bool caseInsensitive = false);
virtual std::vector<AbsolutePath> getFiles(const AbsolutePath & dataPath, const std::string & find, bool caseInsensitive = false);
virtual AbsolutePath configFile();
virtual AbsolutePath userDirectory();
virtual std::vector<AbsolutePath> findDirectories(const RelativePath & path);
virtual AbsolutePath findInsensitive(const RelativePath & path);
virtual AbsolutePath lookupInsensitive(const AbsolutePath & directory, const RelativePath & path);
virtual void run();
- virtual void run2();
protected:
pp::Instance * instance;
std::string serverPath;
Util::Thread::LockObject portal;
+ Manager * manager;
};
}
#endif
#endif
diff --git a/util/system.cpp b/util/system.cpp
index 628cbc6c..3e239e6d 100644
--- a/util/system.cpp
+++ b/util/system.cpp
@@ -1,209 +1,209 @@
#include <string>
#include "system.h"
#include <stdint.h>
#include <stdio.h>
#include <fstream>
#ifndef WINDOWS
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#endif
#ifndef WINDOWS
/* devkitpro doesn't have an implementation of access() yet. if it gets one this function
* can be removed.
*/
#if defined(WII) || defined(PS3)
int access(const char * path, int mode){
struct stat information;
int ok = stat(path, &information);
// printf("stat of '%s' is %d\n", path.c_str(), ok);
if (ok == 0){
if (mode == R_OK){
if (((information.st_mode & S_IRUSR) == S_IRUSR) ||
((information.st_mode & S_IRGRP) == S_IRGRP) ||
((information.st_mode & S_IROTH) == S_IROTH)){
return 0;
} else {
/* handle other modes if they become useful to us */
return -1;
}
} else {
return -1;
}
} else {
// perror("stat");
return -1;
}
}
#endif
#ifdef NACL
/* NOTE FIXME Missing I/O in Native Client */
extern "C" {
int pipe (int filedes[2]){
return 0;
}
int mkdir (const char *filename, mode_t mode){
return 0;
}
int access (const char *filename, int how){
return 0;
}
char * getcwd (char *buffer, size_t size){
return NULL;
}
int lstat (const char *path, struct stat *buf){
return 0;
}
int rmdir (const char *filename){
return 0;
}
int chdir (const char *filename){
return 0;
}
int setuid (uid_t newuid){
return 0;
}
int seteuid (uid_t uid){
return 0;
}
uid_t geteuid (void){
return NULL;
}
int setgid (gid_t gid){
return 0;
}
gid_t getgid (void){
return NULL;
}
int setegid (gid_t gid){
return 0;
}
gid_t getegid (void){
return NULL;
}
char * getlogin (void){
return NULL;
}
uid_t getuid(void){
return NULL;
}
struct passwd * getpwuid (uid_t uid){
return NULL;
}
struct passwd * getpwnam (const char *name){
return NULL;
}
struct group * getgrnam(const char *name){
return NULL;
}
struct group * getgrgid(gid_t gid){
return NULL;
}
int link (const char *oldname, const char *newname){
return 0;
}
int unlink (const char *filename){
return 0;
}
int kill(pid_t pid, int sig){
return 0;
}
}
#endif
static bool isReadable(const std::string & path){
return access(path.c_str(), R_OK) == 0;
}
bool System::isDirectory(const std::string & path){
struct stat info;
if (stat(path.c_str(), &info) == 0){
if (S_ISDIR(info.st_mode) == 1){
return true;
} else {
return false;
}
}
return false;
}
bool System::readableFile(const std::string & path){
return isReadable(path) && ! isDirectory(path);
}
bool System::readable(const std::string & path){
return isReadable(path);
}
void System::makeDirectory(const std::string & path){
mkdir(path.c_str(), 0777);
}
uint64_t System::currentMicroseconds(){
struct timeval hold;
gettimeofday(&hold, NULL);
return hold.tv_sec * 1000 * 1000 + hold.tv_usec;
}
uint64_t System::getModificationTime(const std::string & path){
struct stat data;
if (stat(path.c_str(), &data) == 0){
return data.st_mtime;
}
return 0;
}
static void * start_memory = 0;
unsigned long System::memoryUsage(){
void * here = sbrk(0);
/* hopefully the heap is growing up */
return (char*) here - (char*) start_memory;
}
void System::startMemoryUsage(){
start_memory = sbrk(0);
}
#endif
void System::makeAllDirectory(const std::string & path){
- unsigned int last = path.find('/');
+ size_t last = path.find('/');
while (last != std::string::npos){
std::string sofar = path.substr(0, last);
if (sofar != ""){
makeDirectory(sofar);
}
last = path.find('/', last + 1);
}
makeDirectory(path);
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:28 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68416
Default Alt Text
(14 KB)

Event Timeline