aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Server.tsx120
-rw-r--r--src/client/Server.ts5
-rw-r--r--src/server/Client.ts15
-rw-r--r--src/server/Message.ts27
-rw-r--r--src/server/server.ts31
5 files changed, 198 insertions, 0 deletions
diff --git a/src/Server.tsx b/src/Server.tsx
new file mode 100644
index 000000000..729dfa873
--- /dev/null
+++ b/src/Server.tsx
@@ -0,0 +1,120 @@
+import { Field, FieldWaiting, FIELD_ID, DOC_ID, FIELD_WAITING } from "./fields/Field"
+import { Key, KeyStore } from "./fields/Key"
+import { ObservableMap, computed, action, observable } from "mobx";
+import { Document } from "./fields/Document"
+import * as OpenSocket from 'socket.io-client';
+import { Utils } from "./Utils";
+import { MessageStore } from "./server/Message";
+
+export class Server {
+ static FieldStore: ObservableMap<FIELD_ID, Field> = new ObservableMap();
+ static DocumentStore: ObservableMap<DOC_ID, ObservableMap<Key, FIELD_ID>> = new ObservableMap();
+ static Socket: SocketIOClient.Socket = OpenSocket("http://localhost:8080")
+ static GUID: string = Utils.GenerateGuid()
+
+ public static ClientFieldsCached: ObservableMap<DOC_ID, Field | FIELD_WAITING> = new ObservableMap();
+
+ // 'hack' is here temoporarily for simplicity when debugging things.
+ // normally, you can't assume this will return a document since the server responds asynchronously
+ // and there might not actually be a matching document on the server.
+ // the right way to call this is from within a reaction where you test whether the return value is FieldWaiting.
+ public static GetDocument(docid: DOC_ID, hack: boolean = false) {
+ if (!this.ClientFieldsCached.has(docid)) {
+ this.SEND_DOCUMENT_REQUEST(docid, hack);
+ }
+ return this.ClientFieldsCached.get(docid) as Document;
+ }
+ public static AddDocument(document: Document) {
+ // Replace with call to server
+ this.DocumentStore.set(document.Id, new ObservableMap());
+ document.fields.forEach((field, key) => {
+ this.FieldStore.set((field as Field).Id, (field as Field));
+ this.DocumentStore.get(document.Id)!.set(key, (field as Field).Id);
+ });
+ }
+ public static AddDocumentField(doc: Document, key: Key, value: Field) {
+ // Replace with call to server
+ if (this.DocumentStore.get(doc.Id))
+ this.DocumentStore.get(doc.Id)!.set(key, value.Id);
+ }
+ public static DeleteDocumentField(doc: Document, key: Key) {
+ // Replace with call to server
+ if (this.DocumentStore.get(doc.Id))
+ this.DocumentStore.get(doc.Id)!.delete(key);
+ }
+ public static SetFieldValue(field: Field, value: any) {
+ // Replace with call to server
+ if (this.FieldStore.get(field.Id))
+ this.FieldStore.get(field.Id)!.TrySetValue(value);
+ }
+
+
+ @action
+ public static GetDocumentField(doc: Document, key: Key) {
+ var fieldid = doc._proxies.get(key);
+ if (!this.ClientFieldsCached.has(fieldid)) {
+ this.ClientFieldsCached.set(fieldid, FieldWaiting);
+ this.SEND_DOCUMENT_FIELD_REQUEST(doc, key, fieldid);
+ }
+
+ var field = this.ClientFieldsCached.get(fieldid);
+ if (field != FieldWaiting) {
+ doc._proxies.delete(key); // perhaps another document inquired the same field
+ }
+ return field;
+ }
+ static times = 0; // hack for testing
+
+ @action
+ static cacheField(clientField: Field) {
+ var cached = this.ClientFieldsCached.get(clientField.Id);
+ if (!cached || cached == FieldWaiting) {
+ this.ClientFieldsCached.set(clientField.Id, clientField);
+ } else {
+ // probably should overwrite the values within any field that was already here...
+ }
+ return this.ClientFieldsCached.get(clientField.Id) as Field;
+ }
+
+ public static SEND_DOCUMENT_FIELD_REQUEST(doc: Document, key: Key, fieldid: FIELD_ID) {
+ //simulating a server call with a registered callback action
+ setTimeout(() => this.receivedDocumentField(doc, key, fieldid, this.FieldStore.get(fieldid)),
+ key == KeyStore.Data ? (this.times++ == 0 ? 5000 : 1000) : key == KeyStore.X ? 2500 : 500
+ )
+ }
+
+ public static SEND_DOCUMENT_REQUEST(docid: DOC_ID, hack: boolean = false) {
+ if (hack) { // temporary for debugging
+ this.receivedDocument(docid, this.DocumentStore.get(docid)!)
+ } else {
+ //simulating a server call with a registered callback action
+ setTimeout(() => this.receivedDocument(docid, this.DocumentStore.get(docid)!), 1500);
+ }
+ }
+
+ @action
+ static connected(message: string) {
+ console.log(message)
+ Server.Socket.emit("id", Server.GUID)
+ }
+
+ @action
+ static receivedDocument(docid: DOC_ID, fieldlist: ObservableMap<Key, FIELD_ID>) {
+ var cachedDoc = this.cacheField(new Document(docid));
+ fieldlist!.forEach((field: FIELD_ID, key: Key) => (cachedDoc as Document)._proxies.set(key, field));
+ }
+
+ @action
+ static receivedDocumentField(doc: Document, key: Key, fieldid: FIELD_ID, fieldfromserver: Field | undefined) {
+ doc._proxies.delete(key);
+ var cachedField = this.cacheField(fieldfromserver!);
+
+ // if the field is a document and it wasn't already cached, then we need to inquire all of its fields from the server...
+ if (cachedField instanceof Document && fieldfromserver! == cachedField) {
+ this.SEND_DOCUMENT_REQUEST(cachedField.Id);
+ }
+ doc.fields.set(key, cachedField);
+ }
+}
+
+Server.Socket.on(MessageStore.Handshake.Message, Server.connected); \ No newline at end of file
diff --git a/src/client/Server.ts b/src/client/Server.ts
index 85e55a84e..66ba92497 100644
--- a/src/client/Server.ts
+++ b/src/client/Server.ts
@@ -3,9 +3,14 @@ import { Key, KeyStore } from "../fields/Key"
import { ObservableMap, action } from "mobx";
import { Document } from "../fields/Document"
import { SocketStub } from "./SocketStub";
+import * as OpenSocket from 'socket.io-client';
+import { Utils } from "./../Utils";
+import { MessageStore } from "./../server/Message";
export class Server {
private static ClientFieldsCached: ObservableMap<FIELD_ID, Field | FIELD_WAITING> = new ObservableMap();
+ static Socket: SocketIOClient.Socket = OpenSocket("http://localhost:8080")
+ static GUID: string = Utils.GenerateGuid()
// Retrieves the cached value of the field and sends a request to the server for the real value (if it's not cached).
// Call this is from within a reaction and test whether the return value is FieldWaiting.
diff --git a/src/server/Client.ts b/src/server/Client.ts
new file mode 100644
index 000000000..6b8841658
--- /dev/null
+++ b/src/server/Client.ts
@@ -0,0 +1,15 @@
+import { computed } from "mobx";
+
+export class Client {
+ constructor(guid: string) {
+ this.guid = guid
+ }
+
+ private guid: string;
+
+ @computed
+ public get GUID(): string {
+ return this.guid
+ }
+
+} \ No newline at end of file
diff --git a/src/server/Message.ts b/src/server/Message.ts
new file mode 100644
index 000000000..d5b9b4612
--- /dev/null
+++ b/src/server/Message.ts
@@ -0,0 +1,27 @@
+import { Utils } from "../Utils";
+
+export class Message {
+ private name: string;
+ private guid: string;
+
+ get Name(): string {
+ return this.name;
+ }
+
+ get Message(): string {
+ return this.guid
+ }
+
+ constructor(name: string) {
+ this.name = name;
+ this.guid = Utils.GenerateDeterministicGuid(name)
+ }
+
+ GetValue() {
+ return this.Name;
+ }
+}
+
+export namespace MessageStore {
+ export const Handshake = new Message("Handshake");
+} \ No newline at end of file
diff --git a/src/server/server.ts b/src/server/server.ts
new file mode 100644
index 000000000..db58ed662
--- /dev/null
+++ b/src/server/server.ts
@@ -0,0 +1,31 @@
+import { MessageStore } from "./Message";
+
+// const express = require("express")
+// const path = require("path")
+
+// const app = express();
+// app.set("port", process.env.PORT || 3000);
+
+// var http = require('http').Server(app);
+
+// app.get('/', function (req: any, res: any) {
+// res.sendFile(path.resolve("./deploy/index.html"))
+// })
+
+// const server = http.listen(3000, function () {
+// console.log("Listening on *:3000")
+// })
+
+const server = require("socket.io")();
+var clients = [];
+
+server.on("connection", function (socket: any) {
+ console.log("a user has connected")
+
+ socket.emit(MessageStore.Handshake.Message, "handshake received")
+
+ clients.push(socket)
+})
+
+server.listen(8080);
+console.log("listening on port 8080") \ No newline at end of file