Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
logging-scratch-gui
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Goessmann
logging-scratch-gui
Commits
4bbe6704
Commit
4bbe6704
authored
6 years ago
by
Karishma Chadha
Browse files
Options
Downloads
Patches
Plain Diff
Create a cloud provider to create and manage a connection to the web socket.
parent
1bca98c8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib/cloud-provider.js
+104
-0
104 additions, 0 deletions
src/lib/cloud-provider.js
with
104 additions
and
0 deletions
src/lib/cloud-provider.js
0 → 100644
+
104
−
0
View file @
4bbe6704
import
log
from
'
./log.js
'
;
class
CloudProvider
{
constructor
(
cloudHost
,
vm
,
user
,
projectId
)
{
this
.
vm
=
vm
;
this
.
username
=
user
;
this
.
projectId
=
projectId
;
// Open a websocket connection to the clouddata server
this
.
openConnection
(
cloudHost
);
}
/**
* Open a new websocket connection to the clouddata server.
* @param {string} cloudHost The cloud data server to connect to.
*/
openConnection
(
cloudHost
)
{
if
(
window
.
WebSocket
===
null
)
{
log
.
warn
(
'
Websocket support is not available in this browser
'
);
this
.
connection
=
null
;
return
;
}
this
.
connection
=
new
WebSocket
((
location
.
protocol
===
'
http:
'
?
'
ws://
'
:
'
wss://
'
)
+
cloudHost
);
this
.
connection
.
onerror
=
e
=>
{
log
.
error
(
`Websocket connection error:
${
JSON
.
stringify
(
e
)}
`
);
// TODO Add re-connection attempt logic here
};
this
.
connection
.
onmessage
=
event
=>
{
const
messageString
=
event
.
data
;
log
.
info
(
`Received websocket message:
${
messageString
}
`
);
const
message
=
JSON
.
parse
(
messageString
);
if
(
message
.
method
===
'
set
'
)
{
const
varData
=
{
projectId
:
this
.
projectId
,
varUpdate
:
{
projectId
:
message
.
project_id
,
name
:
message
.
name
,
value
:
message
.
value
}
};
this
.
vm
.
postIOData
(
'
cloud
'
,
varData
);
}
};
this
.
connection
.
onopen
=
()
=>
{
this
.
writeToServer
(
'
handshake
'
);
log
.
info
(
`Successfully connected to clouddata server.`
);
};
this
.
connection
.
onclose
=
()
=>
{
log
.
info
(
`Closed connection to websocket`
);
};
}
/**
* Format and send a message to the cloud data server.
* @param {string} methodName The message method, indicating the action to perform.
* @param {string} dataName The name of the cloud variable this message pertains to
* @param {string | number} dataValue The value to set the cloud variable to
* @param {number} dataIndex The index of the item to update (for cloud lists)
* @param {string} dataNewName The new name for the cloud variable (if renaming)
*/
writeToServer
(
methodName
,
dataName
,
dataValue
,
dataIndex
,
dataNewName
)
{
const
msg
=
{};
msg
.
method
=
methodName
;
msg
.
user
=
this
.
username
;
msg
.
project_id
=
this
.
projectId
;
if
(
dataName
)
msg
.
name
=
dataName
;
if
(
dataValue
)
msg
.
value
=
dataValue
;
if
(
dataIndex
)
msg
.
index
=
dataIndex
;
if
(
dataNewName
)
msg
.
new_name
=
dataNewName
;
const
dataToWrite
=
JSON
.
stringify
(
msg
);
this
.
sendCloudData
(
dataToWrite
);
}
/**
* Send a formatted message to the cloud data server.
* @param {string} data The formatted message to send.
*/
sendCloudData
(
data
)
{
this
.
connection
.
send
(
`
${
data
}
\n`
);
log
.
info
(
`Sent message to clouddata server:
${
data
}
`
);
}
/**
* Provides an API for the VM's cloud IO device to update
* a cloud variable on the server.
* @param {string} name The name of the variable to update
* @param {string | number} value The new value for the variable
*/
updateVariable
(
name
,
value
)
{
this
.
writeToServer
(
'
set
'
,
name
,
value
);
}
}
export
default
CloudProvider
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment