Bitte geben Sie eine Commit-Beschreibung für Ihre Änderungen ein. Zeilen,

This commit is contained in:
cigerxwinchaker 2019-12-18 10:57:04 +01:00
parent 2334b55dd4
commit 46b3c0adf7
5 changed files with 32 additions and 61 deletions

View File

@ -1,20 +1,14 @@
#include "../src/SharedMemory.h"
#include "vkvm.hpp"
#include <cstdlib>
#include <iostream>
#include <sys/shm.h>
#include <csignal>
#include "../src/SharedMemory.h"
#include "vkvm.hpp"
int main() {
vkvm::initialize(0);
initSharedMemory();
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = deleteSharedMemory;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, nullptr);
vkvm::setDefaultValues();
std::atexit(deleteSharedMemory);
std::string eingabe;

View File

@ -1,16 +1,11 @@
#include <cstdlib>
#include "SharedMemory.h"
#include <iostream>
#include <fstream>
#include <cpptoml.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vkvm.hpp>
key_t changedKey()
{
key_t changedKey() {
std::ofstream keyFile;
keyFile.open("/tmp/vkvmKey.log", std::ofstream::out | std::ofstream::trunc);
keyFile.clear();
@ -18,44 +13,40 @@ key_t changedKey()
std::cout << "Type in a new Key for the Shared Memory Segment" << std::endl;
std::cout << "new Key#";
std::cin >> newKey;
std::cout <<"New Key is -> " << newKey << std::endl;
std::cout << "New Key is -> " << newKey << std::endl;
keyFile << newKey;
keyFile.close();
return key_t(newKey);
}
std::string getAnswerFromUser()
{
std::cout <<"answer# ";
std::string getAnswerFromUser() {
std::cout << "answer# ";
std::string answer;
std::cin >> answer;
if(answer.compare("y") == 0 || answer.compare("n") == 0|| answer.compare("override") == 0) {
if (answer == "y" || answer == "n" || answer == "override") {
return answer;
} else {
std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n";
getAnswerFromUser();
}
std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n";
getAnswerFromUser();
return "-0";
}
void initSharedMemory(void) {
if(vkvm::impl.sharedMemoryKey != key_t(0)) {
if (shmget(vkvm::impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0)
{
void initSharedMemory() {
if (vkvm::impl.sharedMemoryKey != key_t(0)) {
if (shmget(vkvm::impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0) {
std::cout << "Shared Memory with Key: " << vkvm::impl.sharedMemoryKey << " is already in use\n"
<<"You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n";
<< "You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n";
std::string answer = getAnswerFromUser();
if(answer.compare("y") == 0)
{
if (answer == "y") {
vkvm::impl.sharedMemoryKey = changedKey();
std::cout << "new Try with Key: " << vkvm::impl.sharedMemoryKey << std::endl;
initSharedMemory();
} else if(answer.compare("n") == 0) {
std::cout << "This will end shared memory" <<std::endl;
} else if (answer == "n") {
std::cout << "This will end shared memory" << std::endl;
exit(0);
} else if(answer.compare("override") == 0) {
} else if (answer == "override") {
deleteSharedMemory();
initSharedMemory();
}
@ -63,34 +54,23 @@ void initSharedMemory(void) {
std::cout << "Shared Memory is (Re-) allocated with Key: " << vkvm::impl.sharedMemoryKey << std::endl;
}
} else {
std::cerr <<"key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl;
std::cerr << "key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl;
vkvm::impl.sharedMemoryKey = changedKey();
initSharedMemory();
}
}
void deleteSharedMemory(int s)
{
std::cerr << "programm ended with Code: " << s << std::endl;
if (shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 )
{
std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs, then delete with ipcrm -m $memID " << std::endl;
} else {
std::cout << "shared Memory deleted" << std::endl;
}
}
void deleteSharedMemory(void)
{
if (shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 )
{
void deleteSharedMemory() {
int id = shmctl(shmget(vkvm::impl.sharedMemoryKey, 0, 0), IPC_RMID, 0);
if (id < 0) {
std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs " << std::endl;
} else {
int x =0;
std::cout << "shared Memory deleted" << std::endl;
}
}
void getConfig(){
void getConfig() {
auto config = cpptoml::parse_file(configFile);
int layoutVersion = config->get_as<int>("layoutVersion").value_or(0);
@ -123,7 +103,7 @@ void getConfig(){
vkvm::setForegroundColor(vkvm::Color(foregroundColor));
vkvm::setCharactersPerRow(charactersPerRow);
vkvm::setCharactersPerColumn(charactersPerColumn);
vkvm::setFont(vkvm::FontType(font,"",0,0));
vkvm::setFont(vkvm::FontType(font, "", 0, 0));
vkvm::setMousePosition(mousePositionX, mousePositionY);
}

View File

@ -1,16 +1,16 @@
#ifndef SHARED_MEMORY_H
#define SHARED_MEMORY_H
#include "internal.hpp" //NOLINT
#include <sys/shm.h>
#include <iostream>
#include "internal.hpp"
//ID-Speicherbereich
//Größe des Speicherbereichs hier 8MB (8000)
//Shared-Memory-Segment erstellen oder öffnen shmget()
#define Max_Memory_Size 8000 * 512
constexpr int Max_Memory_Size (8000 * 512);
void deleteSharedMemory(int s);
void deleteSharedMemory(void);
void initSharedMemory(void);
void deleteSharedMemory();
void initSharedMemory();
void getConfig();
key_t changedKey();
std::string getAnswerFromUser();

View File

@ -1,5 +1,4 @@
#include "../src/SharedMemory.h"
#include <catch2/catch.hpp>
TEST_CASE("Demo test") {
REQUIRE(42 == 42);

View File

@ -1,6 +1,4 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
//Dont touch this file.
// add your own tests in this directory according to https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md